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

时间:2013-07-27 13:05:09

标签: c# .net header itext footer

我已经尝试了所有解决方案并且没有得到答案。最后,这导致我决定发布我自己的问题并得到答案。这显示了页面顶部的标题并离开了其他页面空白。我尝试了以下代码:

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);
}

现在,此代码段仅在第一页上显示标题,而在其他页面上不显示。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

(在评论和修改中回答。见Question with no answers, but issue solved in the comments (or extended in chat)

OP写道:

  

事实上,我已经改变了我的代码,最后在第一页得到了一个标题。虽然我尝试过你从书中指出的代码并没有得到更好的结果。现在的情况是,我有单桌,我扔内容。因此,当文本变得更大时,很可能,表被分成多个页面,我认为它在第一页显示标题,因为我只有一个表?

     

此链接帮助了我很多:link

     

我已将代码更改为:

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();
  

最后已解决!谢谢大家!