在版本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 headerChunk text= new Chunk("my text");Phrase phHeader = new Phrase();phHeader.Add(text);//Assign the Phrase to PDF HeaderHeaderFooter header = new HeaderFooter(phHeader, false);//Add the header to the documentoNewDocument.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 headerChunk text= new Chunk("my text");Phrase phHeader = new Phrase();phHeader.Add(text);//Assign the Phrase to PDF HeaderHeaderFooter header = new HeaderFooter(phHeader, false);//Add the header to the documentoNewDocument.Header = header; 这篇关于无法显示PDF标头Itextsharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!