我正在使用SelectPDF的HtmlToPdf()将html页面转换为pdf。由于html的内容很大,我将它一分为二,并创建2个PDF。
我正在努力编辑页脚中的total_pages以显示实际的总页数,而不仅仅是当前文档;以及编辑page_number以在两个PDF的上下文中显示实际的页码。
如何评估{page_number}和{total_pages}以计算正确的值?我发现的所有示例都使用PdfDocument(),而不是HtmlToPdf()。
Dim converter As New HtmlToPdf()
Dim text As New PdfTextSection(0, 10, "Page: {page_number} of {total_pages} ")
text.HorizontalAlign = PdfTextHorizontalAlign.Center
converter.Footer.Add(text)我对C#和VB都加了标签,因为SelectPDF适用于这两种语言,其中任何一种语言的相关样本都适用于我。谢谢
发布于 2016-05-12 23:12:52
今天,我偶然发现了同样的问题,我找到了解决这个问题的办法。转换器可以显示生成文档的页码,但不能识别多个生成的文件(您不能访问页面属性),所以我连接的所有页面都显示第1页,共1页。
首先,我定义了一个PdfDocument (将其视为主文档),并使用HtmlToPdf将html转换的文件附加到这个主文档。
// Create converter
converter = new HtmlToPdf();
PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages} ", new Font("Arial", 8));
text.HorizontalAlign = PdfTextHorizontalAlign.Right;
converter.Footer.Add(text);
// Create main document
pdfDocument = new PdfDocument();然后我使用这个方法添加页面(从html)
public void AddPage(string htmlPage)
{
PdfDocument doc = converter.ConvertHtmlString(htmlPage);
pdfDocument.Append(doc);
converter.Footer.TotalPagesOffset += doc.Pages.Count;
converter.Footer.FirstPageNumber += doc.Pages.Count;
}这将导致主文档的页码正确。同样的技巧也可以用于在多个文档上拆分文件和页码,如您所描述的。
编辑:如果使用HtmlToPdf转换器看不到任何页码,请不要忘记设置以下属性:
converter.Options.DisplayFooter = true;发布于 2016-04-27 06:29:27
有一个名为itextsharp的开源库,它可以帮助获取总页数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
namespace GetPages_PDF
{
class Program
{
static void Main(string[] args)
{
// Right side of equation is location of YOUR pdf file
string ppath = "C:\\aworking\\Hawkins.pdf";
PdfReader pdfReader = new PdfReader(ppath);
int numberOfPages = pdfReader.NumberOfPages;
Console.WriteLine(numberOfPages);
Console.ReadLine();
}
}
}然后,您还可以在页面上标记文本,但您需要将位置添加到它需要转到的位置。
链接:http://crmhunt.com/how-to-modify-pdf-file-using-itextsharp/
希望这能在某种程度上有所帮助。
发布于 2016-04-30 23:26:52
您应该使用以下属性:
更多详细信息请访问:http://selectpdf.com/html-to-pdf/docs/html/HtmlToPdfHeadersAndFooters.htm
https://stackoverflow.com/questions/36875061
复制相似问题