如何在Aspose.Html中创建分页符并重复标题

时间:2018-05-09 11:07:31

标签: c# html css3 pdf aspose

我正在使用ASPOSE.HTML从HTML创建PDF。我正在关注此示例HTML to PDF。我浏览了文档,我无法找到关于如何检测分页符的示例,在PDF&中给出有意的分页符。重复标题。这是代码 -

using System.IO;
using System;

namespace Aspose.Html.Examples.CSharp.Conversion
{
public class HtmlToPdf
{
    public static void Run()
    {
        // ExStart:HtmlToPdf
        // The path to the documents directory.
        string dataDir = RunExamples.GetDataDir_Data();

        String InputHtml = dataDir + "input.html";
        using (FileStream fs = File.Create(InputHtml))
        using (StreamWriter sw = new StreamWriter(fs))
        {
            sw.Write(
                @"
        HTML PART is very long .so it is shared in jsfiddle.
    ");
        }

        // File name for resultant PDF file
        string Resultant_output = dataDir + "simple-any-page_out.pdf";
        // Create PdfRendering Options object
        Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new 
 Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
        // The PageSetup also provides different properties i.e. FirstPage, 
 LastPage, LeftPage, RightPage and they are used to setup (PageSize, Margin) 
  for every page. 
        // In most cases, usage of setup any page is enough, but in some 
    complicated cases, you may need to fine tune page settings. It can be 
  done either by CSS styles or by using rendering options.
        // the size for drawing is in pixels
       // pdf_options.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(816, 1344));
        // Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
        using (Aspose.Html.Rendering.Pdf.PdfDevice pdf_device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, Resultant_output))
        // Create HtmlRenderer object
        using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
        // Create HtmlDocument instance while passing path of already created HTML file
        using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(InputHtml))
        {
            // Render the output using HtmlRenderer
            renderer.Render(pdf_device, html_document);
        }
        // ExEnd:HtmlToPdf           
    }
}
}

这是HTML&的jsfiddle链接 - HTML/CSS。 CSS。

这是PDF图片 -  PDF

  • 如何通过计算现有的HTML部分在PDF中添加分页符,以便我可以添加有意的分页符。
  • 如何检测PDF中的分页符。
  • 如果内容进入下一页,如何在下一页重复HEADER部分。

正如我们在图片中看到的那样,HEADER 2位于第一页,但内容位于第二页。

1 个答案:

答案 0 :(得分:0)

我们想更新您无法直接计算现有HTML部分的大小,并且API不提供在渲染期间添加分页符的功能,因为这会导致完整的布局重新计算。但是,您可以使用CSS规则添加强制分页符:“page-break-before:always; page-break-after:always;”。要在第二个标题之前接收分页符,您应该将样式属性添加到下一个元素,如下所示:

<p style="page-break-before: always;">HEADER 2</p>

关于在PDF文档中检测分页符,如果您创建从PdfDevice派生的设备,则可以捕获分页符,如下所示:

class MyDevice : PdfDevice
{
    public MyDevice(PdfRenderingOptions options, string file) 
        : base(options, file)
    {
    }

    public override void BeginPage(SizeF size)
    {
        //This method is called at the beginning of new page.
        base.BeginPage(size);
    }
}

要在表格内容之前在每个页面上重复使用HEADER内容,应将其放在thead元素内:

<table>
<thead>
<tr>
<td>
This content is repeated.
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
Table content.
</td>
</tr>
</tbody>
</table>

但是只有当用户从style.css中删除“.pdf-body .chart-container .charts {display:inline-block;}”时它才会起作用,因为表格不支持在这些块内重复的标题。此行为在HTML文档中描述并在Firefox浏览器中实现。此外,无法同时将一个标题附加到三个不同的表。