1

The code creates a .docx and then creates a PDF from it. When open, the margins and styles do not match the .docx, but the main problem is that the text has moved and some images are missed. The code that cretes the PDF:

    InputStream doc = new FileInputStream(new File(sourcePath + name));
    XWPFDocument document = new XWPFDocument(doc);

    PdfOptions options = PdfOptions.getDefault();
    OutputStream out = new FileOutputStream(new File(destinationPath + name.replace("." + MimeUtil2.getExtension(name), "") + ".pdf"));
    PdfConverter.getInstance().convert(document, out, options);

About missed images I have no clue, with margins I have tried to add them adding the following code between instantiating the document and creating PdfOptions:

    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    CTPageMar pageMar = sectPr.addNewPgMar();
    pageMar.setLeft(BigInteger.valueOf(720L));
    pageMar.setTop(BigInteger.valueOf(1440L));
    pageMar.setRight(BigInteger.valueOf(720L));
    pageMar.setBottom(BigInteger.valueOf(1440L));

But do not work.

I found a way to modify the margins: adding the same code above in the creation of the .docx, where it seems to work. But that does not fix the problem of the missed image and moved texts. Also it makes the code uglier and does not feel right because the styles are added correctly after (in the writing of the .docx).

How can i keep the styles from the .docx or adding them to make the PDF the closest to the .docx?

Java 1.8 , Apache POI 3.15 and Pdfbox 2.0.9 .

3
  • 1
    Your question is not about iText. Tag removed. Commented May 8, 2018 at 6:31
  • I don't see how this is about PDFBox, there is no PDFBox code there. PdfConverter is from POI (or does it use PDFBox under the hood?). Commented May 9, 2018 at 15:57
  • You are right, I use PDFBox but later, print the fist PDF to merge multiple PDFs in one. Commented May 18, 2018 at 6:09

1 Answer 1

2

I managed to find a solution but only works on Windows(probably easy to convert to use in Linux, but I didn't research it). My problem was the .docx didn't looks exactly like the printed PDF. I used OfficeToPDF and called it with java. The command line is like this:

[path-to-officetopdf]/officetopdf.exe [source]\myFile.docx [destination]\myNewPDF.pdf

To anyone who may concern, this is how you execute a shell command with java:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public static String executeShellCommand(String command) {
        StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";           
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();

}

I hope this can help someone, if I found the way to make it with Linux I will post it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.