简体   繁体   中英

Text alingment lost when converting Docx to PDF with docx4j

I am using the code below in order to convert a MS Word docx file to pdf.

  private static ByteArrayOutputStream convertToPDFDocx4j(String inputfilepath) throws Exception {


    WordprocessingMLPackage wordMLPackage =
        WordprocessingMLPackage.load(new java.io.File(inputfilepath));

    FieldUpdater updater = new FieldUpdater(wordMLPackage);
    updater.update(true);
    Mapper fontMapper = new IdentityPlusMapper();
    wordMLPackage.setFontMapper(fontMapper);
    FOSettings foSettings = Docx4J.createFOSettings();
    foSettings.setWmlPackage(wordMLPackage);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Docx4J.toFO(foSettings, baos, Docx4J.FLAG_EXPORT_PREFER_XSL);

    if (wordMLPackage.getMainDocumentPart().getFontTablePart() != null) {
      wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
    }

    updater = null;
    foSettings = null;
    wordMLPackage = null;
    return baos;
  }

Although the pdf is generated, it loses the text alignment.

MS Word Format: MS Word 格式

Generated PDF: 生成的PDF

I have uploaded the word and generated pdf in the links below:

MS Word Document

Generated PDF

Your table cells contains:

      <w:p>
        <w:pPr>
          <w:tabs>
            <w:tab w:val="right" w:pos="14175"/>
          </w:tabs>
          <w:rPr>
            <w:rFonts w:cs="Arial"/>
            <w:b/>
            <w:color w:val="FFFFFF" w:themeColor="background1"/>
          </w:rPr>
        </w:pPr>

The w:tabs element is triggering https://github.com/plutext/docx4j-export-FO/blob/master/src/main/java/org/docx4j/convert/out/fo/XsltFOFunctions.java#L739 but that's unwanted in this case.

A workaround would be to remove the right aligned tab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM