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

Free Support Forum - aspose.com

Issue with HtmlFragment class in 9.5.0 version

Hi,

I am using the below piece of code and when I use new line character \n, the HTML Fragment does not recognizes it and places the content in the same line.

Document doc = new Document();
Page page = doc.getPages().add();
HtmlFragment title = new HtmlFragment("Html Fragments\n\nTable Of Contents");
title .setKeptWithNext (true);
title .getMargin().setBottom (10);
title .getMargin().setTop (100);
page.getParagraphs().add(title );
doc.Save("C://toc.pdf");

There is one more issue I noticed, it does not work with cell object when I use cell.getParagraphs().add(title).

Can you please let me know if this is an issue in the 9.5.0 release.

Thanks
Rajeev Mathur

Hi Rajeev


Thanks for your inquiry. Please use
tag for line break, it will help you to move text on new line. Moreover I am afraid I am unable to notice the HtmlFragment object issue with cell object. Please check following code snippet to details.

Document doc = new Document();<o:p></o:p>

Page page = doc.getPages().add();<o:p></o:p>

// Initializes a new instance of the
Table
<o:p></o:p>

com.aspose.pdf.Table table = new
com.aspose.pdf.Table();<o:p></o:p>

table.setColumnWidths(“150 150”);<o:p></o:p>

// Set the table border color as
LightGray
<o:p></o:p>

table.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, .5f,
com.aspose.pdf.Color.getLightGray()));<o:p></o:p>

HtmlFragment html = new HtmlFragment("<fontsize=10>Html
Fragments
Table Of
Contents"
);<o:p></o:p>

html.getMargin().setBottom(10);<o:p></o:p>

// add row to table<o:p></o:p>

com.aspose.pdf.Row row1 =
table.getRows().add();<o:p></o:p>

// add table cells<o:p></o:p>

row1.getCells().add(“Column
1”
);<o:p></o:p>

row1.getCells().add(“Column
2”
);<o:p></o:p>

// create a loop to add 10 rows<o:p></o:p>

for (int row_count = 1;
row_count < 5; row_count++)<o:p></o:p>

{<o:p></o:p>


//
add row to table
<o:p></o:p>


com.aspose.pdf.Row row = table.getRows().add();<o:p></o:p>


//
add table cells
<o:p></o:p>


com.aspose.pdf.Cell cell1 = row.getCells().add();<o:p></o:p>


cell1.getParagraphs().add(new TextFragment(“test”));<o:p></o:p>


<o:p></o:p>


com.aspose.pdf.Cell cell2 = row.getCells().add();<o:p></o:p>


cell2.getParagraphs().add(html);<o:p></o:p>

}<o:p></o:p>

// Add table object to first page of
input document
<o:p></o:p>

page.getParagraphs().add(table);<o:p></o:p>

doc.save(myDir+“out_htmltext.pdf”); <o:p></o:p>

Please feel free to contact us for any further assistance.


Best Regards,

Hi Tilal,

The issue is when we are trying to add HtmlFragment in an existing page. below is the code snippet I am using. I have attached a screenshot of the issue.

Page tocNewPage = doc.getPages().get_Item(4);
com.aspose.pdf.Table table2 = new com.aspose.pdf.Table();
table2.setColumnWidths(“200 50 200”);
table2.setBorder(new BorderInfo(BorderSide.All));
// Set the table border color as LightGray
HtmlFragment html2 = new HtmlFragment("<fontsize=10>Html FragmentsTable Of Contents");

// add row to table
com.aspose.pdf.Row row2 = table2.getRows().add();
// add table cells
row2.getCells().add().getParagraphs().add(html2);
row2.getCells().add(" ");
row2.getCells().add(“Page Numbers”);

newPage.getParagraphs().add(table2);

The result will add the table wit only Page Numbers heading but the html fragment is added on the left corner at the end of the page.

Hi Rajeev,


Thanks for your feedback. Please share your complete code as I am unable to replicate the issue with your shared code.

// Open the source PDF document<o:p></o:p>

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(myDir+"Helloworld.pdf");

Page tocNewPage = pdfDocument.getPages().get_Item(1);

com.aspose.pdf.Table table2 = new com.aspose.pdf.Table();

table2.setTop(100);

table2.setColumnWidths("200 50 200");

table2.setBorder(new BorderInfo(BorderSide.All));

// Set the table border color as LightGray

HtmlFragment html2 = new HtmlFragment("Html FragmentsTable Of Contents");

html2.getMargin().setBottom(10);

// add row to table

com.aspose.pdf.Row row2 = table2.getRows().add();

// add table cells

row2.getCells().add().getParagraphs().add(html2);

row2.getCells().add(" ");

row2.getCells().add("Page Numbers");

tocNewPage.getParagraphs().add(table2);

// Save the output file

pdfDocument.save(myDir+"HtmlFragment_output.pdf");


Please feel free to contact us for any further assistance.


Best Regards,