2

I'm trying to go through all the images in a PDF and resize them to reduce the PDF file size. I'm using iTextSharp in C#. Below is my code so far. When I look at the output file none of the images were modified.

PdfReader pdf = new PdfReader(input);
using (PdfStamper stp = new PdfStamper(pdf, output))
{
    for (int i = 1; i <= pdf.NumberOfPages; i++)
    {
        PdfDictionary page = pdf.GetPageN(i);
        PdfDictionary resources = (PdfDictionary)PdfReader.GetPdfObject(page.Get(PdfName.RESOURCES));
        PdfDictionary xObjects = (PdfDictionary)PdfReader.GetPdfObject(resources.Get(PdfName.XOBJECT));

        if (xObjects == null)
            continue;

        foreach (PdfName name in xObjects.Keys)
        {
            PdfObject xObj = xObjects.Get(name);
            if (!xObj.IsIndirect())
                continue;

            //Filter non-images
            PdfDictionary xObjDic = (PdfDictionary)PdfReader.GetPdfObject(xObj);
            PdfName xObjType = (PdfName)PdfReader.GetPdfObject(xObjDic.Get(PdfName.SUBTYPE));
            if (!PdfName.IMAGE.Equals(xObjType))
                continue;

            //Get embedded image  
            int refId = ((PRIndirectReference)xObj).Number;
            PRStream objStream = (PRStream)pdf.GetPdfObject(refId); 
            PdfImageObject objImg = new PdfImageObject(objStream);                           
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(objImg.GetImageAsBytes());

            //Resize
            img.ScaleAbsolute(200, 200);
            img.SetDpi(72, 72);

            PdfReader.KillIndirect(xObj);
            stp.Writer.AddDirectImageSimple(img, (PRIndirectReference)xObj);
            break;
        }
    }
}

pdf.Close();
prestomanifesto
  • 12,528
  • 5
  • 34
  • 50
  • You look like you're modifying the image files. Have you tried modifying the image placement information instead? I expect PDF will scale the image to the size you want without having to change the files. – Rup Feb 14 '12 at 17:15
  • Well the point is to reduce the PDF file size – prestomanifesto Feb 14 '12 at 17:23
  • I see, know of any good examples? – prestomanifesto Feb 14 '12 at 18:06
  • Actually on reflection that's wrong - sorry. I looked at some old code that used PDFCopy but it was doing something else. I don't have an example of modifying a PDF page, no. – Rup Feb 14 '12 at 18:19
  • In looking at the Image API in iText, it doesn't look like scale does anything but change the transformation matrix used for the image. It's not clear that iText# actually resamples the data. – plinth Feb 14 '12 at 19:55
  • Yeah it looks like scaling is only applied when you add the image to a new document... – prestomanifesto Feb 14 '12 at 20:11
  • 3
    iTextSharp will never modify your images for you, ever. Any scaling, transforms, etc. affect what an image looks like when rendered only. If you want to compress images you'll need to extract the original, compress the image however you want and then delete the old image from the PDF and replace it with your new image. See this link for a full-working solution. http://stackoverflow.com/a/8751517/231316 – Chris Haas Feb 14 '12 at 20:54

0 Answers0