c# – 将页码添加到pdf文档(itextsharp)
发布时间:2020-12-16 00:03:37 所属栏目:百科 来源:网络整理
导读:我想将页码添加到itextsharp pdf文件的页脚.我从html(asp.net repeater)生成pdf.我使用 XMLWorkerHelper来解析html内容.我搜索了很多但是找不到任何有用的东西来实现这一点. 解决方法 您必须使用iTextSharp打开PDF并自行添加页码.我做了类似的事情,这是我的
我想将页码添加到itextsharp pdf文件的页脚.我从html(asp.net repeater)生成pdf.我使用
XMLWorkerHelper来解析html内容.我搜索了很多但是找不到任何有用的东西来实现这一点.
解决方法
您必须使用iTextSharp打开PDF并自行添加页码.我做了类似的事情,这是我的功能,可能会给你一个开始.
该功能将当前页面添加到左下方,因此您可能必须将其放置在符合您需要的其他位置. public static byte[] AddPageNumbers(byte[] pdf) { MemoryStream ms = new MemoryStream(); // we create a reader for a certain document PdfReader reader = new PdfReader(pdf); // we retrieve the total number of pages int n = reader.NumberOfPages; // we retrieve the size of the first page Rectangle psize = reader.GetPageSize(1); // step 1: creation of a document-object Document document = new Document(psize,50,50); // step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.GetInstance(document,ms); // step 3: we open the document document.Open(); // step 4: we add content PdfContentByte cb = writer.DirectContent; int p = 0; Console.WriteLine("There are " + n + " pages in the document."); for (int page = 1; page <= reader.NumberOfPages; page++) { document.NewPage(); p++; PdfImportedPage importedPage = writer.GetImportedPage(reader,page); cb.AddTemplate(importedPage,0); BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA,BaseFont.CP1252,BaseFont.NOT_EMBEDDED); cb.BeginText(); cb.SetFontAndSize(bf,10); cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT,+p + "/" + n,7,44,0); cb.EndText(); } // step 5: we close the document document.Close(); return ms.ToArray(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |