Hi Team
I have a requirement of placing an image in a specific cell of a table.
Scenario: there is a specific text “ABC” in a cell of a table. in the next three cells of that table i need to place an image in each and date in last cell of same row.
Ex: “ABC”| {Image1}|{Image2}|{“myDate”}.
Currently, i’m capturing the locations for the text “ABC” and trying to place image next to it.
Issue: this works for few documents but few documents/tables have different padding/cell sizes thus causing second image to be split b/w two cells.
Can you help me with finding a better solution to place images + date properly within the cells irrespective of the cells size/padding?
I don’t know the table name and there could be multiple table on the doc.
Quick help is appreciated. My firm holds valid aspose license.
below is my sample code:
FileStream initialsImageStream = new FileStream(initialsPath, FileMode.Open);
FileStream signImageStream = new FileStream(signPath, FileMode.Open);
DirectoryInfo directoryInfo = new DirectoryInfo(rootDirectory);
FileInfo[] fileInfos = directoryInfo.GetFiles("*.pdf");
string signedFilesDirectory = rootDirectory + "\\1";
Directory.CreateDirectory(signedFilesDirectory);
foreach (FileInfo file in fileInfos)
{
Document pdfDocument = new Document(file.FullName);
Page pdfPage = (Page)pdfDocument.Pages[1];
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("ABC");
pdfDocument.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (TextFragment txt in textFragmentCollection)
{
LLX = txt.Rectangle.LLX+20.0; //adding 20.0 to move to next cell. but each doc has different cell size
LLY = txt.Rectangle.LLY;
URX = txt.Rectangle.URX;
URY = txt.Rectangle.URY;
Console.WriteLine("{0}", txt.Rectangle);
break;
}
pdfPage.Resources.Images.Add(initialsImageStream);
pdfPage.Contents.Add(new Aspose.Pdf.Operators.GSave());
Rectangle rectangle = new Rectangle(LLX, LLY, URX, URY);
//pdfPage.AddImage(initialsImageStream, rectangle);
//Initials
Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX + 25.0, rectangle.LLY });
pdfPage.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
XImage ximage = pdfPage.Resources.Images[pdfPage.Resources.Images.Count];
pdfPage.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
pdfPage.Contents.Add(new Aspose.Pdf.Operators.GRestore());
Rectangle a_rectangle = new Rectangle(LLX + 100, LLY, URX, URY);//added 100 from actual text location but this is not working always as i cant predict how far second cell is from text.
pdfPage.AddImage(signImageStream, a_rectangle);
pdfDocument.Save(signedFilesDirectory + "\\" + file.Name);