Set PDF Version using iTextSharp

后端 未结 4 755
-上瘾入骨i
-上瘾入骨i 2020-12-11 08:29

Anyone know how to save a PDF as a lower PDF version programmatically using iTextSharp so that you can use certain iTextSharp features that require the PDF to be version 5 o

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 09:06

    How odd. PDF versions are mostly a suggestion. PDFs must start with something like:

    %PDF-1.x
    

    Where the X is 0,1,2,...

    This is just a clue to the app reading the PDF. The only clue. Most "I need version X" requests I see from various customers are bogus. My fellow iText coders know this, so it strikes me as odd that iText is requesting a different version.

    You're sure its iText requesting v5?

    At any rate, to answer your question:

    Yes, iText can change the version number of a PDF. Sadly, it can only be done when writing out a PDF, not when reading it in. You'll have to open the PDF, change its version, and save it again.

    You could probably cheat. Read the PDFs into byte arrays and pdfBytes[7] = 4;, then pass those bytes on to a PdfReader.

    Version 1 of the PDF spec is 1.0 Version 2 is 1.1 ...

    So if you want pdf version 5, you need to write out "1.4", not "1.5".

    If you're not comfortable poking a byte like that, you can parse the whole PDF, change the version, then write it all out again:

     PdfReader reader = new PdfReader(pdfPath);
     PdfStamper stamper = new PdfStamper(reader, outputStream);
     stamper.setPdfVersion(PdfWriter.PDF_VERSION_1_4);
     stamper.close();
    

    You'd then read it in again, and combine it as you have been.

提交回复
热议问题