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