您的位置 首页 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>
        </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:\\Users\\Test1\\Desktop\\Sample.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);
    }
}  

文章来源:智云一二三科技

文章标题:Java 打印PPT文档

文章地址:https://www.zhihuclub.com/181495.shtml

关于作者: 智云科技

热门文章

网站地图