I am trying to build pdf document from scratch instead of using XML template. I have enclosed the same program. I have set default font size and font name for the document. But when I use HtmlFragment, font size and name are not being honored. Content is displayed in times new format. How can we set font information for HtmlFragments?
When using HtmlFragment, you need to specify the font information using HTML. Please take a look at the following code snippet to accomplish your requirement:
[Java]
com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
// com.aspose.pdf.Document.addLocalFontPath(EMRPropertyManager.getAsposePdfFontsFolder());
com.aspose.pdf.Font font =
com.aspose.pdf.FontRepository.findFont("Palatino Linotype");
doc.getPageInfo().getDefaultTextState().setFont(font);
doc.getPageInfo().getDefaultTextState().setFontSize(10f);
Page page = doc.getPages().add();
com.aspose.pdf.HtmlFragment fragment =
new com.aspose.pdf.HtmlFragment("<p style=\"text-align: justify;\"> <font face=\"verdana\" color=\"green\">It was a pleasure </font>to see you on October 3, 2014. On behalf of all of us at EHE International, thank you for your continued participation in our Preventive Medicine and Lifestyle Management Program.</p>" +
"<p class=\"MsoNormal\" style=\"text-align: justify;\">This report details my assessment of your health and recommendations based on the physical exam, current and historical laboratory results, and a summary of EHE benefits available to you throughout the year. I am sending you health information you may find useful in the proactive management of your health. I encourage you to familiarize yourself with your reported personal findings and to call me or members of your EHE health team with your questions or concerns. At the end of the report, I described a few programs that will tailor toward your needs to offer you assistance and/or guidance in the optimization of your health. Please remember that we are here throughout the year.</p>" +
"<p style=\"text-align: justify;\">The following chart summarizes your key health statistics from your exam and will serve as a baseline for future exams:</p>");
page.getParagraphs().add(fragment);
doc.save("c:/pdftest/peFromScratch.pdf");
Thanks for the reply. I am encountering following issues.
1. When font name is "Palatino Linotype", font is not displaying in that format. When I copied the content to word it shows as PalatinoLinotype.
2. When I added size attribute to font element, content is always displaying in 27 font size. It seems pdf is not honoring size attribute.
3. Instead of adding font in the html, can we specify in a css file? Because in our case html content comes from database and it is hard to manipulate html to stylize for pdf. In legacy api, we used to add content using Text element and setting isHtmlTagSupported=true.
1. When font name is “Palatino Linotype”, font is not displaying in that format. When I copied the content to word it shows as PalatinoLinotype.
Hi Mamatha,
Thanks for sharing the feedback.
Please share the Palatino Linotype font, so that we can test the scenario in our environment. Furthermore, when displaying contents in MS Word, proper font is used because its installed over system and proper font is referenced/loaded by MS Word when displaying contents.
mamatha:
2. When I added size attribute to font element, content is always displaying in 27 font size. It seems pdf is not honoring size attribute.
Please share the HTML string with font size information, so that we can test the scenario in our environment.
mamatha:
3. Instead of adding font in the html, can we specify in a css file? Because in our case html content comes from database and it is hard to manipulate html to stylize for pdf. In legacy api, we used to add content using Text element and setting isHtmlTagSupported=true.
When using CSS, you need to use HtmlLoadOptions object to load contents from HTML and CSS file and during rendering, the formatting is honored. Please visit the following link for further information on Convert HTML to PDF Format
Here is sample html string for font size. I tried following approaches with HtmlFragment but it did not work.
Test paragraph
Test paragraph
This approach worked. Font size is honored when it is specified in the style attribute.
Test paragraph
Regarding HtmlLoadOptions: Sample program is converting html file into pdf. But we don't have HTML file. I am using HtmlFragments to build pdf document. In this case how can I use HtmlLoadOptions?
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.