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 Apr 20, 2023 8:53 am

When I add a rich text string containing html tags by using:

Code: Select all
page.Canvas.DrawString(htmlText, font5, PdfBrush.Red, rect, format4, true);


whenever I use my font for the font5 variable above, the print defaults back to Arial.

How can I apply any other font asides Arial, or not all fonts family are supported, or which font family are supported for adding rich text?

Also I tried using PdfHTMLTextElement from the Tutorial: /Spire.PDF/Spire.PDF-Program-Guide/Text/How-to-Insert-HTML-Styled-Text-to-PDF-in-C-VB.NET (I gave the path because I cant post a URL), the namespace for it wasn't found.

grease14
 
Posts: 4
Joined: Fri Oct 28, 2022 6:39 am

Thu Apr 20, 2023 10:07 am

Hello,

Thanks for your inquiry.

Please kindly note that page.Canvas.DrawString() method does not support drawing HTML content. The solution provided in this help document https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Text/How-to-Insert-HTML-Styled-Text-to-PDF-in-C-VB.NET.html is only supported under .NET Framework. It is not supported under .NET Core, .NET 5.0 and above versions.

We have a solution for converting HTML text/HTML file to a PDF file, please refer to this help document
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-with-New-Plugin.html.

If you have any questions, please feel free to contact us.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3043
Joined: Wed Jun 27, 2012 8:50 am

Thu Apr 20, 2023 11:15 am

Thanks for your swift response, is there a way to change the font style of the html string in Spire?

Also I intend to add this html string as a paragraph on an existing pdf page that I'm working on (in a paragraph) not on a new page.

grease14
 
Posts: 4
Joined: Fri Oct 28, 2022 6:39 am

Fri Apr 21, 2023 2:25 am

Hi,

Thanks for your feedback.

We have a method of replacing the font of existing text in PDF. Below is sample code.
Code: Select all
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("1.pdf");

//Get the fonts used in PDF
PdfUsedFont[] fonts = doc.UsedFonts;

//Create a new font
PdfTrueTypeFont newfont = new PdfTrueTypeFont("Arial",12f,PdfFontStyle.Regular,true);

foreach (PdfUsedFont font in fonts)
{
    //Replace the font with new font
    font.Replace(newfont);
}

//Save the document
doc.SaveToFile("out.pdf");


But for your requirement to add html string as a paragraph on an existing pdf page, I'm sorry that there is no way to do this in our Spire.PDF product. Based on the PDF document structure specification, I'm afraid it's not feasible to implement this. For adding HTML string as a paragraph, I would recommend operating it in a Word document instead, as the Word document specification supports this feature.

I have a solution to offer you. First, use our Spire.PDF to convert your PDF file to a Word document. Then, use the Spire.Doc product to add HTML string, and finally save it to a PDF file. Here is an example code for your reference.
Code: Select all
String outputWord = "converted.docx";
//Convert PDF to Word
using (PdfToWordConverter converter = new PdfToWordConverter("source.pdf"))
{
    converter.SaveToDocx(outputWord);
}

//Add html string to Word and save to a PDF file
Document doc2 = new Document();
doc2.LoadFromFile(outputWord);
doc2.Sections[0].Paragraphs[0].AppendHTML(htmlString);
doc2.SaveToFile("result.pdf",Spire.Doc.FileFormat.PDF);


Please let us know if you have any issues.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3043
Joined: Wed Jun 27, 2012 8:50 am

Fri Apr 21, 2023 7:16 am

Thanks.

grease14
 
Posts: 4
Joined: Fri Oct 28, 2022 6:39 am

Return to Spire.PDF

cron