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

Free Support Forum - aspose.com

TextFragmentCollection class of Aspose.Pdf is taking too much time in finding and replacing Text

I am using Aspose.Pdf to find and replace text in a Pdf File. If a Pdf File has 10 words to find and replace then it is taking 2 to 3 minute. But if a Pdf File has 80 words(10 words are repeating 8 times on the File) then it is taking 20 to 25 minutes in Finding and replacing text on a Pdf File. I am using the below code. Please suggest me what the changes i will make in my code that it will take minimum time.
private void PrintPdfDocument(object FileNameSource, DataTable dt)
{
object fileNameSource = FileNameSource;
object fileName = System.IO.Path.GetDirectoryName(System.IO.Path.GetTempPath()) + “\” + DateTime.Now.Ticks.ToString() + “.pdf”;
File.Copy(fileNameSource.ToString(), fileName.ToString(), false);
pdfDocument = new Document(FileNameSource.ToString());
try
{
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("@color_arabic");
pdfDocument.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection)
{
textFragment.Text = txtColorArabic.Text.ToString().Trim();
}
textFragmentAbsorber = new TextFragmentAbsorber("@color_english");
pdfDocument.Pages.Accept(textFragmentAbsorber);
textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection)
{
textFragment.Text = txtColorEnglish.Text.Trim();
}
textFragmentAbsorber = new TextFragmentAbsorber("@unit_arabic");
pdfDocument.Pages.Accept(textFragmentAbsorber);
textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection)
{
textFragment.Text = ConvertNumeralsToArabic(txt_ArabicUnit.Text.ToString());

            }
            textFragmentAbsorber = new TextFragmentAbsorber("@unit_english");
            pdfDocument.Pages.Accept(textFragmentAbsorber);
            textFragmentCollection = textFragmentAbsorber.TextFragments;
            foreach (TextFragment textFragment in textFragmentCollection)
            {
                textFragment.Text = txtUnitEnglish.Text.ToString().Trim();     
            }
            textFragmentAbsorber = new TextFragmentAbsorber("@mfg_arabic");
            pdfDocument.Pages.Accept(textFragmentAbsorber);
            textFragmentCollection = textFragmentAbsorber.TextFragments;
           
            foreach (TextFragment textFragment in textFragmentCollection)
            {
                textFragment.Text = ConvertNumeralsToArabic(dt.Rows[0]["ProdDate"].ToString());
             
            }
            textFragmentAbsorber = new TextFragmentAbsorber("@mfg_english");
            pdfDocument.Pages.Accept(textFragmentAbsorber);
            textFragmentCollection = textFragmentAbsorber.TextFragments;
            foreach (TextFragment textFragment in textFragmentCollection)
            {
                textFragment.Text = dt.Rows[0]["ProdDate"].ToString().Trim();
            }
            textFragmentAbsorber = new TextFragmentAbsorber("@exp_arabic");
            pdfDocument.Pages.Accept(textFragmentAbsorber);
            textFragmentCollection = textFragmentAbsorber.TextFragments;
            string expArabic = dt.Rows[0]["ExpDate"].ToString();
            foreach (TextFragment textFragment in textFragmentCollection)
            {
                textFragment.Text = ConvertNumeralsToArabic(dt.Rows[0]["ExpDate"].ToString());
               
            }
            textFragmentAbsorber = new TextFragmentAbsorber("@exp_english");
            pdfDocument.Pages.Accept(textFragmentAbsorber);
            textFragmentCollection = textFragmentAbsorber.TextFragments;
            foreach (TextFragment textFragment in textFragmentCollection)
            {
                textFragment.Text = dt.Rows[0]["ExpDate"].ToString().Trim();
        
            }
            if ((dt.Rows[0]["InventBatchId"].ToString().Trim().Contains("B")) &&
              (dt.Rows[0]["InventBatchId"].ToString().Trim().Substring(0, 1) == "B"))
            {
                textFragmentAbsorber = new TextFragmentAbsorber("@bch_arabic");
                pdfDocument.Pages.Accept(textFragmentAbsorber);
                textFragmentCollection = textFragmentAbsorber.TextFragments;
                foreach (TextFragment textFragment in textFragmentCollection)
                {
                    textFragment.Text = ConvertNumeralsToArabic(dt.Rows[0]["InventBatchId"].ToString().Trim().Substring(1).Replace("-", ""));
         
                }

                textFragmentAbsorber = new TextFragmentAbsorber("@bch_english");
                pdfDocument.Pages.Accept(textFragmentAbsorber);
                textFragmentCollection = textFragmentAbsorber.TextFragments;
                foreach (TextFragment textFragment in textFragmentCollection)
                {
                    textFragment.Text = dt.Rows[0]["InventBatchId"].ToString().Trim().Substring(1).Replace("-", "");                       
                }
            }
            else
            {
                textFragmentAbsorber = new TextFragmentAbsorber("@bch_arabic");
                pdfDocument.Pages.Accept(textFragmentAbsorber);
                textFragmentCollection = textFragmentAbsorber.TextFragments;
                foreach (TextFragment textFragment in textFragmentCollection)
                {
                    textFragment.Text = ConvertNumeralsToArabic(dt.Rows[0]["InventBatchId"].ToString().Trim());                     
                }

                textFragmentAbsorber = new TextFragmentAbsorber("@bch_english");
                pdfDocument.Pages.Accept(textFragmentAbsorber);
                textFragmentCollection = textFragmentAbsorber.TextFragments;
                foreach (TextFragment textFragment in textFragmentCollection)
                {
                    textFragment.Text = dt.Rows[0]["InventBatchId"].ToString().Trim();                    
                }
            }             

            pdfDocument.Save(@fileName.ToString());

}

@ikramshams,

Can you please share source files along with environment details and version details which you are using on your end so that we may further investigate to help you out.

The Aspose.Pdf.dll File Version is 19.10.0.0. I am using visual Studio 2015. The Pdf File which i am using is as attachment.

Size 7 (135 x 45) General A4 white update.pdf (5.5 MB)

@ikramshams,

Thank you for contacting support.

Would you please share SSCCE code because some variables are not defined. We will then be able to proceed further to help you out. Before sharing requested information with us please try to use Aspose.PDF latest version 20.1 and if there is still an issue than please share these with us.

@Adnan.Ahmad
Sorry for delay in reply. I updated the version 20.1 and checked with different scenario. I found that the delay in replacing the text is due to Pdf File Size. If my File ‘Size6’ (which is attachment) is less than 1 MB(830 KB) then only 1 to 2 minute is taking in replacing all the text. But if my File ‘Size6A4’ (which is as attachment) is more than 5 MB(5.62 MB) then it is taking more time in replacing the Text.
In My coding, For your convenience i have put only static Text. I have removed the Dynamic text.

private void button2_Click(object sender, EventArgs e)
{
pdfDocument = new Document(@“D:\Backup\Pdf\OriginalFiles_Old2\Size6H.pdf”);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("@color_arabic");
pdfDocument.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection)
{
textFragment.Text = “ابيض”;
}

        textFragmentAbsorber = new TextFragmentAbsorber("@color_english");
        pdfDocument.Pages.Accept(textFragmentAbsorber);
        textFragmentCollection = textFragmentAbsorber.TextFragments;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            textFragment.Text = "White";
        }

        textFragmentAbsorber = new TextFragmentAbsorber("@unit_arabic");
        pdfDocument.Pages.Accept(textFragmentAbsorber);
        textFragmentCollection = textFragmentAbsorber.TextFragments;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            textFragment.Text = "١٨ لتر";
        }

        textFragmentAbsorber = new TextFragmentAbsorber("@unit_english");
        pdfDocument.Pages.Accept(textFragmentAbsorber);
        textFragmentCollection = textFragmentAbsorber.TextFragments;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            textFragment.Text = "18 LTR";
        }

        textFragmentAbsorber = new TextFragmentAbsorber("@mfg_arabic");
        pdfDocument.Pages.Accept(textFragmentAbsorber);
        textFragmentCollection = textFragmentAbsorber.TextFragments;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            //textFragment.Text = @"٠١-٠١-٢٠١٩";
            textFragment.Text = ConvertNumerals(@"18-08-2019");
        }

        textFragmentAbsorber = new TextFragmentAbsorber("@mfg_english");
        pdfDocument.Pages.Accept(textFragmentAbsorber);
        textFragmentCollection = textFragmentAbsorber.TextFragments;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            textFragment.Text = @"18-08-2019";
        }

        textFragmentAbsorber = new TextFragmentAbsorber("@exp_arabic");
        pdfDocument.Pages.Accept(textFragmentAbsorber);
        textFragmentCollection = textFragmentAbsorber.TextFragments;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            textFragment.Text = ConvertNumerals(@"31-12-2020");
        }

        textFragmentAbsorber = new TextFragmentAbsorber("@exp_english");
        pdfDocument.Pages.Accept(textFragmentAbsorber);
        textFragmentCollection = textFragmentAbsorber.TextFragments;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            textFragment.Text = @"31-12-2020";
        }

        textFragmentAbsorber = new TextFragmentAbsorber("@bch_arabic");
        pdfDocument.Pages.Accept(textFragmentAbsorber);
        textFragmentCollection = textFragmentAbsorber.TextFragments;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            /*textFragment.Text = @"٠٠٠١٢٢-١٩"; *///
            textFragment.Text = ConvertNumerals(@"19-000122");
        }

        textFragmentAbsorber = new TextFragmentAbsorber("@bch_english");
        pdfDocument.Pages.Accept(textFragmentAbsorber);
        textFragmentCollection = textFragmentAbsorber.TextFragments;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            textFragment.Text = @"19-000122";
        }

        pdfDocument.Save(@"D:\Backup\Pdf\Output Files\Size6H.pdf");
        MessageBox.Show("Pdf File saved successfully.");
    
}
    public string ConvertNumerals(string input)
    {
        if (new string[] { "ar-lb", "ar-SA" }
              .Contains(Thread.CurrentThread.CurrentCulture.Name))
        {
            return input.Replace('0', '\u06f0')
                    .Replace('1', '\u06f1')
                    .Replace('2', '\u06f2')
                    .Replace('3', '\u06f3')
                    .Replace('4', '\u06f4')
                    .Replace('5', '\u06f5')
                    .Replace('6', '\u06f6')
                    .Replace('7', '\u06f7')
                    .Replace('8', '\u06f8')
                    .Replace('9', '\u06f9');
        }
        else return input;
    }

Size6H.pdf (5.6 MB)
Size6.pdf (829.3 KB)

@ikramshams,

I have observed your issue and like to inform that I have created ticket with ID PDFNET-47657 in our issue tracking system to investigate and resolve this issue as soon possible.