创建PDF时页眉和页脚被覆盖

时间:2019-04-03 15:52:15

标签: c# itext

我包括了一个用于创建pdf文件的页脚和标题的cs类,所以标题是页脚,而页脚正在覆盖文件中的当前内容,我想要的是显示和移动其他单元格的标题在每个页面中

public  class ITextEvents : PdfPageEventHelper  
    {

        PdfContentByte cb;

        // we will put the final number of pages in a template  
        PdfTemplate headerTemplate, footerTemplate;

        // this is the BaseFont we are going to use for the header / footer  
        BaseFont bf = null;

        // This keeps track of the creation time  
        DateTime PrintTime = DateTime.Now;

        #region Fields
        private string _header;
        #endregion

        #region Properties
        public string Header
        {
            get { return _header; }
            set { _header = value; }
        }
        #endregion

        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            try
            {
                PrintTime = DateTime.Now;
                bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                cb = writer.DirectContent;
                headerTemplate = cb.CreateTemplate(100, 100);
                footerTemplate = cb.CreateTemplate(50, 50);
            }
            catch (DocumentException de)
            {
            }
            catch (System.IO.IOException ioe)
            {
            }
        }

        public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
        {
            base.OnEndPage(writer, document);
            Font georgia = FontFactory.GetFont("georgia", 10f);
            //iTextSharp.text.Font baseFontNormal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
            //iTextSharp.text.Font baseFontBig = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);
            Phrase p1Header = new Phrase("Sample Header Here", georgia);

            //Create PdfTable object  
            PdfPTable pdfTab = new PdfPTable(3);

            iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory + @"Content\Images\ArgoGroup.png");
            PdfPCell cell = new PdfPCell(myImage);
            pdfTab.DefaultCell.Padding = 4;
            cell.Border = PdfPCell.NO_BORDER;


            String text = "Page " + writer.PageNumber + " of ";
            //Add paging to footer  
            {
                cb.BeginText();
                cb.SetFontAndSize(bf, 12);
                cb.SetTextMatrix(document.PageSize.GetRight(180), document.PageSize.GetBottom(30));
                cb.ShowText(text);
                cb.EndText();
                float len = bf.GetWidthPoint(text, 12);
                cb.AddTemplate(footerTemplate, document.PageSize.GetRight(180) + len, document.PageSize.GetBottom(30));
            }
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;

            cell.VerticalAlignment = Element.ALIGN_TOP;

            cell.Border = 0;

            pdfTab.AddCell(cell);

            pdfTab.TotalWidth = document.PageSize.Width - 80f;
            pdfTab.WidthPercentage = 70;
            //pdfTab.HorizontalAlignment = Element.ALIGN_CENTER;      

            //call WriteSelectedRows of PdfTable. This writes rows from PdfWriter in PdfTable  
            //first param is start row. -1 indicates there is no end row and all the rows to be included to write  
            //Third and fourth param is x and y position to start writing  
            pdfTab.WriteSelectedRows(0, -1, 40, document.PageSize.Height - 30, writer.DirectContent);
            //set pdfContent value  
        }

        public override void OnCloseDocument(PdfWriter writer, Document document)
        {
            base.OnCloseDocument(writer, document);

            headerTemplate.BeginText();
            headerTemplate.SetFontAndSize(bf, 12);
            headerTemplate.SetTextMatrix(0, 0);
            headerTemplate.ShowText((writer.PageNumber - 1).ToString());
            headerTemplate.EndText();

            footerTemplate.BeginText();
            footerTemplate.SetFontAndSize(bf, 12);
            footerTemplate.SetTextMatrix(0, 0);
            footerTemplate.ShowText((writer.PageNumber - 1).ToString());
            footerTemplate.EndText();
        }
    }
public void createpdf(){



 PdfWriter writer = PdfWriter.GetInstance(document, ms);

                    writer.PageEvent = new ITextEvents();
                    document.Open();
                    TextReader t = new StringReader(htmlcontent.ToString());
                    HTMLWorker hw = new HTMLWorker(document);
                    hw.Parse(t);
                    document.Close();
                    writer.Close();

}

2 个答案:

答案 0 :(得分:1)

OnEndPage中,您在此处明确设置了页脚的坐标

cb.SetTextMatrix(document.PageSize.GetRight(180), document.PageSize.GetBottom(30));

以及此处的标题

pdfTab.WriteSelectedRows(0, -1, 40, document.PageSize.Height - 30, writer.DirectContent);

如果您不希望页脚或页眉与常规页面内容重叠,只需相应地选择坐标。或者,设置document边距以防止重叠。

  

我想要的是在每个页面中显示和移动其他单元格的标题

这不是itext事件的工作方式。在OnEndPage中,常规页面的内容已经过布局并且位于固定位置,您只需在页面上某处标记诸如页眉和页脚之类的多余内容,因此您必须在尚未使用的位置进行标记。

答案 1 :(得分:0)

您应该添加

cb = writer.DirectContent;

像这样

public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
    base.OnEndPage(writer, document);
    cb = writer.DirectContent;