I know this is an old post but I wanted to post the way I got this to work. No one posting was very helpful.
This is my code using ItextSharp. I hope it's helpful to someone. I needed to move an existing barcode on the page. Instead what you do is copy the barcode, delete the existing barcode and place a copy back on the page at an absolute position. public class BarcodeMover { public void MoveDuploBarcode(string inputfile, string outputfile) { using (FileStream outputstreampdf = new FileStream(outputfile, FileMode.Create)) { using (PdfReader inputstreampdf = new PdfReader(inputfile)) { PdfStamper pdfstamper = new PdfStamper(inputstreampdf, outputstreampdf); for (int pagenum = 1; pagenum <= inputstreampdf.NumberOfPages; pagenum++) { if (IsEven(pagenum)) { using (MemoryStream pagememorystream = new MemoryStream()) { PdfDictionary pagexobjects = GetAllXObjectsDictionaryFromPage(inputstreampdf, pagenum); PdfContentParser pagecontentparser = GetContentParserForPage(inputstreampdf, pagenum); PdfName barcodestreamobject = null; while (true) { List<PdfObject> currentstreamobjects = GetNextSectionOfContent(pagecontentparser); if (currentstreamobjects.Count == 0) { break; } bool ismatrixbarcode = DoesStreamContainMatrixBarcode(currentstreamobjects, pagexobjects); if (ismatrixbarcode) { barcodestreamobject = (PdfName)currentstreamobjects.First(); } else { WriteToMemoryStream(currentstreamobjects, pagememorystream); } } if (barcodestreamobject != null) { PdfObject barcodeobject = pagexobjects.Get((PdfName)barcodestreamobject); PdfDictionary xobjectdictionary = (PdfDictionary)PdfReader.GetPdfObject(barcodeobject); int xrefIdx = ((PRIndirectReference)barcodeobject).Number; PdfObject pdfObj = inputstreampdf.GetPdfObject(xrefIdx); PdfStream streamobject = (PdfStream)pdfObj; byte[] imagestream = PdfReader.GetStreamBytesRaw((PRStream)streamobject); PdfReader.KillIndirect(barcodeobject); ImgCCITT timg = BuildNewImage(xobjectdictionary, imagestream, inputstreampdf, pagenum); PlaceNewImageOnPage(pdfstamper, pagenum, pagememorystream, timg); barcodestreamobject = null; } } } } pdfstamper.Close(); } } } private List<PdfObject> GetNextSectionOfContent(PdfContentParser pagecontentparser) { return pagecontentparser.Parse(null); } private bool IsEven(int pagenum) { return pagenum % 2 == 0; } private void WriteToMemoryStream(List<PdfObject> pagecontentobjects, MemoryStream memoryStream) { foreach (PdfObject o in pagecontentobjects) { o.ToPdf(null, memoryStream); memoryStream.WriteByte((byte)'\n'); } } private PdfDictionary GetAllXObjectsDictionaryFromPage(PdfReader pdfreader, int pagenum) { PdfDictionary pagedictionary = pdfreader.GetPageN(pagenum); PdfDictionary pageresources = (PdfDictionary)PdfReader.GetPdfObject(pagedictionary.Get(PdfName.RESOURCES)); return (PdfDictionary)PdfReader.GetPdfObject(pageresources.Get(PdfName.XOBJECT)); } private PdfContentParser GetContentParserForPage(PdfReader pdfReader, int pagenum) { byte[] pagecontentstream = pdfReader.GetPageContent(pagenum); return new PdfContentParser(new PRTokeniser(new RandomAccessFileOrArray(pagecontentstream))); } private void PlaceNewImageOnPage(PdfStamper pdfStamper, int i, MemoryStream memoryStream, ImgCCITT timg) { pdfStamper.Reader.SetPageContent(i, memoryStream.GetBuffer()); pdfStamper.GetOverContent(i).AddImage(timg); } private ImgCCITT BuildNewImage(PdfDictionary tg, byte[] bytes, PdfReader pdfReader, int i) { double width = Convert.ToInt32(tg.Get(PdfName.WIDTH).ToString()); double height = Convert.ToInt32(tg.Get(PdfName.HEIGHT).ToString()); ImgCCITT timg = new ImgCCITT((int)width, (int)height, false, ImgCCITT.CCITTG4, ImgCCITT.CCITT_ENDOFBLOCK, bytes); timg.ScaleToFit(24, 24); timg.SetAbsolutePosition(0, pdfReader.GetPageSize(i).Top - 140); return timg; } private bool DoesStreamContainMatrixBarcode(List<PdfObject> contentobjects, PdfDictionary pagexobjects) { if ("Do".Equals(contentobjects.Last().ToString()) && contentobjects.First().ToString().Contains("img")) { PdfObject possibleobject = pagexobjects.Get((PdfName)contentobjects.First()); if (possibleobject.IsIndirect()) { if (possibleobject != null) { PdfDictionary xobjectdictionary = (PdfDictionary)PdfReader.GetPdfObject(possibleobject); PdfName type = (PdfName)PdfReader.GetPdfObject(xobjectdictionary.Get(PdfName.SUBTYPE)); if (PdfName.IMAGE.Equals(type)) { if (xobjectdictionary.Get(PdfName.FILTER).ToString() == "/CCITTFaxDecode") { return true; } } } } } return false; } } -- View this message in context: http://itext.2136553.n4.nabble.com/Replacing-and-removing-images-tp2165841p4660872.html Sent from the iText mailing list archive at Nabble.com. ------------------------------------------------------------------------------ _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php