问题:使用 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();
}
}
}
fd izkFkhZ dh eka }kjk [kjhns x;s edku la[;k&vk0la0&27@2 fNodh ftldk edku u a0 392@681 pd j?kqukFk fnukad 27-09-2014 ehjk n soh iRuh Lo0 宽松 yky frokjh fuokfluh fNodh] u Suh] iz;kxjkt l s [kjhnk Fkk] izkFkhZ dh ek¡ d s uke ij jftLVªh gqbZ gSA ftudk uke fo|k frokjh iRuh Lo0 vfuy dqekj frokjh gSA ftldk bUn zkt [kljk [krkSuh ¼U;w vfHkys[kksa½ esa ntZ] gSA bl edku d s lsgu esa dqN [kkyh tehu dh Hkh jftLVªh ehjk nsoh us edku d s lkFk fd;k gS] ftl s Hkw&ekfQ;k x.ks”k frokjh vkSj 泥浆 Hkkb Z o 泥浆 iq= ,oa vU; lg;ksxh tcfj;u dCtk djuk pkgrs gSa vkSj edku esa yxs gq、rkys dks rksM+dj viuk rkyk yxk fn;k gSA izkFkhZ dh ekrk d s euk djus ij vkeknk Qlkn gksrs gSaA