无法显示PDF标头Itextsharp [英] PDF header doesnt get displayed 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内容,但不生成标题文本..??
版本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;
Hi I am writng some pdf by parsing html to it using itextsharp and it is working fine but I want to add page number on each every page of the pdf. for this I am adding header with dummy text in it(later I will replace it with page count) and writing dome content but the content is not displayed ..
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();
}
where I am worng. this code generate all the html content but not header text..??
Version 5+ you have to do this with a page event:
Handling header footer page event, itextsharp 5+
Before version 5 it works like:
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;
这篇关于无法显示PDF标头Itextsharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!