java - docx4j:docx 到 pdf 的转换 - docx 内容没有逐页显示为 pdf
问题描述
问题:使用 DOCX4J 将 docx 转换为 pdf。问题是 docx 的内容没有逐页转换为 pdf 文档。第 2 页的几行出现在 pdf 的第 1 页中。
pom.xml:
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>6.1.2</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
代码:
private static void convertToPDFDocx4j() throws Exception {
InputStream is = new FileInputStream(new File(inputfilepath));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(is);
List sections = wordMLPackage.getDocumentModel().getSections();
for (int i = 0; i < sections.size(); i++) {
wordMLPackage.getDocumentModel().getSections().get(i)
.getPageDimensions();
}
Mapper fontMapper = new IdentityPlusMapper();
PhysicalFont font = PhysicalFonts.getPhysicalFonts().get(
"Comic Sans MS");//set your desired font
fontMapper.getFontMappings().put("Algerian", font);
wordMLPackage.setFontMapper(fontMapper);
PdfSettings pdfSettings = new PdfSettings();
org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
wordMLPackage);
OutputStream out = new FileOutputStream(new File(outputfilepath));
conversion.output(out, pdfSettings);
System.out.println("DONE!!");
}
想知道 docx4j 是否有控制它的设置?
试过了,但没有多大帮助 使用 Java 将 docx 文件转换为 PDF
解决方案
使用以下依赖项将您的 Docx4j 版本从 6.X 升级到 8.X 以解决此问题。
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-MOXy</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.0.0</version>
</dependency>
使用以下代码进行 docx 到 pdf 的转换。
import org.docx4j.Docx4J;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class DocToPDF {
public static void main(String[] args) {
try {
InputStream templateInputStream = new FileInputStream("D:\\\\Workspace\\\\New\\\\Sample.docx");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
String outputfilepath = "D:\\\\Workspace\\\\New\\\\Sample.pdf";
FileOutputStream os = new FileOutputStream(outputfilepath);
Docx4J.toPDF(wordMLPackage,os);
os.flush();
os.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
推荐阅读
- python-3.x - 如何在python应用程序中使用elasticsearch?
- html - 在html img标签(spring boot)中从spring框架中的本地目录插入图像
- polymer - 在无头浏览器中运行 polymer-3.x 测试
- html - 页脚与移动设备上的内容重叠
- mysql - 如何阻止 GORM 按 ID 对我的预加载进行排序?
- python - 包含 %(百分比)和格式说明符的 Python 打印语句
- javascript - jQuery mousedown 在 chrome/safari 中不起作用
- angular - 如何解决这个问题
- python - python OpenCv IMREAD_UNCHANGED 只返回三个通道
- asp.net-core - 每当在数据库中更改时,更新用户声明列表中的声明值