Replace text inside PDF document using Aspose.PDF for .NET - Text replacement in table cells doesn't work properly

I place a table in existing .PDF file with settings
table.ColumnAdjustment = ColumnAdjustment.AutoFitToContent;
Add come text in table cells and save the file.

Afterwards open the saved document.
replace text in table cells with text of longer length, the text doesn’t get set properly.
The cells column width doesn’t get autoadjusted to accomodate longer text.
Also the text in columns which are on right to changed text cell, get disturbed.

@MukundGadre

Would you kindly share your sample input PDF document with us along with sample code snippet to replace the text. We will test the scenario in our environment and address it accordingly.

I have already shared the same with Mr. Asad Ali

@MukundGadre

We are afraid that we did not receive any document or code snippet from your side. Would you please make sure to attach them in your reply while composing the post. Please use upload button in the post editor to upload the files. We will surely proceed further to assist you accordingly.

I had sent the code and files as reply to your email requesting to share the code. Any way, I am repeating the contents here.

Dear Mr.Asad,

Following is the code snippet for your reference. Also the generated files are attached for your reference.

The table was placed using function PlaceTable. The generated file is “AdjustedTable087.pdf”
The code was replaced using function ReplaceText on above generated file. The output file is “TextReplacedAdjustedTable087.pdf”

    public bool ReplaceText(
                           string SourceFileName
                            , string DestinationFileName
                            , int PageIndex
                            , string OriginalText
                            , string NewText
                            )
    {
        try
        {

            // Open document
            Document pdfDocument = new Document(SourceFileName);

            // Create TextAbsorber object to find all instances of the input search phrase
            TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(OriginalText);

            // Accept the absorber for all the pages
            pdfDocument.Pages.Accept(textFragmentAbsorber);

            // Get the extracted text fragments
            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

            // Loop through the fragments
            foreach (TextFragment textFragment in textFragmentCollection)
            {
                // Update text and other properties
                if (textFragment.Text == OriginalText)
                {
                    textFragment.Text = NewText;
                    MessageBox.Show("Text replaced successfully");
                    break;
                }
            }

            // Save resulting PDF document.
            pdfDocument.Save(DestinationFileName);
            MessageBox.Show("File saved.");
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
    }

Code for placing table in pdf file.
The following code in the below code needs to be uncommented to set the columns as auto adjust column width as per text contents.

            //table.ColumnAdjustment = ColumnAdjustment.AutoFitToContent;




    public bool PlaceTable(string SourceFileName
                            , string DestinationFileName
                            , int Left, int Top
                            , int PageIndex
                            , int RowCount, int ColCount
                            , ref List<string> TableContents
                            )
    {
        string RowContentString = "";
        string[] RowCellContent;

        try
        {
            // Load source PDF document
            Aspose.Pdf.Document doc = new Aspose.Pdf.Document(SourceFileName);
            // Initializes a new instance of the Table
            Aspose.Pdf.Table table = new Aspose.Pdf.Table();
            // Set the table border color as LightGray
            table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
            // Set the border for table cells
            table.DefaultCellBorder = new spose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All,0.5f,spose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
            
            table.Top = Top;
            table.Left = Left;
            //table.ColumnAdjustment = ColumnAdjustment.AutoFitToContent;

            table.ColumnWidths = "150 150 200";

            for (int row_index = 1; row_index <= RowCount; row_index++)
            {
                RowCellContent = TableContents[row_index - 1].Split(',');
                              // The row cell contents are filled using code similar to following code by the caller function  
        // prepare table contents
        //TableContents.Add("Submitted By : , Submitted On : , Submitter's comment");
        //TableContents.Add("Reviewed By : , Reviewed On : , Reviewer's comment");
        //TableContents.Add("Approved By : , Approved On : , Approver's comment");



                // Add row to table
                Aspose.Pdf.Row row = table.Rows.Add();
                // Add table cells

                for (int Col_Index = 1; Col_Index <= ColCount; Col_Index++)
                {
                    row.Cells.Add(RowCellContent[Col_Index - 1]);
                }
                //////row.Cells.Add("Column (" + row_index + ", 1)");
                //////row.Cells.Add("Column (" + row_index + ", 2)");
                //////row.Cells.Add("Column (" + row_index + ", 3)");
            }
            // Add table object to first page of input document
            doc.Pages[PageIndex].Paragraphs.Add(table);
            MessageBox.Show("Table placed.");
            
            // Save updated document containing table object
            doc.Save(DestinationFileName);

            MessageBox.Show("File saved.");

            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
    }

AdjustedTable087.pdf (88.0 KB)
TextReplacedAdjustedTable087.pdf (130.4 KB)

@MukundGadre

We have noticed the similar issue in our environment and logged it as PDFNET-48486 in our issue tracking system. We will surely check this in details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

PS: You need to post reply in forum directly as email to topic does not work at the moment.

Any updates on this one? I’m encountering the same with Aspose.PDF for Java.

@zhaolang

Regretfully, the issue has not been yet resolved. However, we have recorded your concerns and will certainly update you once additional updates are available regarding issue rectification. Please give us some time.

We apologize for the inconvenience.