简体   繁体   中英

iTextSharp “The document has no pages.”

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.

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.

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.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM