Current code replace all images in all pages, i need replace one image in specific page
thanks
My code
//Source pdf
PdfReader pdf = new PdfReader(@\\file-01\);
//destination pdf
PdfStamper stp = new PdfStamper(pdf, new FileStream(E:\CMMS));
PdfWriter writer = stp.Writer;
//red text (Mz.083mDD)to find the specific page that content image
int pageNo = pdf.XrefSize;
for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentPageText = PdfTextExtractor.GetTextFromPage(pdf, pageNumber, strategy);
if (currentPageText.Contains("Mz.083mDD"))
{
Session["page_location"] = pageNumber;
}
}
int pageNumber1 = (int)Session["page_location"];
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(@"E:\CMMS\143.gif");
PdfDictionary pg = pdf.GetPageN(pageNumber1);
PdfDictionary res =
(PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
PdfDictionary xobj =
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
if (xobj != null)
{
foreach (PdfName name in xobj.Keys)
{
PdfObject obj = xobj.Get(name);
if (obj.IsIndirect())
{
PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
PdfName type =
(PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
if (PdfName.IMAGE.Equals(type))
{
PdfReader.KillIndirect(obj);
iTextSharp.text.Image maskImage = img.ImageMask;
if (maskImage != null)
writer.AddDirectImageSimple(maskImage);
writer.AddDirectImageSimple(img, (PRIndirectReference)obj);
break;
}
}
}
}