简体   繁体   English

使用iTextSharp在多个pdf页面上构造标题

[英]Using iTextSharp to construct header on multiple pdf pages

I'm generating a PDF file based on a record selected in my datagridview. 我正在根据在datagridview中选择的记录生成PDF文件。 It will consist of 3-5 pages. 它将包括3-5页。 I created a table with 2 columns to represent my header. 我创建了一个包含2列的表格来代表我的标题。 the first cell is left aligned and the 2nd cell is right aligned. 第一个单元格左对齐,第二个单元格右对齐。 I want this same inforamtion displayed on all pages. 我想在所有页面上显示相同的信息。

After doing some googling, I saw a header.WriteSelectedRows() property which is supposed to help with that? 进行一些谷歌搜索后,我看到了header.WriteSelectedRows()属性,该属性应该对此有所帮助? One example was : 一个例子是:

header.WriteSelectedRows(0, -1, doc.PageSize.GetLeft(5), doc.PageSize.GetTop(5), wri.DirectContent);

2nd was: 第二个是:

 header.WriteSelectedRows(0, -1, doc.LeftMargin, doc.PageSize.Height - 36, wri.DirectContent);

However both resulted in just the first page having the table/header. 但是,两者都导致仅第一页具有表/标题。 Any ideas on what I need to fix? 关于我需要解决的任何想法? Thanks! 谢谢!

Code: 码:

 PdfPTable header = new PdfPTable(2);
 header.HorizontalAlignment = Element.ALIGN_LEFT;
 header.TotalWidth = doc.PageSize.Width - 20f;
 header.LockedWidth = true;
 Phrase cell1 = new Phrase(signal.ProformaType);
 Phrase cell2 = new Phrase("text" + Environment.NewLine + "text"
     + Environment.NewLine + signal.Signal);

 PdfPCell c1 = new PdfPCell(cell1);
 c1.Border = iTextSharp.text.Rectangle.NO_BORDER;
 c1.VerticalAlignment = iTextSharp.text.Element.ALIGN_TOP;
 c1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
 header.AddCell(c1);

 PdfPCell c2 = new PdfPCell(cell2);
 c2.Border = iTextSharp.text.Rectangle.NO_BORDER;
 c2.VerticalAlignment = iTextSharp.text.Element.ALIGN_TOP;
 c2.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;             
 header.AddCell(c2);
 header.WriteSelectedRows(0, -1, doc.LeftMargin, doc.PageSize.Height - 36, wri.DirectContent);

The PdfPTable is added to the first page only because you are adding it to the first page only. 仅将PdfPTable添加到首页,是因为将其添加到首页。 If you want to add it to every page that is created by iText, you shouldn't add the PdfPTable where you are adding it now. 如果要将其添加到iText创建的每个页面中,则不应在现在添加PdfPTable位置添加它。

Instead you should add it in the OnEndPage() method of a page event. 相反,您应该将其添加到页面事件的OnEndPage()方法中。 This is explained in answers to questions such as: 在回答以下问题时对此进行了解释:

In other words, you need to create your own implementation of the PdfPageEvent interface. 换句话说,您需要创建自己的PdfPageEvent接口实现。 The best way is to extend the PdfPageEventHelper class: 最好的方法是扩展PdfPageEventHelper类:

public class MyPageHeader : PdfPageEventHelper
{

    PdfPTable header = ... // define header table here        

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        header.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
    }
}

To make this work, you need to declare this page event before opening the Document : 为此,您需要在打开Document之前声明此page事件:

PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfFileStream);
pdfWriter.PageEvent = new MyPageHeader();
document.Open();  

Now, every time a new page is created, the header will be added automatically. 现在,每次创建新页面时,标题都会自动添加。

You may want to adapt document.Left and document.Top in the code above, because right now, it will add the table in the upper-right corner of each page, you may want to use document.Left + 36 and document.Top - 5 or something like that. 您可能想要调整上面代码中的document.Leftdocument.Top ,因为现在,它将在每个页面的右上角添加表格,因此您可能想使用document.Left + 36document.Top - 5或类似的东西。

Also: make sure that there is sufficient room for the header, otherwise your header will overlap with the content you are adding straight to the Document using document.Add() . 另外:确保标题有足够的空间,否则标题将与您使用document.Add()直接添加到Document的内容重叠。 You can change the margins in the constructor of the Document class. 您可以在Document类的构造函数中更改边距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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