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.");
}
}