Replace Text/PlaceHolders with HTML in existing PDF

Document pdfDocument = new Document(dataDir + “PDFwithFixedTemplate.pdf”);

        HtmlFragment tableContent = new HtmlFragment("<div style='color:Orange'"></div>);

//content in PDF
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("

");
foreach (Page page in pdfDocument.Pages)
{
page.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
for (int j = 1; j <= textFragmentCollection.Count; j++)
{
                Aspose.Pdf.Text.TextFragment textFragment = textFragmentCollection[j];
                if (textFragment.Text == "<table>")
                {
                    textFragment.Page.Paragraphs.Add(tableContent);
                    textFragment.Text = "";
                }
                // Update text and other properties
                if (textFragment.Text == "<paragraph>")
                {
                    textFragment.Page.Paragraphs.Add(ParagraphContent);
                    textFragment.Text = "";
                }

}
}

I need to replace a placeholder/Element/Text with HTML . So that i am able to create dynamic PDF using ASPOSE.

@Pravesh2486,

Thanks for contacting support.

I have tested the scenario and have managed to replicate the same problem. For the sake of correction, I have logged it as PDFNET-43356 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.

@Pravesh2486

We have investigated the issue and found no bugs. It is found that the source code from the ‘Description’ section wrong and ineffective.

Also, please use HtmlFragment margins to set Y-position of added HTML-text fragment.

Document pdfDocument = new Document(dataDir + "Input (1).pdf");

HtmlFragment tableContent = new HtmlFragment("Sample");//Hi");
//content in PDF
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(" Plan must be cloned from the Master copy and should be named UAT BPT – RC 2 – Material –");

pdfDocument.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

for (int j = 1; j <= textFragmentCollection.Count; j++)
{
    TextFragment textFragment = textFragmentCollection[j];
    Page page = textFragment.Page;
    Rectangle fragmentRect = (Rectangle)textFragment.Rectangle.Clone();

    //Remove source text
    textFragment.Text = "";

    //Specify margins to set HtmlFragment position
    MarginInfo info = new MarginInfo(
        fragmentRect.LLX - page.Rect.LLX - page.PageInfo.Margin.Left,
        fragmentRect.LLY - page.Rect.LLY - page.PageInfo.Margin.Top,
        page.Rect.URX - fragmentRect.URX - page.PageInfo.Margin.Right,
        page.Rect.URY - fragmentRect.URY - page.PageInfo.Margin.Bottom);
    tableContent.Margin = info;

    //Add html-text
    page.Paragraphs.Add(tableContent);
}

pdfDocument.Save(dataDir + "43356_replace_sample.pdf");