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? &nbsp;Thank you.<br=
 /><br />&nbsp; &nbsp; &nbsp; &nbsp; Dim reader =3D New iTextSharp.text.pdf=
.PdfReader(sourcePdf)<br />&nbsp; &nbsp; &nbsp; &nbsp; Dim pdfStamper =3D N=
ew PdfStamper(reader, New FileStream(newFile, FileMode.Create))<br />&nbsp;=
 &nbsp; &nbsp; &nbsp; Dim writer =3D pdfStamper.Writer<br /><br />&nbsp; &n=
bsp; &nbsp; &nbsp; Dim page_count As Integer =3D reader.NumberOfPages<br />=
&nbsp; &nbsp; &nbsp; &nbsp; Dim i As Integer =3D 1<br />&nbsp; &nbsp; &nbsp=
; &nbsp; Do While (i &lt;=3D page_count)<br />&nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; Dim pg As PdfDictionary =3D reader.GetPageN(i)<br />&nbsp; &nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp; Dim res As PdfDictionary =3D CType(PdfReade=
r.GetPdfObject(pg.Get(PdfName.RESOURCES)), PdfDictionary)<br />&nbsp; &nbsp=
; &nbsp; &nbsp; &nbsp; &nbsp; Dim xobj As PdfDictionary =3D CType(PdfReader=
.GetPdfObject(res.Get(PdfName.XOBJECT)), PdfDictionary)<br />&nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; If (Not (xobj) Is Nothing) Then<br />&nbsp; &nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; For Each name As PdfName In x=
obj.Keys<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp=
; &nbsp; Dim obj As PdfObject =3D xobj.Get(name)<br />&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If obj.IsIndirect Then<br =
/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nb=
sp; &nbsp; Dim tg As PdfDictionary =3D CType(PdfReader.GetPdfObject(obj), P=
dfDictionary)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; Dim type As PdfName =3D CType(PdfReader.GetPdfO=
bject(tg.Get(PdfName.SUBTYPE)), PdfName)<br />&nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If PdfName.IMAGE.Equ=
als(type) Then<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If tg.GetAsNumber(pdf.PdfName.WI=
DTH).IntValue =3D 61 Then<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim xre=
fIdx As Integer =3D CType(obj, PRIndirectReference).Number<br />&nbsp; &nbs=
p; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; Dim pdfObj As PdfObject =3D reader.GetPdfObject(x=
refIdx)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim maskImage As Image =
=3D Image.ImageMask<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (Not (mask=
Image) Is Nothing) Then<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbs=
p; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; writer.AddDirectImageSimple(maskImage)<br />&nbsp; &nbsp; &nbsp; &nbsp=
; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nb=
sp; &nbsp; End If<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PdfReader.KillI=
ndirect(obj)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.AddDirectImag=
eSimple(Image, obj)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit For<br /=
>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbs=
p; &nbsp; &nbsp; &nbsp; End If<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbs=
p; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />&nbsp; &nbsp; &nbs=
p; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br /><br />&nbsp=
; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Next<br />&nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; End If<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; i =3D (i + 1)<br />&nbsp; &nbsp; &nbsp; &nbsp; Loop<br /><br /=
>&nbsp; &nbsp; &nbsp; &nbsp; pdfStamper.Writer.CloseStream =3D False<br />&=
nbsp; &nbsp; &nbsp; &nbsp; pdfStamper.Close()<br />&nbsp; &nbsp; &nbsp; &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==--