gpt4 book ai didi

pdf - docx4j:docx 到 pdf 的转换 - docx 内容没有逐页显示为 pdf

转载 作者:行者123 更新时间:2023-12-04 10:01:23 25 4
gpt4 key购买 nike

问题:使用 DOCX4J 将 docx 转换为 pdf。问题是 docx 的内容没有逐页转换为 pdf 文档。 pdf的第1页出现了第2页的几行。

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 是否具有控制它的设置?

文档:
docx

PDF:
pdf

试过了,但没有太大帮助
Convert docx file into PDF with Java

最佳答案

使用以下依赖项将您的 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();
}
}

}

关于pdf - docx4j:docx 到 pdf 的转换 - docx 内容没有逐页显示为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61805246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com