17

I'm using iTextSharp to update A PDF's file properties:

FileStream fs = File.Open(@"C:\Developer\C#Projects\BylawSearch\0001.pdf", FileMode.Open);
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
document.AddSubject("Blah");
document.AddTitle("Blah blah");
document.AddKeywords("Blah blah blah");
document.Close();

I'm getting a "The document has no pages." error from iTextSharp. Any help appreciated.

Kurubaran
  • 8,696
  • 5
  • 43
  • 65
witchlightning
  • 271
  • 1
  • 2
  • 10
  • Have you tried to add some pages? At least empty one, just for the test. – alex.b Oct 09 '13 at 17:24
  • 1
    Try to add paragraph to document: `document.Add(new Paragraph("Hello World"));` – Alezis Oct 09 '13 at 17:28
  • See this (http://stackoverflow.com/a/322565/231316) and this (http://itextpdf.com/examples/iia.php?id=216) for modifying an existing document. – Chris Haas Oct 10 '13 at 13:08
  • I've hit the same issue. I know why it is, the document when rendered would be empty. Which is a valid case. There does not actually seem to be a way to handle this situation gracefully that i can see. The PageCount is set only on the generated document. It's a valid situation because the HTML has header, etc, just no "rendered content" – Tom John May 03 '18 at 08:40

3 Answers3

12

You haven't added any information to put on a page ... !!

document.Add(new Paragraph("Hello World!"));

... for example.

Your title etc are part of the document properties (rather than something that's "printed" to the pdf).

Check out this introductory example, that seems to cover what you're after.

noelicus
  • 14,468
  • 3
  • 92
  • 111
  • 1
    Thanks. That does edit the document's properties. The only problem is that it also overwrites the existing document with "Hello World!" completely erasing the document's content. That is not so good. How can I use iTextSharp to update the document's properties only? – witchlightning Oct 09 '13 at 17:56
  • No problem. Check the new link I've added - it shows to do the document properties first, then open it to write to. – noelicus Oct 09 '13 at 18:14
  • Thanks. The problem I have with his solution is that document.Add(new Paragraph("Hello World!")); overwrites content in an existing pdf. I need to edit the metadata only. I'm thinking PdfStamper is the way to go... – witchlightning Oct 09 '13 at 18:35
  • It does not solved my problem, but let me display accurate error in my document instead of "The document has no pages" error, so +1 . – VISHMAY Nov 17 '16 at 05:43
  • I had this same error despite adding a table to the document, then adding cells and content. I needed to add the cells and content before adding the table to the document. The size of an element seems to be set at add time, not render time. As such, it was zero with no cells and content, indicating no elements, indicating no page. The same logic would apply for other container elements. Populate prior to adding to document. – Andrew Taylor Mar 01 '18 at 23:14
  • What if the document generally has no content other than head and html so nothing is rendered. The same error occurs - there does not seem to be a way to gracefully handle this. – Tom John May 03 '18 at 08:42
  • I don't know, but creating a document with no pages sounds like a legitimate reason to throw an exception to me (arguably). Creating a single blank page would gracefully handle it. Subclass it and handle it how you want to? – noelicus May 04 '18 at 15:38
1

I had the same issue with Xamarin, .NET. For me the error message was misleading, because it happened when i tried to create fonts from local files.

Project settings > Android Options > Additional supported encodings. Set this to West and it solved my problem.

Gozo
  • 141
  • 2
  • 7
0

In my case, I had added a paragraph, but specified a font that was null.

document.Add(new Paragraph("Hello World!", nullFont));

Either make sure the font is valid, or else don't use the Paragraph constructor with the Font argument.

(This does not apply to the poster's scenario, but may be helpful to someone else.)

llessurt
  • 555
  • 3
  • 14