我已经尝试了所有解决方案,但没有找到答案,最后这让我决定发布我自己的问题并获得答案。好吧,这仅在页面顶部显示标题,其他页面为空白. 我尝试了以下代码:
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);
}
现在,此代码片段仅在第一页上显示标题,而不在其他页面上显示。任何帮助表示赞赏。