Extra space inserted on new pages when creating PDFs with iTextSharp
I'开发者_StackOverflow中文版m using iTextSharp 5.0.5 to create dynamic PDFs (asp.net c#), and I'm having problems with an extra space being inserted when creating a new page. The first page looks fine but on any additional pages the extra space appears at the top.
I've made a mini example that inserts a new page with a logo and a title for each article in a list. If I only insert the logo or only the title it works fine, but if I try to insert both there's an extra space coming from somewhere. Anyone got any ideas of what I'm doing wrong?
public static void CreatePDF(List<Item> Articles)
{
Document document = new Document(PageSize.A4, 35, 35, 35, 35);
MemoryStream memoryStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
for (int i = 0; i < Articles.Count; i++)
{
if (i > 0)
{
document.NewPage();
}
Image logo = Image.GetInstance(@"C:\logo.gif");
document.Add(logo);
Paragraph title = new Paragraph("test title", new Font(Font.FontFamily.HELVETICA, 20, Font.NORMAL, CMYKColor.BLACK));
document.Add(title);
}
document.Close();
SendPDFResponse(memoryStream, "myfile.pdf");
}
Thanks,
Annelie
There's no extra space on the pages that follow page 1. The problem is that there's less space on page 1. No, this is not a joke. The problem is caused by the "leading" (that is: the space between two lines). The initial value for the leading is 0. When you add content to a document, this value changes to a value different from 0.
What is the leading you're using throughout the document? You should set this as the initial leading with the method setInitialLeading() in PdfWriter.
That sounds like a bug. I suspect some relatively new aspect of layout isn't being reset properly between pages.
Would you mind cross-posting this to the itext-questions mailing list?
精彩评论