【问题标题】:How can I add pTab elements to docx4j while converting document to pdf如何在将文档转换为 pdf 时将 pTab 元素添加到 docx4j
【发布时间】:2020-03-23 07:57:55
【问题描述】:

我在 Java 中使用 docx4j 库将 document 转换为 pdf 时遇到一些错误。遗憾的是,我的错误是这样的

不支持 w:pict without 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>

任何帮助都将不胜感激,因为我对这种转换并不熟悉。提前致谢

【问题讨论】:

  • 你的问题标题提到了 pTab,但问题正文是关于 w:pict?

标签: java pdf document docx4j


【解决方案1】:

通过 XSL FO 创建 PDF 不支持没有 v:imagedata 的 w:pict(即不是简单图像的图形)。

虽然您可以通过适当配置日志记录来隐藏消息,但您的 PDF 输出将是有损的。

您的选择是更正输入 docx(即使用图像而不是您当前拥有的任何内容),或使用具有适当支持的 PDF 转换器。对于一种选择,请参阅https://www.docx4java.org/blog/2020/03/documents4j-for-pdf-output/

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多