PDF标题没有显示Itextsharp

时间:2011-11-21 09:05:23

标签: c# pdf itextsharp

您好我通过使用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内容但不生成标题文本.. ??

2 个答案:

答案 0 :(得分:1)

版本5+您必须使用页面事件执行此操作:

Handling header footer page event, 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;

答案 1 :(得分:0)

  HeaderFooter hdr = new HeaderFooter(stringvalue, false);
  hdr.Border = Rectangle.NO_BORDER;
  hdr.Alignment = Element.ALIGN_LEFT;
  doc.Header = hdr;

尝试这个不确定你的版本是否支持这个...尝试一下