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)