HTML Text Block

Just confirming that what I’m trying to do isn’t supported: I have a block of HTML text in a string that I’d like to inject into a PDF document. This seems to only be possible using Aspose.HTML. (We are evaluation switching from a competitor but one of the things we get from them in an HTMLStamp which acts like ImageStamp/TextStamp but will resolve HTML).

@smorley

Aspose.PDF supports adding an HTML String in PDF document which will be rendered in the final document. Please check the below documentation topic for further information:

Please feel free to let us know in case you need more information.

I have this working but its not positioning the block where I expect.
var doc = new Aspose.Pdf.Document();
var html = new HtmlFragment("<span style=“color:red”>Some Italics
Some Bold");
var page = doc.Pages.Add();
html.TextState = new TextState();
html.TextState.ForegroundColor = Aspose.Pdf.Color.Green;
html.TextState.BackgroundColor = Aspose.Pdf.Color.Silver;
html.VerticalAlignment = VerticalAlignment.Bottom;
html.HorizontalAlignment = HorizontalAlignment.Left;
html.Margin.Bottom = 100;
html.Margin.Top = 100;
html.Margin.Left = 100;
html.Margin.Right = 100;
page.Paragraphs.Add(html);
I would expect this to position the text near the bottom of the page but instead its about a 1/3 of the way down the page. The horizontal position was as expected. And the background/foreground colors don’t work but the style color did. Can you point me to details on how the Margin should operate.

@smorley

Please try below code and see if it helps in achieving the required output:

var pdfDocument = new Document();
pdfDocument.Pages.Add();
for (int i = 1; i <= pdfDocument.Pages.Count; i++)
{
 var hf = new Aspose.Pdf.HeaderFooter { Margin = new Aspose.Pdf.MarginInfo(40, 0, 40, 0) };
 String htmltable = "<span style=\"color:red\"><i>Some Italics</i><br/><b>Some Bold</b></span>";
 var html = new HtmlFragment(htmltable)
 {
  Margin = new Aspose.Pdf.MarginInfo(0, 0, 0, 40)
 };
 hf.Paragraphs.Add(html);
 pdfDocument.Pages[i].Footer = hf;
 html.VerticalAlignment = VerticalAlignment.Top;
}
pdfDocument.Save(dataDir + "output.pdf");

This doesn’t look like a generalized solution to positioning objects. Is it not possible to specify the absolute position of a paragraph, graph, or other object on the page?

@smorley

In that case, you can please use floating box to specify the position over page and add HtmlFragment inside it like below:

Document doc = new Document();
Page page = doc.Pages.Add();
FloatingBox floatingBox = new FloatingBox();
floatingBox.Left = 100;
floatingBox.Top = 100;
var htmlFragment = new HtmlFragment(html);
floatingBox.Paragraphs.Add(htmlFragment);
page.Paragraphs.Add(floatingBox);
doc.Save(dataDir + "HTMLFragmentinFloatingBox.pdf");

That worked. I had stumbled on this feature was just trying to use it when this email arrived and confirmed that path.
Thank you.

@smorley

It is nice to know that your were able to achieve your required output. Please feel free to create a new topic in case you need further assistance.