页眉、页脚使用itextsharp显示在最后一页

本文关键字:显示 最后 一页 itextsharp 页眉 | 更新日期: 2023-09-27 18:25:13

我尝试了所有的解决方案,但都没有得到答案,最后我决定发布我自己的问题并得到答案。这只在页面顶部显示标题,而其他页面则为空白。我尝试了以下代码:

public void WriteDocument()
{
    RichTextBox rtbnew = new RichTextBox();
    //rtbnew.Rtf = this.rtb.Rtf;
    //String _rtfTohtml = this.markupConverter.ConvertRtfToHtml(rtbnew.Rtf);
    //MessageBox.Show(_rtfTohtml);
    rtbnew.Text = this.rtb.Text;
    string str = rtbnew.Text;
    TextReader tr = new StringReader(str);
    //Declare a itextSharp document 
    Document document = new Document(PageSize.A4, 16, 16, 16, 16);
    //Create our file stream and bind the writer to the document and the stream 
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@path + "/Doc2.pdf", FileMode.Create));
    events = new PDFGeneration.Events();
    //Open the document for writing 
    document.Open();
    events.OnEndPage(writer, document);
    events.onOpenDocument(writer, document);
    events.setHeader("Urdu Word Processor");
    //Add a new page 
    document.NewPage();
    events.OnStartPage(writer, document);
    //events.onEndPage(writer,document);
    //Reference a Unicode font to be sure that the symbols are present. 
    BaseFont bfArialUniCode = BaseFont.CreateFont(@"fonts''adobe-arabic-regular-1.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    //Create a font from the base font
    iTextSharp.text.Font font = new iTextSharp.text.Font(bfArialUniCode, 16.0f);
    Paragraph p = new Paragraph(tr.ReadToEnd(), new iTextSharp.text.Font(bfArialUniCode, 12.0f));
    //Use a table so that we can set the text direction 
    PdfPTable table = new PdfPTable(1);
    //Ensure that wrapping is on, otherwise Right to Left text will not display 
    table.DefaultCell.NoWrap = false;
    table.SplitRows = true;
    table.SplitLate = true;
    //Create a regex expression to detect hebrew or arabic code points 
    const string regex_match_arabic_hebrew = @"['u0600-'u06FF,'u0590-'u05FF]+";
    if (Regex.IsMatch(rtbnew.Text, regex_match_arabic_hebrew, RegexOptions.IgnoreCase))
    {
        table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
    }
    //Create a cell and add text to it 
    PdfPCell text = new PdfPCell(p);
    //Ensure that wrapping is on, otherwise Right to Left text will not display 
    text.NoWrap = false;
    text.SetLeading(5.0f, 1.0f);// line spacing
    text.Padding = 1.0f;
    text.Border = iTextSharp.text.Rectangle.TOP_BORDER;
    text.UseBorderPadding = true;
    text.BorderWidthTop = 5.0f;
    //text.BorderColorTop = BaseColor.GRAY;
    //text.BackgroundColor = BaseColor.LIGHT_GRAY;
    text.ArabicOptions = ColumnText.DIGITS_EN2AN;
    table.SplitRows = true; // waits for the rows to get fit in a page and as it exceeds page height, forward text to next page.
                            //Add the cell to the table 
    table.AddCell(text);
    //Add the table to the document 
    document.Add(table);
    //Close the document 
    events.onCloseDocument(writer, document);
    document.Close();
    System.Diagnostics.Process.Start(@path + "/Doc2.pdf");
    //Launch the document if you have a file association set for PDF's 
}

对于我使用的事件:

public override void OnStartPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
    // TopNosh Header logo:
    PdfPTable tableTitle = new PdfPTable(1);
    string path = @"C:'Users'M.Shahid.Sultan'Pictures";
    iTextSharp.text.Image imgLogo = iTextSharp.text.Image.GetInstance(path + "//header.png");
    imgLogo.Alignment = 6; // iTextSharp.text.Image.ALIGN_RIGHT;
    imgLogo.ScalePercent(80f); // change it's size
    Chunk chnk = new Chunk(imgLogo, 0, -10);
    PdfPCell imgTag = new PdfPCell(new Phrase(chnk));
    imgTag.Colspan = 2;
    imgTag.Border = 0;
    imgTag.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
    tableTitle.AddCell(imgTag);
    document.Add(tableTitle);
}
public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
    PdfPTable tablefooter = new PdfPTable(1);
    tablefooter.HorizontalAlignment = 1;
    float[] widthfooter = new float[] { 1f };
    tablefooter.SetWidths(widthfooter);
    tablefooter.SpacingBefore = 100f;
    PdfPCell headerfooter = new PdfPCell(new Phrase("© Copyright 2010                            " + "Date: " + DateTime.Now.ToString("dd-MMM-yyyy") + "                            " + "Page: " + writer.PageNumber, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));
    headerfooter.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
    headerfooter.Border = iTextSharp.text.Rectangle.TOP_BORDER;
    tablefooter.AddCell(headerfooter);
    document.Add(tablefooter);
}

现在,这些代码片段只在第一页上显示标题,而在其他页上不显示。感谢您的帮助。

页眉、页脚使用itextsharp显示在最后一页

(在评论和编辑中回答。请参阅没有答案的问题,但在评论中解决了问题(或在聊天中扩展)

OP写道:

实际上,我对代码做了一些更改,最后在第一页得到了一个标题。虽然我试过你从书中指出的代码,但没有得到更好的结果。现在的情况是,我只有一张桌子,我正在往里面扔东西。所以,当文本的大小变大时,很可能会将表拆分为多个页面,我认为它在第一页显示标题,因为我只有一个表?

这个链接帮了我很多:链接

我把我的代码改成了:

PdfPTable footerTbl = new PdfPTable(1);
        //set the width of the table to be the same as the document
        footerTbl.TotalWidth = document.PageSize.Width;
        //Center the table on the page
        footerTbl.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
        //Create a paragraph that contains the footer text
        Paragraph para = new Paragraph(String.Format("Urdu Word Processor                 Date : {0:dd/MMM/yyyy}                  Page Number : {1}", DateTime.Now, writer.PageNumber), footer);
        //add a carriage return
        //para.Add(Environment.NewLine);
        //para.Add("Some more footer text");
        //create a cell instance to hold the text
        PdfPCell cell = new PdfPCell(para);
        //set cell border to 0
        cell.Border = 0;
        //add some padding to bring away from the edge
        cell.PaddingLeft = 120.0f;
        //add cell to table
        footerTbl.AddCell(cell);
        //create new instance of Paragraph for 2nd cell text
        //para = new Paragraph("Some text for the second cell", footer);
        //create new instance of cell to hold the text
        //cell = new PdfPCell(para);
        //align the text to the right of the cell
        //cell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
        //set border to 0
        //cell.Border = 0;
        // add some padding to take away from the edge of the page
        //cell.PaddingRight = 5;
        //add the cell to the table
        //footerTbl.AddCell(cell);
        //write the rows out to the PDF output stream.
        footerTbl.WriteSelectedRows(0, -1, 0, (document.BottomMargin + 790), writer.DirectContent);

并编辑了我的主要课程:

Document document = new Document(PageSize.A4);
        //Create our file stream and bind the writer to the document and the stream 
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@path + "/Doc2.pdf", FileMode.Create));
        **events = new PDFGeneration.Events();
        writer.PageEvent = new PDFGeneration.Events();**
        //Open the document for writing 
        document.Open();

终于解决了谢谢大家!