Hi,

 

I have a Pdf that is the result of some sort "mail merge". While the Pdf
is correct, it is huge in size because every item in the merge contains
the same high resolution images and embedded fonts. I'm hoping to use
iText (the .Net version) to alter the Pdf file so each image's byte
content is only present once in the file. 

To achieve this I thought I'd loop through the pages and add every
unique (based on an md5 checksum of the image's content bytes) image to
the body of the pdf. This seems to work as the file shrinks to the size
that I would expect, but how do I alter the Pdf to reference the Image
from the body of the Pdf?

 

I've tried several things, but until now I've only been able to create
(corrupt) PDF's which don't display the images. Any help is greatly
appreciated!

 

Thanks in advance,

 

Koen

 

 

What I've come up with so far is (based on the FAQ about replacing
images (How to replace images in a PDF?
<http://itext.ugent.be/library/question.php?id=66>  )):

 

private void ReplaceDuplicateImages()

    {

        PdfReader pdf = new PdfReader(@"C:\Data\TEMP\BulkMergePdf.pdf");

        PdfStamper stp = new PdfStamper(pdf, new
FileStream(@"C:\Data\TEMP\BulkMergePdf_Optimized.pdf",
FileMode.Create));

        PdfWriter writer = stp.Writer;

 

        MD5 md5 = new MD5CryptoServiceProvider();

        ASCIIEncoding enc = new ASCIIEncoding();

        Dictionary<String, PdfIndirectObject> dictImg = new
Dictionary<string, PdfIndirectObject>();

 

        try

        {

            int intPageNum = pdf.NumberOfPages;

            for (int i = 1; i <= intPageNum; i++)

            {

                PdfDictionary pg = pdf.GetPageN(i);

                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))

                            {

                                // Get the checksum for this image

                                String strMD5 =
enc.GetString(md5.ComputeHash(PdfReader.GetStreamBytesRaw((PRStream)PdfR
eader.GetPdfObject(obj))));

 

                                // See if we already have the Image in
our own dictionary

                                if (!dictImg.ContainsKey(strMD5))

                                {

                                    PdfIndirectObject objRef =
writer.AddToBody(PdfReader.GetPdfObject(obj));

                                    dictImg.Add(strMD5, objRef);

                                }

 

                                // Remove the original image data from
the Pdf

                                PdfReader.KillIndirect(obj);

 

                                // Get the PdfIndirectObject from our
dictionary

                                PdfIndirectObject
objPdfIndirectObjectFromDictionary = dictImg[strMD5];

                                

                                // Now what???

                                

                            }

                        }

                    }

                }

            }

            stp.Close();

 

        }

        catch (Exception)

        {

            if (pdf != null)

                pdf.Close();

            if (stp != null)

                stp.Close();

 

            throw;

        }

    }

------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to