首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将HTML页眉附加到Spire PDF

将HTML页眉附加到Spire PDF
EN

Stack Overflow用户
提问于 2019-04-25 18:21:57
回答 1查看 659关注 0票数 0

我使用Spire PDF将我的HTML模板转换为PDF文件。下面是相同的示例代码:

代码语言:javascript
复制
class Program
  {
      static void Main(string[] args)
      {
          //Create a pdf document.
          PdfDocument doc = new PdfDocument();
          PdfPageSettings setting = new PdfPageSettings();
          setting.Size = new SizeF(1000,1000);
          setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);
          PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
          htmlLayoutFormat.IsWaiting = true;
          String url = "https://www.wikipedia.org/";

          Thread thread = new Thread(() =>
          { doc.LoadFromHTML(url, false, false, false, setting,htmlLayoutFormat); });
          thread.SetApartmentState(ApartmentState.STA);
          thread.Start();
          thread.Join();
          //Save pdf file.

          doc.SaveToFile("output-wiki.pdf");
          doc.Close();
          //Launching the Pdf file.
          System.Diagnostics.Process.Start("output-wiki.pdf");
      }
    }

这是预期的工作,但现在我想添加页眉和页脚到所有的页面。虽然可以使用SprirePdf添加页眉和页脚,但我的要求是将HTML模板添加到页眉,这是我无法实现的。有没有办法将html模板渲染到页眉和页脚?

EN

回答 1

Stack Overflow用户

发布于 2019-08-06 14:38:43

Spire.PDF提供了一个PdfHTMLTextElement类,支持在页面上呈现简单的HTML标签,包括Font,B,I,U,Sub,Sup和BR。您可以使用以下代码片段将HTML附加到现有PDF文档的标题空间。据我所知,没有办法使用Spire.PDF将复杂的超文本标记语言仅作为文档的一部分来呈现。

代码语言:javascript
复制
//load an existing pdf document
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

//loop through the pages
for (int i = 0; i < doc.Pages.Count; i++)
{
    //get the specfic page
    PdfPageBase page = doc.Pages[i];

    //define HTML string
    string htmlText = "<b>XXX lnc.</b><br/><i>Tel:889 974 544</i><br/><font color='#FF4500'>Website:www.xxx.com</font>";

    //render HTML text
    PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12);
    PdfBrush brush = PdfBrushes.Black;
    PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, brush);
    richTextElement.TextAlign = TextAlign.Left;

    //draw html string at the top white space
    richTextElement.Draw(page.Canvas, new RectangleF(70, 20, page.GetClientSize().Width - 140, page.GetClientSize().Height - 20));
}

//save to file
doc.SaveToFile("output.pdf");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55847045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档