1
votes

I'm generating a PDF document using iTextSharp version 5.5.7, using their "streaming" mode - by which I mean I'm not specifying the location of every piece of text, I'm just adding Paragraphs to the Document and letting iTextSharp figure out where to draw them. The text I'm outputting is the result of a report generator, so it is different every time.

The problem I'm running into is this: imagine that, given the page size and the selected font, I can fit 40 lines of text on a page. I output 40 Paragraphs, then I output a blank Paragraph (contents=" "), then an image that fills an entire page. iTextSharp does exactly what I tell it - I end up with one page full of text, a blank page, and then a page containing my image.

But now my document looks funny - there is this unexpectedly blank page in the middle of everything.

I can't just say "don't output any blank lines" because of course that blank line might show up after only 20 lines of text, in which case it has to be there. I need some way to either tell iTextSharp "include this paragraph only if it's not the only thing on a page" or else somehow detect that the page is blank in OnEndPage() and suppress its output (without screwing up my page numbers).

Any suggestions on how I can do this?

ADDED LATER

Output from the report generator:

<html>
<p>Information header</p> 
<p>Detail</p>
<p>Detail</p>
<p>Detail</p>
<p></p>  <!-- Blank line inserted by report generator for clarity -->
<p>Information header</p>
<p>Detail</p>
<p>Detail</p>
<p>Detail</p>
...
<p>Detail</p>  <!-- just by random happenstance this is the last line that will fit on the first page -->
<p></p>   <!-- This line happens to be blank, I have no control over it -->
<img src="blah blah"></image>

My (pseudo) code:

foreach (HtmlNode node in htmlFromReportGenerator)
{
   if (node is text)
      pdfDoc.Add(new Paragraph(node.text));
   else if (node is image)
      pdfDoc.Add(new Image(node.image));
}
1
Why are you using a dirty trick to insert a blank page? Dirty tricks have dirty side-effects. See stackoverflow.com/a/11185274/1622493 to find out how to add a blank page. You shouldn't expect another answer from the people on Stack Overflow because you didn't provide any code that allows us to reproduce your problem. - Bruno Lowagie
I've reread your question (it's phrased in a very confusion way) and maybe you don't want to add a blank page between the 40 paragraphs and the image. Maybe you're using the dirty trick of adding a blank paragraph because you want to introduce some space between the text and the image. In any case, a question with that much text and no sample code, is not a good Stack Overflow question. - Bruno Lowagie
Anyway: iText ignores document.newPage() if a page is blank. You have to do a special effort if you want to introduce a blank page. The statement there is this unexpectedly blank page in the middle of everything isn't clear. You should really do some more effort to explain the problem. - Bruno Lowagie
@BrunoLowagie hopefully the pseudo-code will add some clarity. Basically, I'm receiving some information which is a mixture of text and images. I am taking that information and generating a PDF document from it. I'm not calling document.newPage(), I'm not trying to insert a blank page at all. It's just accidental that in this particular situation, the blank line happened to fall at the start of a new page, followed by a large image. - Betty Crokker
Add another if that checks for blank lines. Suppress them and make sure you define a setSpacingBefore for the next Paragraph. - Bruno Lowagie

1 Answers

1
votes

Following Bruno's suggestion, my (pseudo)code now looks like this:

Paragraph lastParagraph = null;
foreach (HtmlNode node in htmlFromReportGenerator)
{
   if (node is text)
   {
      Paragraph parg = new Paragraph(node.text);
      if ( (lastParagraph != null) && (text.Trim().Length == 0) )
         lastParagraph.SpacingAfter += parg.Leading;
      else
      {
         pdfDoc.Add(parg);
         lastParagraph = parg;
      }
   }
   else if (node is image)
   {
      pdfDoc.Add(new Image(node.image));
      lastParagraph = null;
   }
}