Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Dec 14, 2017 7:41 pm

I'm trying to append some HTML to my PDF document using PdfDocument.LoadFromHTML. I need the HTML rendered to the PDF as text that can be copied by the user rather than an image. Is this possible?
Code: Select all
 
void AddHtmlToPdf(String template)
{
    PdfDocument htmlPdf = new PdfDocument();
    PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
    htmlLayoutFormat.Layout = PdfLayoutType.OnePage;
    htmlLayoutFormat.IsWaiting = false;
    PdfPageSettings setting = new PdfPageSettings();
    setting.Size = PdfPageSize.Letter;
    setting.Margins = new PdfMargins(72, 50, 72, 50);
    string htmlCode = this.ToHtml(template);
    htmlCode = "<span style='font-size:7pt'>" + htmlCode + "</span>";

    Thread thread = new Thread(() =>
    { htmlPdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat);});
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();

    orderPdf.AppendPage(htmlPdf);
}

Bill.Schalck
 
Posts: 14
Joined: Fri Jul 15, 2016 2:35 pm

Fri Dec 15, 2017 3:16 am

Hello,

Thanks for your inquiry. The method "LoadFromHTML" would render the pdf depending on the IE engine installed on your system, if it is IE9 or above, it would be rendered as an image while version 8 or below as text. Never mind, our Spire.Pdf
supports the parsing of a series of basic HTML tags including Font, B, I, U, Sub, Sup and BR. If your html string is not so complex, you could try the below code to accomplish your task.
Code: Select all
            PdfDocument orderPdf = new PdfDocument();
            orderPdf.LoadFromFile(@"sample.pdf");

            PdfDocument htmlPdf = new PdfDocument();
            PdfMargins margins = new PdfMargins(72, 50, 72, 50);
            PdfNewPage newPage = htmlPdf.Pages.Add(PdfPageSize.Letter, margins) as PdfNewPage;
           
            string htmlCode = "Spire.PDF for .NET is a professional PDF component applied to creating, writing, editing, handling and reading PDF files without any external dependencies within .NET application. Using this .NET PDF library, you can implement rich capabilities to create PDF files from scratch or process existing PDF documents entirely through C#/VB.NET without installing Adobe Acrobat.";
            htmlCode = "<span style='font-size:7pt'>" + htmlCode + "</span>";

            //Render HTML text
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 5);
            PdfBrush brush = PdfBrushes.Black;
            PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlCode, font, brush);
            richTextElement.TextAlign = Spire.Pdf.Graphics.TextAlign.Left;

            //Format Layout
            PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
            format.Layout = PdfLayoutType.Paginate;
            format.Break = PdfLayoutBreakType.FitPage;

            //Draw htmlString 
            richTextElement.Draw(newPage, new RectangleF(0, 20,
                newPage.GetClientSize().Width-margins.Left-margins.Right, newPage.GetClientSize().Height-margins.Top-margins.Bottom), format);
           //Save and reload, this step couldn't be ignored.
            htmlPdf.SaveToFile("temp.pdf");
            PdfDocument tempPdf = new PdfDocument("temp.pdf");

           //Append the page to the old document.
            orderPdf.AppendPage(tempPdf );
            orderPdf.SaveToFile("newpdf.pdf");

If your html has complicated content rather than the basic tags, you need to try the other approach, that is converting the html to pdf with new plugin.
Any doubt, welcome to write back.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Dec 19, 2017 8:18 am

Hello,

Greetings from e-iceblue!
How is the issue now? Could you please give us some feedback?

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.PDF