Hi,
I am using ASPOSE.PDF to replace text with an image into PDF. I have a situation where I need to insert different images on one PDF page. The code below is only inserting one image on a given page. Can you see what’s wrong here??
using (FileStream outputStream = new FileStream(@outputFileName, FileMode.Create))
using (FileStream signatureImageStream = new FileStream(@signatureFile, FileMode.Open))
using (Document pdfDocument = new Document(inputFileName))
{
foreach (Page page in pdfDocument.Pages)
{
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchText);
//Console.WriteLine("!!!Create TextAbsorber object to find all instances of the input search phrase!!!!");
page.Accept(textFragmentAbsorber);
//Console.WriteLine("!!!Accept the absorber for all the pages!!!!");
// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
if (textFragmentCollection.Count == 0)
{
page.Dispose();
continue;
}
int textFragmentCount = 0;
foreach (TextFragment textFragment in textFragmentCollection)
{
textFragment.Text = String.Empty;
textFragmentCount += 1;
//Console.WriteLine("Text Fragment Count: {0}", textFragmentCount);
// Set coordinates
int lowerLeftX = (int)textFragment.Position.XIndent;
int lowerLeftY = (int)textFragment.Position.YIndent;
int upperRightX = (int)textFragment.Position.XIndent + 100;
int upperRightY = (int)textFragment.Position.YIndent + 25;
Rectangle rectangle = new Rectangle(
lowerLeftX,
lowerLeftY,
upperRightX,
upperRightY
);
Matrix matrix = new Matrix(new double[] {
rectangle.URX - rectangle.LLX,
0,
0,
rectangle.URY - rectangle.LLY,
rectangle.LLX, rectangle.LLY
});
page.Resources.Images.Add(signatureImageStream);
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
XImage ximage = page.Resources.Images[page.Resources.Images.Count];
page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
}
}
pdfDocument.Save(outputStream);
Thank you