使用Java。中的docx4j库将文档转换为pdf时出现错误,可悲的是,我的错误是这个
不支持v:pict,不支持v:imagedata
并且它显示在转换后的pdf上,而不是在我的java终端中显示错误。
我浏览了一些文章和问题,因此找到了这个converting docx to pdf。但是,我不确定如何在我的代码中使用它或将其转换。这是我的代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class docTopdf {
public static void main(String[] args) {
try {
InputStream is = new FileInputStream(
new File(
"test.docx"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(is);
List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
for (int i = 0; i < sections.size(); i++) {
wordMLPackage.getDocumentModel().getSections().get(i)
.getPageDimensions();
}
PhysicalFonts.discoverPhysicalFonts();
@Deprecated
Map<String, PhysicalFont> physicalFonts = PhysicalFonts.getPhysicalFonts();
// 2) Prepare Pdf settings
@Deprecated
PdfSettings pdfSettings = new PdfSettings();
// 3) Convert WordprocessingMLPackage to Pdf
@Deprecated
org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
wordMLPackage);
@Deprecated
OutputStream out = new FileOutputStream(
new File(
"test.pdf"));
conversion.output(out, pdfSettings);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
还有我的pom.xml
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>3.2.1</version>
</dependency>
由于我对此转换没有任何帮助,不胜感激。在此先感谢
通过XSL FO创建PDF在没有v:imagedata(即不是简单图像的图形)的情况下不支持w:pict。
虽然您可以通过适当配置日志记录来抑制此消息,但是您的PDF输出将是有损的。
您的选择是更正输入docx(即使用图像代替当前使用的图像),或使用具有适当支持的PDF转换器。有关一个选项,请参见https://www.docx4java.org/blog/2020/03/documents4j-for-pdf-output/