前言
本文将介绍如何应用Free Spire.Presentation for Java提供的PresentationPrintDocument和PrinterSettings对象来打印PowerPoint文档。代码示例次要从以下几个方面来解说打印办法:
- 通过默认打印机打印文档中的所有 幻灯片
- 通过默认打印机打印文档中的特定 幻灯片
- 通过虚构打印机打印
- 应用PrinterSettings对象打印
在运行代码前,须要配置测试环境。可通过E-iceblue官方网站下载Free Spire.Presentation for Java产品包,解压后将lib文件夹下的Spire.Presentation.jar手动导入IDEA中。
这里,我更举荐另一种办法,那就是通过Maven仓库装置产品及依赖包。创立一个Maven我的项目,在pom.xml文件下输出以下代码,而后点击“Import Changes” 即可。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
代码示例
通过默认打印机打印文档中的所有幻灯片
import com.spire.presentation.*;
public class PrintByDefaultPrinter {
public static void main(String[] args) throws Exception {
//加载示例文档
Presentation ppt = new Presentation();
ppt.loadFromFile("data/print.pptx");
//应用默认打印机打印文档中的所有幻灯片
PresentationPrintDocument document = new PresentationPrintDocument(ppt);
document.print();
ppt.dispose();
}
}
通过默认打印机打印文档中的特定幻灯片
在设置打印幻灯片的指定范畴时,还可通过setCopies办法来设置打印份数。
import com.spire.ms.Printing.PrintRange;
import com.spire.presentation.*;
public class PrintSpecificRange {
public static void main(String[] args) throws Exception {
//加载示例文档
Presentation presentation = new Presentation();
presentation.loadFromFile("data/Template_Ppt_6.pptx");
//应用默认打印机打印文档中的指定幻灯片
PresentationPrintDocument document = new PresentationPrintDocument(presentation);
document.getPrinterSettings().setPrintRange( PrintRange.SomePages);
document.getPrinterSettings().setFromPage(2);
document.getPrinterSettings().setToPage(3);
//设置打印份数
short copyies=2;
document.getPrinterSettings().setCopies(copyies);
presentation.print(document);
}
}
通过虚构打印机打印
import com.spire.presentation.*;
public class PrintByVirtualPrinter {
public static void main(String[] args) throws Exception {
//加载示例文档
Presentation presentation = new Presentation();
presentation.loadFromFile("data/Template_Ppt_6.pptx");
//应用虚构打印机打印文档中的所有幻灯片
PresentationPrintDocument document = new PresentationPrintDocument(presentation);
document.getPrinterSettings().setPrinterName("Microsoft XPS Document Writer");
presentation.print(document);
}
}
应用PrinterSettings对象打印
在打印过程中,可抉择打印所有幻灯片或局部幻灯片,可设置打印方向、是否加框、是否灰度打印、以及打印到一页的幻灯片数量。
import com.spire.ms.Printing.*;
import com.spire.presentation.*;
public class PrinterSettings {
public static void main(String[] args) throws Exception {
//加载示例文档
Presentation presentation = new Presentation();
presentation.loadFromFile("C:UsersTest1DesktopGermany.pptx");
//应用PrinterSettings对象打印文档中的幻灯片
com.spire.ms.Printing.PrinterSettings ps = new com.spire.ms.Printing.PrinterSettings();
ps.setPrintRange(PrintRange.AllPages);//打印所有幻灯片
//presentation.SelectSlidesForPrint("1", "3");//打印局部幻灯片 //设置打印文档名称 ps.setPrintToFile(true);
ps.setPrintFileName("output/setPrintSettingsByPrinterSettings.xps");
//设置幻灯片是否加框
presentation.setSlideFrameForPrint(true);
//设置是否灰度打印
presentation.setGrayLevelForPrint(false);
//设置打印到一页的幻灯片数量(此处每四张打印到一页)
presentation.setSlideCountPerPageForPrint(PageSlideCount.Four);
//设置打印方向
presentation.setOrderForPrint(Order.Horizontal);
//打印文档
presentation.print(ps);
}
}
发表回复