Replace images in pdf
[email protected] Mon, 21 Dec 2015 13:42:22 -0600 (CST)
| Newsgroups | gmane.comp.java.lib.itext.general |
|---|---|
| Message-ID | <14264189.376131.1450726942922.JavaMail.root@vznit170080.mailsrvcs.net> |
--===============0627427964381987197== Content-type: text/html; charset=UTF-8 Content-transfer-encoding: quoted-printable <div style=3D"FONT-FAMILY: Arial; COLOR: #000000; FONT-SIZE: 12px">I am try= ing to replace a particular image on several pages in a pdf. It runs throug= h the code fine but when I open the file I see the first image replaced and= when I scroll to the second page it says there is an error in the pdf. &nb= sp;Can anyone help me figure out what I am doing wrong? Thank you.<br= /><br /> Dim reader =3D New iTextSharp.text.pdf= .PdfReader(sourcePdf)<br /> Dim pdfStamper =3D N= ew PdfStamper(reader, New FileStream(newFile, FileMode.Create))<br /> = Dim writer =3D pdfStamper.Writer<br /><br /> &n= bsp; Dim page_count As Integer =3D reader.NumberOfPages<br />= Dim i As Integer =3D 1<br />  = ; Do While (i <=3D page_count)<br /> &= nbsp; Dim pg As PdfDictionary =3D reader.GetPageN(i)<br /> &nb= sp; Dim res As PdfDictionary =3D CType(PdfReade= r.GetPdfObject(pg.Get(PdfName.RESOURCES)), PdfDictionary)<br />  = ; Dim xobj As PdfDictionary =3D CType(PdfReader= .GetPdfObject(res.Get(PdfName.XOBJECT)), PdfDictionary)<br /> = If (Not (xobj) Is Nothing) Then<br /> &nb= sp; For Each name As PdfName In x= obj.Keys<br />  = ; Dim obj As PdfObject =3D xobj.Get(name)<br /> = If obj.IsIndirect Then<br = /> &nb= sp; Dim tg As PdfDictionary =3D CType(PdfReader.GetPdfObject(obj), P= dfDictionary)<br /> = Dim type As PdfName =3D CType(PdfReader.GetPdfO= bject(tg.Get(PdfName.SUBTYPE)), PdfName)<br /> &= nbsp; If PdfName.IMAGE.Equ= als(type) Then<br /> = If tg.GetAsNumber(pdf.PdfName.WI= DTH).IntValue =3D 61 Then<br /> &n= bsp; Dim xre= fIdx As Integer =3D CType(obj, PRIndirectReference).Number<br /> &nbs= p; &n= bsp; Dim pdfObj As PdfObject =3D reader.GetPdfObject(x= refIdx)<br /> = Dim maskImage As Image = =3D Image.ImageMask<br /> &= nbsp; If (Not (mask= Image) Is Nothing) Then<br /> &nbs= p; &n= bsp; writer.AddDirectImageSimple(maskImage)<br />  = ; &nb= sp; End If<br /> &nb= sp; PdfReader.KillI= ndirect(obj)<br /> &= nbsp; writer.AddDirectImag= eSimple(Image, obj)<br /> &= nbsp; Exit For<br /= > &nbs= p; End If<br /> &nbs= p; End If<br /> &nbs= p; End If<br /><br /> = ; Next<br /> = End If<br /><br /> &= nbsp; i =3D (i + 1)<br /> Loop<br /><br /= > pdfStamper.Writer.CloseStream =3D False<br />&= nbsp; pdfStamper.Close()<br /> &nb= sp; reader.Close()=09=09=09=09</div> --===============0627427964381987197== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------------ --===============0627427964381987197== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ iText-questions mailing list [email protected] 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 --===============0627427964381987197==--