Home > Mobile >  Convert PDF file version in java (not only header version!)
Convert PDF file version in java (not only header version!)

Time:09-23

I'm trying to convert PDF version 1.3 to PDF version 1.5 or above. The challenge here is: i don't want to change only header version like almost all forums writting on. I want to change ALL file version.

I read this topic: convert PDF to an older version from a servlet?, but firstly new version of iTextPdf 7, does not support PdfStamper so i just skip it.

I think I need to create a TMP file, write PDF to TMP, replace original with TMP and delete TMP. But how can i do that in JAVA?

This code only convert HEADER version! I use Itext version 7.

WriterProperties wp = new WriterProperties();

wp.setPdfVersion(PdfVersion.PDF_1_7);

PdfDocument pdfDoc = new PdfDocument(new PdfReader("source"), new PdfWriter("destination", wp));

pdfDoc.close();

Any suggestions?

picture from Firefox (PDF v 1.3) picture: no text available

here you can download pdf sample: https://wetransfer.com/downloads/ce2d2f41ac29c36baa2ac895ebc0473c20210922065257/5889b2

CodePudding user response:

There is no need to convert PDF version 1.3 to PDF version 1.5 or above as PDF is designed to be backwards compatible. Thus, every PDF 1.3 document already also is a PDF 1.4 document. And a PDF 1.5 document. And a PDF 1.6 document. ...

In a comment you explained why you want the version change nonetheless:

But if you want to open PDF 1.3 in Firefox, you cannot open it! So we have some clients which use firefox for opening PDF formats. Firefox only support 1.5 and above

In the light of the compatibility discussed above that does not make sense. But sometimes programs behave in a nonsensical way. Thus, I tested this.

The result: The Firefox 87.0 I have installed here accepts PDF 1.3 and PDF 1.4 files I found among my documents without any issue!

Unfortunately I don't have any PDF 1.2 (or earlier) files around here, so I cannot check support of such files.

Thus, I'm afraid you'll have to go back to analyzing the issue your customers have, it is not as simple as "Firefox only support 1.5 and above".

(Some ideas: Maybe your PDF 1.3 files actually are broken and Firefox fails to open them because of that; they may be broken already on your side or they may get broken during transfer to your clients. Or maybe your clients have some older Firefox version with some bugs in its PDF viewer.)

A Fix For The Actual Problem

In comments here the OP provided example files. Analyzing them it turned out that the Actual Problem is that Firefox cannot properly determine the built-in encoding of the embedded fonts.

To help Firefox in this regard, we can provide an explicit base encoding, so Firefox does not need the built-in encoding.

As you used iText 7 in your question, here a proof-of-concept working with your example PDF:

try (   PdfReader pdfReader = new PdfReader("1100-SD-9000455596.pdf");
        PdfWriter pdfWriter = new PdfWriter("1100-SD-9000455596-Fixed.pdf");
        PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter) ) {
    for (int page = 1; page <= pdfDocument.getNumberOfPages(); page  ) {
        PdfPage pdfPage = pdfDocument.getPage(page);
        PdfResources pdfResources = pdfPage.getResources();
        for (Entry<PdfName, PdfObject> fontEntry : pdfResources.getResource(PdfName.Font).entrySet()) {
            PdfObject fontObject = fontEntry.getValue();
            if (fontObject != null && fontObject.getType() == PdfObject.INDIRECT_REFERENCE) {
                fontObject = ((PdfIndirectReference)fontObject).getRefersTo(true);
            }
            if (fontObject instanceof PdfDictionary) {
                PdfDictionary fontDictionary = (PdfDictionary) fontObject;
                PdfDictionary encodingDictionary = fontDictionary.getAsDictionary(PdfName.Encoding);
                if (encodingDictionary != null) {
                    if (encodingDictionary.getAsName(PdfName.BaseEncoding) == null &&
                            encodingDictionary.getAsArray(PdfName.Differences) != null) {
                        encodingDictionary.put(PdfName.BaseEncoding, PdfName.WinAnsiEncoding);
                    }
                }
            }
        }
    }
}

(FixForFirefox test testFix1100_SD_9000455596)

  • Related