美文网首页Java
Java 打印PPT文档

Java 打印PPT文档

作者: Tina_Tang | 来源:发表于2020-09-25 15:19 被阅读0次

    前言

    本文将介绍如何使用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:\\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.haomeiwen.com/subject/mwsfuktx.html