5

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 or lower?

I'm trying to merge two PDF version 7 documents together and it insists that they be version 5 or lower.

RichC
  • 7,829
  • 21
  • 85
  • 149

4 Answers4

5
///for itextSharp 5.4.4
PdfReader reader = new PdfReader(pdfPath);
PdfStamper stamper = new PdfStamper(reader, outputStream); 
stamper.Writer.setPdfVersion(PdfWriter.PDF_VERSION_1_4); 
stamper.close(); 
user2831210
  • 51
  • 1
  • 2
3

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.

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
  • You were write, I didn't realize I was using PDFSharp and not iTextSharp. iTextSharp looks much better but I am having an issue [over here](http://stackoverflow.com/questions/6326506/how-do-i-combine-merge-pdfs-with-fillable-form-fields-using-itextsharp) – RichC Jun 13 '11 at 03:54
  • “How odd. PDF versions are mostly a suggestion.” It seems worth being explicit—I came here because I was patching PDFs with embedded fonts. Adobe refused to render the fonts. I set the version number in the stamper to 1.4 and it rendered fine in Adobe. Seems that it takes the version literally at least in that case. – Christopher Done Nov 11 '11 at 11:06
  • Also odd. Adobe tends to work hard to fix "broken" PDFs, including those with bad version numbers. – Mark Storer Nov 11 '11 at 17:43
  • I using iTextSharp 5.5.9: stamper.Writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_6); it not working. – D T Jan 25 '19 at 11:10
  • iText has gone through several major version changes since I wrote this answer some 7+ years ago. OTOH, the docs on SetPdfVersion() now read "If the PDF Header hasn't been written yet, this changes the version as it will appear in the PDF Header.". I suspect you've already done something to cause your header to be written. You can fix this by moving your SetPdfVersion so it's the first thing you do with your PdfWriter/PdfStamp once you have it. – Mark Storer Jan 26 '19 at 00:29
0

Use this: writer.PdfVersion = PdfWriter.VERSION_1_3;

This worked for me

stephyness
  • 29
  • 5
-1

Looks like this is no longer valid, well, at least for me it didn't work. However, I found this and it worked for me: http://itext-general.2136553.n4.nabble.com/iTextSharp-PDF-version-td3477631.html.

youngr
  • 1
  • This link is broken. A prime example of why you should never just link the solution, but also write it explicitly into the answer. – Attila Csipak Dec 09 '21 at 14:23