使用aspose.words从html制作的docx文件转为PDF样式表现混乱,但是把docx文件改一下再撤回再保存之后,再使用aspose.words转为PDF就正常了,但肉眼看不出两个docx文件的区别。

docx转为PDF的方法代码如下:

Document document = new Document(file.getInputStream());

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.save(outputStream, SaveFormat.PDF);

Files.write(Paths.get("/Users/pizhai/Downloads/report_0.pdf"), outputStream.toByteArray());

文件见附件:
没问题.zip (1.4 MB)

有问题.zip (1.4 MB)

@pizhai2

Cause

The issue you’re experiencing with the PDF conversion from the DOCX file may stem from how Aspose.Words processes the document’s formatting and content. Subtle differences in the document structure or formatting can lead to discrepancies in the output PDF.

Solution

To improve the PDF conversion quality, you can try the following steps:

  1. Load Options: Use LoadOptions to specify additional settings when loading the DOCX file. This can help in preserving certain formatting aspects.

  2. Update Fields: Ensure that any fields in the document are updated before saving. This can be done by calling document.updateFields().

  3. Check for Hidden Content: Sometimes, hidden or non-visible content can affect the output. Review the document for any such elements.

Code

Here’s an updated version of your code that incorporates these suggestions:

import com.aspose.words.*;

public class DocxToPdfConverter {
    public static void main(String[] args) throws Exception {
        // Load the document with LoadOptions
        LoadOptions loadOptions = new LoadOptions();
        Document document = new Document("path/to/your/document.docx", loadOptions);

        // Update fields in the document
        document.updateFields();

        // Save as PDF
        document.save("path/to/output/report.pdf", SaveFormat.PDF);
    }
}

Next Steps

  1. Test the updated code with your DOCX files.
  2. If the issue persists, check the document for any specific formatting or content that might be causing the problem.
  3. If necessary, share the problematic document for further analysis.

Please implement these suggestions and let me know the outcome!

@pizhai2 该问题无法重现。 能否请您提供更多有关该问题的信息以及重现该问题的文件?

1 Like

使用html转为docx再转为PDF的代码如下:

String htmlPath = "/Users/pizhai/Downloads/docx.html";
        //从html文件中获取html字符串
        org.jsoup.nodes.Document htmlDocument = Jsoup.parse(new File(htmlPath), "UTF-8", "");
        String html = htmlDocument.html();
        //将html字符串转换为docx文档
        Document docxDocument = new Document();
        DocumentBuilder builder = new DocumentBuilder(docxDocument);
        builder.insertHtml(html);

        NodeCollection<Table> tables = docxDocument.getChildNodes(NodeType.TABLE, true);

        for (Table table : tables) {
            table.setAllowAutoFit(false);
        }

        NodeCollection headerFooters = docxDocument.getChildNodes(NodeType.HEADER_FOOTER, true);

        for (int i = 0; i < headerFooters.getCount(); i++) {
            Node node = headerFooters.get(i);
            if (node.getNodeType() == NodeType.TABLE) {
                Table table = (Table) node;
                table.setAllowAutoFit(true);
            }
        }

        for (Section section : docxDocument.getSections()) {
            ParagraphCollection paragraphs = section.getBody().getParagraphs();
            for (Paragraph paragraph : paragraphs) {
                ParagraphFormat paragraphFormat = paragraph.getParagraphFormat();
                paragraphFormat.setFarEastLineBreakControl(true);
                paragraphFormat.setWordWrap(false);
                paragraphFormat.setHangingPunctuation(true);
                paragraphFormat.setBaselineAlignment(BaselineAlignment.CENTER);
            }
            // 获取文档的页面设置
            PageSetup pageSetup = section.getPageSetup();
            // 设置页脚的下边距
            pageSetup.setFooterDistance(20.0);
            // 设置页眉的上边距
            pageSetup.setHeaderDistance(10.0);
            // 设置页面的左边距
            pageSetup.setLeftMargin(36.0);
            // 设置页面的右边距
            pageSetup.setRightMargin(36.0);
            // 设置页面的上边距
            pageSetup.setTopMargin(36.0);
            // 设置页面的下边距
            pageSetup.setBottomMargin(36.0);
            // 设置分栏间的间距
            pageSetup.getTextColumns().setSpacing(20.0);
        }

        docxDocument.save("/Users/pizhai/Downloads/aspose_doc.pdf", SaveFormat.PDF);

代码运行环境:
Linux centos8
win11
macos 15.5

html文件如下:
docx.html.zip (30.8 KB)

转换结果中的问题:


但是如果保存为docx,然后在Office.word或者wps中,修改再撤回再保存一下,然后再转为PDF,结果就是正常的了。

如果使用了LoadOptions:

        LoadOptions loadOptions = new LoadOptions();
        loadOptions.setLoadFormat(LoadFormat.HTML);
        Document docxDocument = new Document(htmlPath, loadOptions);

则会导致下面的图片位置错乱

@pizhai2 感谢您提供更多信息。 下面的代码可以简单地解决形状问题:

Document doc = new Document("docx.html");
doc.save("output.pdf");

现在,形状的位置看起来就像输入的 html 中的一样。 能否请您提供输出文件中的预期结果?

你是真人吗?有没有可能我需要那些中间操作?图片给你了、html也给你了、PDF最初的时候也给你了,你还要什么?我打飞的过去?

@pizhai2 是的,我是一个真实的人,我希望尽可能多地了解您的情况,以便解决问题。之前提供的 DOCX 和 PDF 文档与 html 文档略有不同。DOCX 和 PDF 文档没有问题文档中提到的页眉/栏和形状,这些文档中的所有内容都是表格,而不是所提供的 html 文件。

您必须使用提供的代码正确转换 HTML 文档:

Document doc = new Document("docx.html");
doc.save("output.pdf");

就所提供的 html 而言,除了提到的形状之外,我没有发现任何内容问题。 因此,我需要了解您在将 html 转换为 pdf 后期望得到什么结果?

不便之处,敬请原谅。