利用aspose进行word转pdf、打印pdf

本文介绍了如何使用Aspose库在Java中实现Word文档到PDF的转换,以及如何打印PDF文件。代码展示了如何加载文件,设置保存选项,执行转换并打印PDF的过程。
摘要由CSDN通过智能技术生成

Word 转 PDF:

import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.aspose.words.SaveOptions;
import com.aspose.words.PdfSaveOptions;

public class WordToPdfConverter {

    public static void convertToPdf(String inputFilePath, String outputFilePath) {
        try {
            // 加载 Word 文档
            Document document = new Document(inputFilePath);

            // 设置 PDF 保存选项
            PdfSaveOptions saveOptions = new PdfSaveOptions();
            saveOptions.setSaveFormat(SaveFormat.PDF);

            // 保存为 PDF 文件
            document.save(outputFilePath, saveOptions);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String inputFilePath = "path/to/your/input.docx";
        String outputFilePath = "path/to/your/output.pdf";

        convertToPdf(inputFilePath, outputFilePath);
        System.out.println("Conversion from Word to PDF completed.");
    }
}

打印 PDF:

import com.aspose.pdf.Document;
import com.aspose.pdf.PrinterSettings;

public class PdfPrinter {

    public static void printPdf(String inputFilePath) {
        try {
            // 加载 PDF 文档
            Document document = new Document(inputFilePath);

            // 创建打印机设置
            PrinterSettings printerSettings = new PrinterSettings();

            // 设置打印机
            printerSettings.setPrinterName("YourPrinterName");

            // 打印 PDF
            document.print(printerSettings);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String inputFilePath = "path/to/your/input.pdf";

        printPdf(inputFilePath);
        System.out.println("PDF printed successfully.");
    }
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值