本文介绍了我想在export pdf中每页添加页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 < pre> public byte [] GetPDF( string pHTML ) { byte [] bPDF = null ; MemoryStream ms = new MemoryStream(); TextReader txtReader = new StringReader(pHTML); // 我们检索总页数 // 1:创建itextsharp文档类的对象 文档doc = new 文档(PageSize.LEGAL_LANDSCAPE, 10 , 10 , 42 , 35 ); doc.SetPageSize( new Rectangle(850f,1100f)); // 2:我们创建一个itextsharp pdfwriter,它监听文档并将XML流引导到file PdfWriter oPdfWriter = PdfWriter.GetInstance(doc,ms); // 3:我们创建一个工作人员解析文档 HTMLWorker htmlWorker = new HTMLWorker(doc); // 4:我们打开文档并在文档上启动worker doc.Open(); doc.NewPage(); // htmlWorker.StartDocument(); /// / 5:将html解析为文档 // htmlWorker.Parse(txtReader); List< IElement> elements = HTMLWorker.ParseToList(txtReader, null ); foreach (IElement el in elements) { // 如果元素是一个表,则手动设置其标题行数 if (el PdfPTable) {((PdfPTable)el).HeaderRows = 2 ; } doc.Add(el); } // 6:关闭文件和worker htmlWorker.EndDocument(); htmlWorker.Close(); doc.Close(); bPDF = ms.ToArray(); return bPDF; } 我的尝试: 我想在export pdf中每页添加页码 解决方案 将页面事件添加到PdfWriter(iText 5) [ ^ ] <pre> public byte[] GetPDF(string pHTML) { byte[] bPDF = null; MemoryStream ms = new MemoryStream(); TextReader txtReader = new StringReader(pHTML); // we retrieve the total number of pages // 1: create object of a itextsharp document class Document doc = new Document(PageSize.LEGAL_LANDSCAPE, 10, 10, 42, 35); doc.SetPageSize(new Rectangle(850f, 1100f)); // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms); // 3: we create a worker parse the document HTMLWorker htmlWorker = new HTMLWorker(doc); // 4: we open document and start the worker on the document doc.Open(); doc.NewPage(); //htmlWorker.StartDocument(); //// 5: parse the html into the document //htmlWorker.Parse(txtReader); List<IElement> elements = HTMLWorker.ParseToList(txtReader, null); foreach (IElement el in elements) { //If the element is a table manually set its header row count if (el is PdfPTable) { ((PdfPTable)el).HeaderRows = 2; } doc.Add(el); } // 6: close the document and the worker htmlWorker.EndDocument(); htmlWorker.Close(); doc.Close(); bPDF = ms.ToArray(); return bPDF; }What I have tried:i want add page number every page in export pdf 解决方案 Adding page events to PdfWriter (iText 5)[^] 这篇关于我想在export pdf中每页添加页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-10 04:36