乐趣区

关于java:Java-打印PPT文档

前言

本文将介绍如何应用 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);
    }
}
退出移动版