0

I am creating a pdf with iTextSharp c# and am having a problem when a piece of text runs over to a new page. The following works fine until, for example section 2, runs over to the next page.

private Font _smallFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
private Font _smallBoldFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK);

AddParagraph(doc, Element.ALIGN_LEFT, _smallBoldFont, new Paragraph("\nSection 1\n"));
AddParagraph(doc, Element.ALIGN_LEFT, _smallFont, new Paragraph(textBox2.Text));
AddParagraph(doc, Element.ALIGN_LEFT, _smallBoldFont, new Paragraph("\nSection 2\n"));
AddParagraph(doc, Element.ALIGN_LEFT, _smallFont, new Paragraph(textBox3.Text));

In the over-ridden base.OnStartPage I add a logo and a header text.

Image logoImage = Image.GetInstance(appPath + "\\logo.jpg");
logoImage.ScaleAbsolute(newWidth: 141f, newHeight: 65f);
logoImage.Alignment = Element.ALIGN_CENTER;
document.Add(logoImage);

var headerText = "\nReport Header  " + "\n";
Paragraph subText = new Paragraph(headerText, new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
document.Add(subText);

cb.AddTemplate(templateH, pageSize.GetLeft(document.LeftMargin), pageSize.GetTop(document.TopMargin - 5));

This works except for the fact that the line spacing is increased to about 1.5 (default?) until a new AddParagraph. This happens even if the header text is the same font and size. I have an extremely dirty workaround where I add a second line to the header with a font size of Helvetica 8. This ensures the same leading as the previous page. Why 8 I don’t know as the sections are Helvetica 10. I have tried setting the leading in the AddParagraph but this doesn't help. What am I doing wrong?

Gerry
  • 129
  • 2
  • 13
  • have you tried playing with a different font also column widths may need decresing along with adjusting to a smaller font size.. I had similar problem in the past – MethodMan Nov 27 '17 at 14:56
  • Yep, tried different fonts:-( I am not using a table, just outputting paragraphs. – Gerry Nov 27 '17 at 15:45

1 Answers1

0

As documented in different places, it is forbidden to add content in the OnStartPage method. You should add the logo and the header text in the OnEndPage() method. See for instance How to add multiple headers and footers in pdf using itext

Also note that the Document object you are using in document.Add(subText); is not the same object as the Document object you are using in the rest of your code. It is forbidden to use the Add() method in a page event. That is also explained in the documentation. See also "'System.StackOverflowException" in "OnEndPage" event handler

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Ok, I have only just started using iTextSharp but this doesn't seem logical - adding a header in the OnEndPage event. I will definitely modify my code according to the guidelines, but for the moment I got it working by forcing the leading (12) in the header text. Paragraph subText = new Paragraph(12, headerText, new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, iTextSharp.text.Font.BOLD, BaseColor.BLACK)); Thanks. – Gerry Nov 27 '17 at 18:59
  • If you are just starting with iTextSharp, then *why are you using an outdated version*? See my answer to [this question](https://stackoverflow.com/a/47517568/1622493): *I know this is odd, but then again, you are using the old iText version. If you want a more consistent API, please use iText 7. That upgrade is really significant. We've spent an awful amount of time and money on improving iText, so it's kind of frustrating to see that new iText developers choose to work with iText 5 rather than with iText 7.* – Bruno Lowagie Nov 27 '17 at 19:11
  • Read the [iText 7 Jump-Start tutorial](https://developers.itextpdf.com/content/itext-7-jump-start-tutorial-net/chapter-3-using-renderers-and-event-handlers) to learn how headers and footers are added in iText 7. You also have to think about the people who will have to maintain your code. They will really hate your current "fix". – Bruno Lowagie Nov 27 '17 at 19:14
  • Thank you very much. I did not know about iText 7. I will definitely check this out. BTW as this is for hobby (I'm a pensioner and programming to keep the little grey cell working) no one else will be maintaining it :-) – Gerry Nov 27 '17 at 21:49
  • If you want to make money with your hobby: we are looking for people with great ideas who turn their idea into an add-on. If the add-on is marketable, we publish it on the iText 7 platform, and we take care of world-wide sales and marketing. The revenue that is made with the add-on is shared with the original developer(s). – Bruno Lowagie Nov 27 '17 at 21:51
  • At the moment it is not about money, but I have noted your profile anyway. You never know. – Gerry Nov 27 '17 at 22:27