Hello

i solve the problem...

when i replace the image i have to use the following method:

stream.setData(replaceImage.getOriginalData(),false,PRStream.NO_COMPRESSION);

where replaceImage is a instance of the itext image class.

now it works.

with regards

Thomas Kübler


the code:

                final PdfImageObject image = new PdfImageObject(stream);
                
                try
                        {
                        byte[] replaceImageData = IOUtils.toByteArray(new 
FileInputStream("replacecmyk.jpg"));
                        Image replaceImage      = 
Image.getInstance(replaceImageData);
                        
                        stream.clear();
                        
stream.setData(replaceImage.getOriginalData(),false,PRStream.NO_COMPRESSION);
                        
                        stream.put(PdfName.TYPE, PdfName.XOBJECT);              
                
                        stream.put(PdfName.SUBTYPE, PdfName.IMAGE);             
                                                                
                        // only use jpeg files
                        stream.put(PdfName.FILTER, PdfName.DCTDECODE);          
                
                        stream.put(PdfName.WIDTH, new 
PdfNumber(replaceImage.getWidth()));                      
                        stream.put(PdfName.HEIGHT, new 
PdfNumber(replaceImage.getHeight()));                    
                        stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)); 
                
                        
                        
                        switch (replaceImage.getColorspace())
                                {
                                case 1:
                                        stream.put(PdfName.COLORSPACE, 
PdfName.DEVICEGRAY);
                                        break;
                                case 3:
                                        stream.put(PdfName.COLORSPACE, 
PdfName.DEVICERGB);
                                        break;
                                default:
                                        stream.put(PdfName.COLORSPACE, 
PdfName.DEVICECMYK);
                                        if (replaceImage.isInverted())
                                                {
                                                stream.put(PdfName.DECODE, new 
PdfLiteral("[1 0 1 0 1 0 1 0]"));
                                                }
                                }
                        
                        
                        
                        }
                catch (BadElementException e)
                        {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        }


Am 26.09.2012 um 14:30 schrieb Thomas Kübler <[email protected]>:

> Hello to itext user,
> 
> i have a problem to replace an jpeg image inside an existing pdf document.
> 
> First i check if an xobject is inside the pdf document, if i found one i get 
> the PRStream from this object.
> Then i create an PdfImageObject read the data and save this to the filesystem 
> (this image i can open with photoshop etc.)
> Then i clear the prstream and read the save image to replace (for test i use 
> the image saved before, so this is exact the same image),
> i do all necessary settings like before, but the saved PDF document doesn't 
> show the image. 
> 
> I got an error from acrobat "not enough memory" and when i try to open this 
> saved image i got an error from photoshop "wrong jpeg mark found".
> 
> any idea's?
> 
> 
> 
> here is the code snippet:
> 
> stream.clear();
> 
> stream.put(PdfName.TYPE, PdfName.XOBJECT);
> stream.put(PdfName.SUBTYPE, PdfName.IMAGE);
> stream.put(PdfName.WIDTH, new PdfNumber(replaceImage.getWidth()));
> stream.put(PdfName.HEIGHT, new PdfNumber(replaceImage.getHeight()));
> 
> 
> switch (replaceImage.type())
>       {
>       case Image.JPEG:
>                               
>       switch (replaceImage.getColorspace())
>               {
>               case 1:
>               stream.put(PdfName.COLORSPACE, PdfName.DEVICEGRAY);
>               break;
>               case 3:
>               stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB);
>               break;
>               default:
>               stream.put(PdfName.COLORSPACE, PdfName.DEVICECMYK);
>                                               
>               if (replaceImage.isInverted())
>                       {
>                       stream.put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0 
> 1 0]"));
>                       }
>               }
>               stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
>               stream.put(PdfName.LENGTH, new PdfNumber(image.length));
>                               
>               stream.setData(image);
>                               
>               stream.put(PdfName.FILTER, PdfName.DCTDECODE);
> }
> 
> with regards
> 
> Thomas Kübler
>                               
> 
> 
> 
> 
> 
> 
> 
> 
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and 
> threat landscape has changed and how IT managers can respond. Discussions 
> will include endpoint security, mobile security and the latest in malware 
> threats. 
> http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________
> 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

Mit freundlichen Grüßen

Thomas Kübler

----------------------------------------------------

Dynamic Document Solution variex
Alt-Marienfelde 25
12277 Berlin

t. + 49 30 723 23 183
f. + 49 30 723 23 185

mailto:[email protected]
www.variex.de





------------------------------------------------------------------------------
Got visibility?
Most devs has no idea what their production app looks like.
Find out how fast your code is with AppDynamics Lite.
http://ad.doubleclick.net/clk;262219671;13503038;y?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html
_______________________________________________
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

Reply via email to