Inline image alignment in HTML for Aspose.PDF

Hi,

I am using the latest version of the Aspose.Pdf.dll - 9.7.0.0. I am trying to render a text paragraph containing HTML tags. The HTML has inline images in it and I can’t find a way to vertically align images with the middle of the text.

Is there a way to do this?

I have attached the HTML snippet: 5nf4.lsn_snippet.txt
and the resulting PDF: 5NF4_Lsn.pdf.
I verified that the snippet HTML displays properly in Chrome, IE, and Firefox.

Here is the code:
private static void addHTMLParagraph(Section section, string content)
{
Aspose.Pdf.Generator.Text text = new Aspose.Pdf.Generator.Text(content);
text.IsHtmlTagSupported = true;
text.IsHtml5Supported = true;
section.Paragraphs.Add(text);
}



Hi Pinkhas,


Thanks for your inquiry. We have tested the scenario with new DOM approach to add HTML text in PDF document and its working fine. Please check following documentation link for the purpose. It will help you to accomplish the task.

Moreover, please note Aspose.Pdf.Generator is a legacy approach and it will be obsolete in near future. We are making new enhancements/bug fixes in new Document Object Model (DOM) of Aspose.Pdf namespace. So it is recommend to use new DOM appraoch, as it is more efficient and improved.


Please feel free to contact us for any further assistance.

Best Regards,

Thanks Tilal for the suggestion.


Yes, the HTML looks much better this way.
However, based on the example in the Programmer’s Guide, html fragments must be added to specific pages. This means that I must somehow know the physical dimensions of the fragment and make sure that it fits the available free space on the page. I am not clear if it is possible.
How should one handle many html fragments including really large ones? Some of the fragments may possibly be larger than a single page or span multiple pages.

Thanks for your support.



Hi Pinkhas,


Thanks for your feedback. Please note Page object in new DOM is similar to Section object in old generator. It adds object in a flow-layout. If a data exceeds than the page height, it automatically flows to subsequent page. However, if you want to force an object to new page then you can create a new page object and add it to paragraph object of that page. You do not need to know physical dimensions of the fragment. Please give it a try and feel free to contact us for any further assistance.

Best Regards,


Tital,


I am sorry not being clear that I tried what you suggested and it did not work.

I have tried 2 approaches:

- tried to add multiple individual HTML fragments,so they may exceed a page size. It does not fail and the PDF file size seemingly reflects all the added HTML fragments. But when I open the file only the first page is displayed.

Here is the code for this method

public static bool ProducePDFFromHTMLManyFragments(string html, string fileName)
{
bool done = false;
Document pdfDoc = new Document();
Page page = pdfDoc.Pages.Add();
try
{
for (int i = 0; i < 50; i++)
{
HtmlFragment htmlFragment = new HtmlFragment(html);
htmlFragment.HorizontalAlignment = HorizontalAlignment.Left;
page.Paragraphs.Add(htmlFragment);
}
pdfDoc.Save(fileName);
done = true;
}
catch (Exception x)
{
System.Diagnostics.Debug.WriteLine(x.Message);
}
return done;
}
I have attached the output PDF (many_html_lines.pdf) for this method.

- tried to combine all the HTML into one large multi-line HTML fragment to exceed the page size and it results in the following exception "The rectangle size can not be larger page size
Parameter name: rectangle: "
Here is the code for this method
public static bool ProducePDFFromHTMLOneFragment(string html,string fileName)
{
bool done = false;
Document pdfDoc = new Document();
Page page = pdfDoc.Pages.Add();
try
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 50; i++)
{
builder.Append(html);
}
HtmlFragment htmlFragment = new HtmlFragment(builder.ToString());
htmlFragment.HorizontalAlignment = HorizontalAlignment.Left;
page.Paragraphs.Add(htmlFragment);

pdfDoc.Save(fileName);
done = true;
}
catch (Exception x)
{
System.Diagnostics.Debug.WriteLine(x.Message);
}
return done;
}

I have attached the html fragment (snippet.html.txt) I used to pass to these functions.

Respectfully,

Pinkhas



Hi Pinkhas,

Thanks for sharing your source code. While testing the scenario with latest version of Aspose.Pdf for .NET 9.7.0, we have managed to reproduce the issue and logged a ticket PDFNEWNET-37646 in our issue tracking system for further investigation and resolution. We will notify you as soon as it is resolved.

We are sorry for the inconvenience caused.

Best Regards,

The issues you have found earlier (filed as PDFNEWNET-37646) have been fixed in Aspose.Pdf for .NET 9.8.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Thanks for letting us know.

However, I have to report that the fix seems to be partial from our perspective as it does not work for some html fragments. We have encountered at least these 2 issues:
- large html fragments
- html fragments containing large(size) images .
The document save method does not fail, it just freezes and never returns.

Here is the code snippet we used.

public static bool ProducePDFFromHTMLOneFragment(string html,string fileName)
{
bool done = false;
Document pdfDoc = new Document();
Page page = pdfDoc.Pages.Add();
try
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 5; i++)
{
builder.Append(html);
}
string buffer = builder.ToString();
HtmlFragment htmlFragment = new HtmlFragment(buffer);
page.Paragraphs.Add(htmlFragment);

pdfDoc.Save(fileName);
done = true;
}
catch (Exception x)
{
System.Diagnostics.Debug.WriteLine(x.Message);
}
return done;
}
By the way, this example works if I limit the loop to two iterations,
I have attached the html snippet (snippet_large.html.txt) we used.

I have also attached another html snippet (snippet_large_image.html.txt) that contains large image. The save method freezes while saving a document with this single html fragment.



Hi Pinkhas,


Thanks for your feedback. We have tested the scenario with your shared html text and noticed the reported issue. We logged following issues in our issue tracking system for further investigation and resolution. We will notify you as soon as these are resolved.


PDFNEWNET-37877: Large Html Fragments

PDFNEWNET-37878: Large Fragment containing large(size) images


We are sorry for the inconvenience caused.


Best Regards,

The issues you have found earlier (filed as PDFNEWNET-37877) have been fixed in Aspose.Pdf for .NET 10.0.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

The issues you have found earlier (filed as PDFNET-37878) have been fixed in Aspose.PDF for .NET 22.7.