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

Free Support Forum - aspose.com

Text Replacement issue with automatically re-arrange Page Contents with Aspose.pdf

I’m trying to do a text replacement of a small text with a large text where it can automatically re-arrange Page Contents.

content overlap

i m using snapping code below

string DocPath = @“C:\Users\Sumitra.9.Kumar\Desktop\C02_M06_Understanding_Residential_Construction-Structural_Components.pdf”;
Document pdfDocument = new Document(DocPath);
string searchParagraphText = @" such as terminology, shapes, how to distinguish the";
string repParagraphSearch = @“Detail key components of an agreement. this is not working fine at this time.”;
string regSearchText = searchParagraphText.Replace(" “, @”\s*");
Rotation rt;

        foreach (Page page in pdfDocument.Pages)
        {
            rt = page.Rotate;
            page.Rotate = Rotation.None;
            var textFragmentAbsorber = new TextFragmentAbsorber("(?i)(" + regSearchText + ")", new TextSearchOptions(true));
            page.Accept(textFragmentAbsorber);
            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
            foreach (TextFragment textFragment in textFragmentCollection)
            {
             
                textFragment.Text = repParagraphSearch;
            }
        }
        string newDocPath = @"C:\Users\Sumitra.9.Kumar\Desktop\pdf\C02_M06_Understanding_Residential_Construction-Structural_Components1.pdf"; //New Generate Pdf
        pdfDocument.Save(newDocPath);

@shravan_sumitra

Can you please share your sample PDF document for our reference? We will test the scenario in our environment and address it accordingly.

Thanks asad.ali for your immediate response.

As you requested , please find the attached a copy of PDF and below is code snippet
Aspose.Pdf.License PdfLicense = new Aspose.Pdf.License();
//// MemoryStream stream = new MemoryStream(File.ReadAllBytes(@“Aspose.PDF.NET.lic”));
PdfLicense.SetLicense(@“Aspose.PDF.NET.lic”);
Console.WriteLine(“Replacing Process continue , now check new Generated PDF”);
string DocPath = @“C:\Users\Sumitra.9.Kumar\Desktop\CitationNeededBook-Sample.pdf”;
Document pdfDocument = new Document(DocPath);
string searchParagraphText = @“Wikipedia. Whether you’ve used it to settle an argument, “;
string repParagraphSearch = @“Detail key components of an agreement. this is not working fine at this time.”;
string regSearchText = searchParagraphText.Replace(” “, @”\s*”);
Rotation rt;

        foreach (Page page in pdfDocument.Pages)
        {
            rt = page.Rotate;
            page.Rotate = Rotation.None;
            var textFragmentAbsorber = new TextFragmentAbsorber("(?i)(" + regSearchText + ")", new TextSearchOptions(true));
            page.Accept(textFragmentAbsorber);
            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
            foreach (TextFragment textFragment in textFragmentCollection)
            {
             
                textFragment.Text = repParagraphSearch;
            }
        }
        string newDocPath = @"C:\Users\Sumitra.9.Kumar\Desktop\pdf\CitationNeededBook-Sample.pdf"; //New Generate Pdf
        pdfDocument.Save(newDocPath);

CitationNeededBook-Sample.pdf (1.0 MB)

Thanks
shravan sumitra

@shravan_sumitra

Please try to set the TextReplaceOptions like below and let us know in case you still face any issues:

textFragmentAbsorber.TextReplaceOptions = new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);

Thanks for Response.
Using this
textFragmentAbsorber.TextReplaceOptions = new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
code Table content overlap and Bullet point also not in proper position .

Here is snapping whole code

Aspose.Pdf.License PdfLicense = new Aspose.Pdf.License();
//// MemoryStream stream = new MemoryStream(File.ReadAllBytes(@“Aspose.PDF.NET.lic”));
PdfLicense.SetLicense(@“Aspose.PDF.NET.lic”);
Console.WriteLine(“Replacing Process continue , now check new Generated PDF”);
string DocPath = @“C:\Users\Sumitra.9.Kumar\Desktop\CitationNeededBook-Sample.pdf”;
Document pdfDocument = new Document(DocPath);
string searchParagraphText = @“Residential Lot Styles and Features”;
string repParagraphSearch = @“Detail key components of an agreement of purchase and sale for an industrial property”;
string regSearchText = searchParagraphText.Replace(" “, @”\s*");
Rotation rt;

        foreach (Page page in pdfDocument.Pages)
        {
            rt = page.Rotate;
            page.Rotate = Rotation.None;
            var textFragmentAbsorber = new TextFragmentAbsorber("(?i)(" + regSearchText + ")", new TextSearchOptions(true));
            textFragmentAbsorber.TextReplaceOptions = new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
            page.Accept(textFragmentAbsorber);
            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
            foreach (TextFragment textFragment in textFragmentCollection)
            {
                textFragment.Text = repParagraphSearch;
            }
        }
        string newDocPath = @"C:\Users\Sumitra.9.Kumar\Desktop\pdf\CitationNeededBook-Sample.pdf"; //New Generate Pdf
        pdfDocument.Save(newDocPath);

CitationNeededBook-Sample.pdf (716.6 KB)

@shravan_sumitra

We were able to replicate the similar issue in our environment. Therefore, we have logged it as PDFNET-50797 in our issue tracking system for correction. We will look into its details and keep you posted with the status of its resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.