We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

HtmlLoadOptions for HtmlFragment

I have snippets of HTML that I want to add to my pdf. The html Snippets have images embeded as base64 string in the html itself.

When I add the html snippet as HTMLfragement to page it works fine, but when the images are big it cuts of the image… Following is the code I am doing
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”><pre style=“font-family: “Courier New”; font-size: 9pt;”>row = table.getRows().add();
c = row.getCells().add();
HtmlFragment html = new HtmlFragment(i.getComments());<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”;”><pre style=“font-family: “Courier New”;”>c.getParagraphs().add(html);

But when I do the same html snippet with loadoptions and save as
pdf it looks fine.
Document htmlDoc = new Document(new ByteArrayInputStream(i.getComments().getBytes(StandardCharsets.UTF_8)),
htmlLoadOptions);
htmlDoc.save(
"D:/work/temp_xxx.pdf"); Is there a way for me when creating a HTMLFragment, to set the size like loadOptions ? If not is their a workaround ?

Hi Ravi,


Thanks for using our APIs.

Can you please share the input HTML and any related files, which can help us in testing the scenario in our environment. We are sorry for this inconvenience.

Attached is the HTML string that I am trying to add to my pdf using HTMLFragment. The image which is as base64 string in the html string, does not fit into the PDF page…


Attached is the code and the html string in text file. I attached the zip of both the text and java code.

Note: This is just a sample what I am seeing,… in reality the html string comes from a relational database

Hi Ravi,


Thank you for the details. You can retrieve an image stream from the PDF, and then recalculate the width and height as per the aspect ratio. Finally, you will replace the resized image using replace method of the XImage class. The width of the image should not be more than the width of PDF page (page.getPageInfo().getWidth()) Please refer to this help topic: Retrieve an Image from the PDF Document

Please let us know in case of any further assistance or questions.

Hi Ravi,


Adding more to Imran’s comments, the reason Image is not appearing properly is because image width is larger than page dimensions of resultant document. So in order to generate correct output, please try setting page dimensions as specified below. Please note that page dimensions are measured in Points where one Inch = 72 points.

[Java]

page.getPageInfo().setWidth(900);<o:p></o:p>

page.getPageInfo().setHeight(842);

page.getParagraphs().add(table);


Besides this, we do understand that it may get difficult to set the page dimensions for every other document, so for the sake of correction, I have logged it as PDFJAVA-36799 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.


PS, for your reference, I have also attached the output generated over my end.

Hmm… When I run the same code, the image displays properly, but the HTML text gets truncated.

Attached is what I get… can you share your code, following is my code…

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>String comments = FileUtils.readFileToString(new File(“D:/work/tempComments.txt”));

Document pdfDoc = new Document();
Page page = pdfDoc.getPages().add();



TextState labelState = new TextState();
labelState.setFont(FontRepository.findFont(“Arial”));
labelState.setFontSize(10);
labelState.setFontStyle(FontStyles.Bold);
TextState valueState = new TextState();
valueState.setFont(FontRepository.findFont(“Arial”));
valueState.setFontSize(10);
MarginInfo mi = new MarginInfo();
mi.setTop(5);mi.setBottom(5);mi.setLeft(5);mi.setBottom(5);

Table table = new Table();
table.setColumnWidths(“100%”);
Row row = table.getRows().add();
Cell c = null;
c = row.getCells().add();
c.setColSpan(2);
c.setWordWrapped(true);

TextFragment fragment = new TextFragment();
fragment.setMargin(mi);
TextSegment ts = new TextSegment("Comments: ");
ts.setTextState(labelState);
fragment.getSegments().add(ts);


HtmlFragment html = new HtmlFragment(comments);
html.setMargin(mi);

c.getParagraphs().add(fragment);
c.getParagraphs().add(html);

page.getPageInfo().setWidth(900);
page.getPageInfo().setHeight(842);
page.getParagraphs().add(table);



pdfDoc.save(“D:/work/tempComments.pdf”);

pdfDoc.freeMemory();

Ignore my previous post, I found my issue… I was setting the colspan when I was having only one column

Hi Ravi,


Thank you for the intimation. It is nice to hear from you that the issue has been identified and resolved. Please let us know in case of any further assistance or questions.