使用选择PDF进行HTML到PDF转换时的页面控制

时间:2016-08-09 02:26:13

标签: vb.net selectpdf

我正在尝试将大型HTML文件转换为PDF。但只想设置第一页和后面的页码。

我使用了以下代码

        converter = New HtmlToPdf()
        Dim file As String = "C:\TEMP\Document5.pdf"

        converter.Options.PdfPageSize = PdfPageSize.A4
        converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait
        converter.Options.MarginTop = 20
        converter.Options.MarginBottom = 20
        converter.Options.MarginLeft = 10
        converter.Options.MarginRight = 10
        converter.Options.DisplayFooter = True

        Dim doc As PdfDocument = converter.ConvertHtmlString(htmlString)

        converter.Footer.TotalPagesOffset =2  
        converter.Footer.FirstPageNumber = 2

         doc.Save(file)

            ' close pdf document
         doc.Close()

但这部分不起作用,

        converter.Footer.TotalPagesOffset =2  
        converter.Footer.FirstPageNumber = 2 

并且有什么知道总页数吗?

1 个答案:

答案 0 :(得分:1)

以下是使用SelectPDF和ASP.NET MVC Razor处理页码的方法。

for (int x = 0; x < PDF.Pages.Count; x++) {
    if (x > 0 && x != PDF.Pages.Count - 1) { // will not number first/last page
        PdfPage page = PDF.Pages[x];
        PdfTemplate customFooter = PDF.AddTemplate(page.PageSize.Width, 33f);
        page.DisplayFooter = true;
        PdfHtmlElement customHtml = new PdfHtmlElement(domain + "/template/_pagenumber?pageNum=" + x.ToString() + "&totalPages=" + PDF.Pages.Count.ToString());
        customFooter.Add(customHtml);
        page.CustomFooter = customFooter;
    }
}

这就是我的_pagenumber.cshtml文件的样子......

<div style="margin-right:48px;margin-left:48px;height:46px;position:relative;top:-4px;z-index:999;">
    <div class="row">
        <div class="col-xs-6">
            <small>Company info goes here</small>
        </div>
        <div class="col-xs-6 text-right">
            <small><strong>Page @(Request.QueryString["pageNum"]) of @(Request.QueryString["totalPages"])</strong></small>
        </div>
    </div>
</div>