-1

私はすべての解決策を試しましたが、答えがありませんでした。最終的に、自分の質問を投稿して答えを出すという決定に至りました.まあ、これはページの上部にのみヘッダーを表示し、他のページは空白のままにします. . 次のコードを試しました:

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

現在、このコード スニペットは最初のページにのみヘッダーを表示し、他のページには表示していません。どんな助けでも大歓迎です。

4

1 に答える 1

0

(コメントと編集で回答済み。回答のない質問を参照してください。ただし、コメントで問題は解決されています (またはチャットで拡張されています) )

OP は次のように書いています。

実際、コードを少し変更し、最終的に最初のページに単一のヘッダーを取得しました。あなたが本から指摘したコードを試してみましたが、より良い結果は得られませんでした。今の状況は、コンテンツを投げている単一のテーブルがあります。したがって、テキストのサイズが大きくなると、おそらくテーブルが複数のページに分割されますが、テーブルが1つしかないため、最初のページにヘッダーが表示されていると思いますか?

このリンクは私を大いに助けました:リンク

コードを次のように少し変更しました。

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

ついに解決!みんなありがとう!

于 2015-01-26T15:22:15.503 に答える