PDF标题不显示Itextsharp
本文关键字:Itextsharp 显示 标题 PDF | 更新日期: 2023-09-27 18:15:28
嗨,我正在通过使用itextsharp解析html来编写一些pdf,它工作得很好,但我想在pdf的每一页上添加页码。为此,我在其中添加了带有虚拟文本的标题(稍后我将用页面计数替换它)并编写圆顶内容,但内容不显示。
try
{
Document oNewDocument = new Document(PageSize.A4, 20f, 20f, 30f, 10f);
PdfWriter.GetInstance(oNewDocument, new FileStream(pdfpath + "/" + sSaleInvoicePdf, FileMode.Create));
string content = "Some HTML Content";
List<IElement> parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), styles);
oNewDocument.AddHeader("text","text");
foreach (var htmlElement in parsedHtmlElements)
{
oNewDocument.Add(htmlElement as IElement);
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
oNewDocument.Close();
}
我错了。这段代码生成所有的HTML内容,但不是标题文本…??
版本5+你必须用一个页面事件来做这件事:
处理页眉页脚事件,itextsharp 5+
在版本5之前,它的工作方式如下:
Document oNewDocument = new Document(PageSize.A4, 20f, 20f, 30f, 10f);
PdfWriter.GetInstance(oNewDocument, new FileStream(pdfpath + "/" + sSaleInvoicePdf, FileMode.Create));
//Create some text to add to the header
Chunk text= new Chunk("my text");
Phrase phHeader = new Phrase();
phHeader.Add(text);
//Assign the Phrase to PDF Header
HeaderFooter header = new HeaderFooter(phHeader, false);
//Add the header to the document
oNewDocument.Header = header;
HeaderFooter hdr = new HeaderFooter(stringvalue, false);
hdr.Border = Rectangle.NO_BORDER;
hdr.Alignment = Element.ALIGN_LEFT;
doc.Header = hdr;