When I create a PDF, HTML segments styled with foreground are not displayed as expected
Hi There,
Thanks for contacting support.
In order to add HTML content inside PDF document, you can use HtmlFragment Class provide by Aspose.Pdf DOM model. Please check following code snippet where I have added your HTML string into PDF using Aspose.Pdf for .NET 17.4.0, by HtmlFragment and did not notice the issue which you have mentioned. I have also attached a screenshot of the output, generated by below code, for your reference.
Document doc = new Document();
Table tab = new Table();
tab.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow;
tab.DefaultCellBorder = new BorderInfo(BorderSide.All, 1.0f, Color.Black);
tab.IsBordersIncluded = false;
Row row = tab.Rows.Add();
Cell cell = row.Cells.Add();
// Below HTML string (htmlString) is same which you have shared
cell.Paragraphs.Add(new HtmlFragment(htmlString));
cell = row.Cells.Add();
cell.Paragraphs.Add(new HtmlFragment(htmlString));
cell = row.Cells.Add();
cell.Paragraphs.Add(new HtmlFragment(htmlString));
doc.Pages.Add().Paragraphs.Add(tab);
doc.Save(dataDir + "HtmlFragment.pdf");
Please try to add HTML content by above mentioned approach and latest version of the API and in case if you still face an issue, please share a complete code snippet with sample input file(s). This way we can test the scenario in our environment and address it accordingly.
Best Regards,
Thank you for your help.
Hello There,
graphicsxp44:
I forgot to mention that I’m using a XML template. It looks like this:
Original
However, I don’t see how to use the HtmlFragment element in XML. Do you have an example ?
Thanks for adding more details to the scenario. As shared above, HtmlFragment class has been introduced in Aspose.Pdf DOM (Document Object Model) approach, whereas you are using an old Aspose.Pdf.Generator approach. I am afraid that old Aspose.Pdf.Generator approach is going to be obsolete soon and it is highly recommended to use new DOM model.
However, in order to generate PDF from XML by new model, your XML structure should be based upon new Aspose.Pdf DOM. Please check attached XML file in which I have shown a basic DOM based XML structure. You can modify your XML template/file according to new DOM based structure and generate PDF by following code snippet (Please check attached PDF file, generated by below code).
Aspose.Pdf.Document doc = new Document();<o:p></o:p>
doc.BindXml(dataDir + “XMLDOM.xml”);
doc.Save(dataDir + “XMLtoPDF_out.pdf”);
graphicsxp44:
Also I have tried your code :
firstRow.Cells[“original”].Paragraphs.Add(new HtmlFragment(evalRow.Original));
But then I get a cast error - cannot convert from ‘Aspose.Pdf.HtmlFragment’ to ‘Aspose.Pdf.Generator.Paragraph’
I’m using the latest version.
You encountered this problem because you were trying to add DOM based object (i.e HtmlFragment) into an old Aspose.Pdf.Generator based Paragraph collection. However, in order to use HtmlFragment, please check following code snippet, based upon new DOM model, where I have added a HtmlFragment inside table cell.
Aspose.Pdf.Document doc = new Document();
doc.Pages.Add();
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
Aspose.Pdf.Row row = table.Rows.Add();
Aspose.Pdf.Cell cell = row.Cells.Add();
cell.Paragraphs.Add(new HtmlFragment("<h1>This is text</h1>"));
doc.Pages[1].Paragraphs.Add(table);
For more information, you may visit “Add Html String using DOM” article in API documentation. In case of any further assistance, please feel free to contact us.
Best Regards,
Well…Aspose.Pdf.Generator may become obsolete but we have been using it extensively over the last few years. It seems the new Library is not compatible with our XML templates and we are on a schedule here.
Is an xsd available and where can we download it?
Hi,
graphicsxp44:Well…Aspose.Pdf.Generator may become obsolete but we have been using it extensively over the last few years. It seems the new Library is not compatible with our XML templates and we are on a schedule here.I’ve attached one of our XML templates as well as the corresponding C# code to generate it. Could you please take a look and send us the updated version with the new code and XML template ?We’ll get inspiration from that one in order to update all our templates.
Hi Laurent,
lnullens:Is an xsd available and where can we download it?
Thanks for the schema.
Hi Laurent,
Hello,
Hi There,
Hello,
Hi There,
Hello There,
Sure, here it is.
Hi,
Could you please urgently get back to us with a fix ? We are close to production release and this issue has been pending for almost a month.
Hi,
Can you give us an estimation as to when it will be fixed ?