我需要在 Java 应用程序中将 DOCX 文件转换为 PDF。我尝试了 xDocReport lib,但它不转换目录。在输出 PDF 文件中有空白区域而不是 ToC。
这是我的代码,很简单:
import java.io.*;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class DocxToPDFService {
public void generatePDF(String srcPath, String destPath) throws Exception {
XWPFDocument docx;
try {
docx = new XWPFDocument(new FileInputStream(srcPath));
} catch (IOException ioE) {
throw ioE;
}
PdfOptions options = PdfOptions.create().fontEncoding("windows-1250");
FileOutputStream pdf = new FileOutputStream(destPath);
PdfConverter.getInstance().convert(docx, pdf, options);
}
}
是否有任何解决方法可以使其转换 ToC?