如何在aspose.html生成的pdf中添加页码

时间:2018-05-21 12:54:32

标签: c# css aspose

我正在使用ASPOSE.HTML从HTML创建PDF。 我能够重复标题,但无法重复页脚,我们如何在其中添加页码。我正在关注此示例HTML to PDF。 HTML代码段如下



<!DOCTYPE html>
<html lang='en'>

<head>
  <meta charset='UTF-8'>
  <meta name='viewport' content='width=device-width, initial-scale=1.0'>
  <meta http-equiv='X-UA-Compatible' content='ie=edge'>
  <title>Document</title>
  <style>
    @import url('https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900');
    body {
      margin: 0;
      margin: 0 auto;
      -webkit-margin-before: 0;
      -webkit-margin-after: 0;
      -webkit-margin-start: 0;
      -webkit-margin-end: 0;
      -webkit-padding-start: 0;
      font-family: 'roboto' !important;
      width: 816px;
    }
    
    .top-abc_abc-td {
      vertical-align: top;
    }
    
    .abc_abc1-img {
      background-color: #123664;
      height: 105px;
      margin: 8px 8px 8px 0px;
    }
    
    .col-1 {
      width: 286px;
      padding: 6px;
    }
    
    .col-2 {
      width: 202px;
      padding: 6px;
    }
    
    .col-div {
      margin-bottom: 5px;
    }
    
    .sub-col-1 {
      width: 100px;
    }
    
    .sub-col-2 {
      width: 145px;
    }
    
    .col-3 {
      width: 286px;
      padding: 6px;
    }
    
    .col-border-right {
      border-right: 3px solid #749db6;
    }
    
    .templateabc_abc {
      background-color: #edece8;
    }
    
    p.templateabc_abcText {
      font-weight: bold;
      margin-top: 0px;
      margin-bottom: 10px;
    }
    
    .abc {
      padding-top: 30px;
    }
    
    .abc .abc_abc {
      padding-bottom: 10px;
      border-bottom: 1px solid #D8D8D8;
      width: 774px;
      font-size: 24px;
      font-weight: 300;
    }
    
    .abc .description {
      margin-top: 20px;
      margin-bottom: 10px;
      padding-bottom: 10px;
      width: 774px;
    }
    
    .abcText {
      font-weight: bold;
      margin-right: 10px;
    }
    
    .text-circle {
      background-color: #193661;
      color: white;
      border-radius: 50%;
      padding: 2px;
      margin-right: 10px;
      font-size: 13px;
    }
    
    .text-tectangle {
      color: white;
      background-color: green;
      border-radius: 4px;
      padding: 2px;
      font-size: 13px;
      margin-right: 10px;
    }
    
    .template-table {
      border-collapse: separate;
      border-spacing: 6px;
    }
    
    .template-table td {
      text-align: left;
      vertical-align: top;
      font-size: 100%;
    }
  </style>
</head>

<body>
  <table>
    <thead>
      <tr>
        <td>
          <table class='top-abc_abc-td'>
            <tr>
              <td class='top-abc_abc-td'>
                <img class='abc_abc1-img' src='WMC_logo_no_container_R-white_20170905_141629.png' alt=''>
              </td>
              <td class='top-abc_abc-td' style='width: 415px;'>
                <h2>aaaaaa Name</h2>
                <h2>aaaaaa1 Name</h2>
              </td>
              <td class='top-abc_abc-td'>
                <div style='margin: 8px'>Date</div>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <table class='template-table'>
            <tr>
              <td class='col-1 templateabc_abc'>
                <p class='templateabc_abcText'>hjksdldasd</p>
                hjksdldasdck. hjksdldasd shown in
                <i>italics</i> are inactive.
              </td>
              <td class='col-2 templateabc_abc'>
                <p class='templateabc_abcText'>aaaaaa hjksdldasd</p>
                hjksdldasd.
              </td>
              <td class='col-3 templateabc_abc'>
                <p class='templateabc_abcText'>hjksdldasd</p>
                hjksdldasd the hjksdldasd falls in a top percentile.
              </td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td>
          <table class='abc'>
            <thead>
              <tr>
                <td class='abc_abc'>
                  <span class='abcText'>xyzA, xyz1A</span>
                </td>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>
                  <table class='template-table'>
                    <tr>
                      <td class='col-1 col-border-right'>
                        <table>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              .
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>

                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              .
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              .
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                        </table>
                      </td>
                      <td class='col-2 col-border-right'>
                        <div class='col-div'>
                          <span class='text-circle'>hjk</span>3xyz, aaaaaa</div>
                        <div class='col-div'>
                          <span class='text-circle'>hjk</span>3xyz, aaaaaa
                        </div>
                      </td>
                      <td class='col-3'>
                        <div class='col-div'>
                          <span class='text-tectangle'>hjk</span> 3xyz, aaaaaa
                        </div>
                        <div class='col-div'>
                          <span class='text-tectangle'>hjk</span> 3xyz, aaaaaa
                        </div>
                        <div class='col-div'>
                          <span class='text-tectangle'>hjk</span> 3xyz, aaaaaa
                        </div>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
      <tr>
        <td>
          <table class='abc'>
            <thead>
              <tr>
                <td class='abc_abc'>
                  <span class='abcText'>xyzB, xyz1B</span>
                </td>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>
                  <table class='template-table'>
                    <tr>
                      <td class='col-1 col-border-right'>
                        <table>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              .
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>

                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              .
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              .
                            </td>
                          </tr>
                          <tr>
                            <td class='sub-col-1'>
                              xyz, aaaaaa
                            </td>
                            <td class='sub-col-2'>
                              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.
                            </td>
                          </tr>
                        </table>
                      </td>
                      <td class='col-2 col-border-right'>
                        <div class='col-div'>
                          <span class='text-circle'>hjk</span>3xyz, aaaaaa</div>
                        <div class='col-div'>
                          <span class='text-circle'>hjk</span>3xyz, aaaaaa
                        </div>
                      </td>
                      <td class='col-3'>
                        <div class='col-div'>
                          <span class='text-tectangle'>hjk</span> 3xyz, aaaaaa
                        </div>
                        <div class='col-div'>
                          <span class='text-tectangle'>hjk</span> 3xyz, aaaaaa
                        </div>
                        <div class='col-div'>
                          <span class='text-tectangle'>hjk</span> 3xyz, aaaaaa
                        </div>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
      <tr>
        <td>
          <table class='abc'>
            <thead>
              <tr>
                <td class='abc_abc'>
                  <span class='abcText'>xyzC, xyz1C</span>
                </td>
              </tr>
            </thead>
          </table>
        </td>
      </tr>
      <tr>
        <td>
          <table class='abc'>
            <thead>
              <tr>
                <td class='abc_abc'>
                  <span class='abcText'>xyzD, xyz1D</span>
                </td>
              </tr>
            </thead>
          </table>
        </td>
      </tr>
      <tr>
        <td>
          <table class='abc'>
            <thead>
              <tr>
                <td class='abc_abc'>
                  <span class='abcText'>xyzE, xyz1E</span>
                </td>
              </tr>
            </thead>
          </table>
        </td>
      </tr>
    </tbody>
  </table>
</body>

</html>
&#13;
&#13;
&#13;

我使用aspose.html进行此转换,使用以下代码

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 code here
            ");
        }

        // 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));
        pdf_options.PageSetup.AnyPage.Margin = new Aspose.Html.Drawing.Margin(20);
        // 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           
    }
}

}

我怎么能

  • 每页重复页脚?
  • 为每页提供页码?

1 个答案:

答案 0 :(得分:0)

在调查故障单 HTMLNET-1251 HTMLNET-1252 之前,您可以使用Aspose.PDF下面的代码片段将HTML文件转换为PDF文档.NET API。

HtmlLoadOptions options = new HtmlLoadOptions(dataDir);

Document document = new Document(dataDir + "HTMLToPDF.html", options);

document.Save("HTMLToPDF_out.pdf");

然后您可以使用您环境中的以下代码段为生成的PDF文档添加页码标记。

// Open document
Document pdfDocument = new Document(dataDir+ "PageNumberStamp.pdf");

// Create page number stamp
PageNumberStamp pageNumberStamp = new PageNumberStamp();
// Whether the stamp is background
pageNumberStamp.Background = false;
pageNumberStamp.Format = "Page # of " + pdfDocument.Pages.Count;
pageNumberStamp.BottomMargin = 10;
pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center;
pageNumberStamp.StartingNumber = 1;
// Set text properties
pageNumberStamp.TextState.Font = FontRepository.FindFont("Arial");
pageNumberStamp.TextState.FontSize = 14.0F;
pageNumberStamp.TextState.FontStyle = FontStyles.Bold;
pageNumberStamp.TextState.FontStyle = FontStyles.Italic;
pageNumberStamp.TextState.ForegroundColor = Color.Aqua;

// Add stamp to particular page
pdfDocument.Pages[1].AddStamp(pageNumberStamp);

dataDir = dataDir + "PageNumberStamp_out.pdf";
// Save output document
pdfDocument.Save(dataDir);

您可以访问以下文档文章以获取更多信息: