How to downscale all images in a PDF

Manipulate PDF, Shapes, Images, Manipulate PDF, Shapes
1/23/2014

Downloads

This code sample shows how to downscale images in a PDF file. In order to downscale the images, we extract all graphical content as a collection of shapes. We then replace each ImageShape in this collection with a downscaled copy. Next, the result is written back to a new PDF file.

static void Main(string[] args)
{
    using (FileStream fileIn = new FileStream(@"..\..\..\inputDocuments\BlueWater.pdf", FileMode.Open, FileAccess.Read))
    {
        Document pdfIn = new Document(fileIn);

        //downscale all images in the document to 18 dots per inch
        int dpi = 10;
        Document pdfOut = new Document();

        foreach (Page page in pdfIn.Pages)
        {
            ShapeCollection shapes = page.CreateShapes();
            //downscale all imageshapes on this page
            downScaleImages(shapes, dpi);

            //add all shapes to the new document
            Page newPage = new Page(page.Width, page.Height);
            newPage.Overlay.Add(shapes);
            pdfOut.Pages.Add(newPage);
        }

        using (FileStream fileOut = new FileStream(@"..\..\out.pdf", FileMode.Create, FileAccess.Write))
        {
            pdfOut.Write(fileOut);
        }
    }
}

static void downScaleImages(ShapeCollection shapes, int dpi)
{
    for (int i = 0; i < shapes.Count; i++)
    {
        Shape shape = shapes[i];

        if (shape is ShapeCollection)
        {
            // recurse
            downScaleImages(shape as ShapeCollection, dpi);
        }
        else if (shape is ImageShape)
        {
            shapes.RemoveAt(i);
            ImageShape downScaled = downScale(shape as ImageShape, dpi);
            shapes.Insert(i, downScaled);
        }
    }
}

The following code snippet shows how we resample a given ImageShape.

static ImageShape downScale(ImageShape image, int dpi)
{
    Matrix matrix = image.Transform.CreateGdiMatrix();
    PointF[] points = new PointF[] 
    {
            new PointF(0, 0), 
            new PointF((float)image.Width, 0),
            new PointF(0, (float)image.Height) 
    };
    matrix.TransformPoints(points);

    // real dimensions of the image in points as it appears on the page
    float realWidth = distance(points[0], points[1]);
    float realHeight = distance(points[0], points[2]);

    // given the desired resolution, these are the desired number of cols/rows of the optimized image
    int desiredColumns = (int)(realWidth * ((float)dpi / 72f));
    int desiredRows = (int)(realHeight * ((float)dpi / 72f));

    // create the new image and copy the source image to it (resampling happens here)
    using (Bitmap bitmap = image.CreateBitmap())
    {
        if (desiredColumns > bitmap.Width) return image; // prevent upscale
        if (desiredRows > bitmap.Width) return image; // prevent upscale

        Bitmap optimizedBitmap = new Bitmap(desiredColumns, desiredRows, PixelFormat.Format32bppArgb);

        //draw the image so the pixels can be resampled
        using (Graphics graphics = Graphics.FromImage(optimizedBitmap))
        {
            graphics.DrawImage(bitmap, 0, 0, desiredColumns, desiredRows);
        }

        //create new imageshape and keep all of the settings the same
        ImageShape optimized = new ImageShape(optimizedBitmap, true);
        optimized.Compression = Compression.Jpeg;
        optimized.Width = image.Width;
        optimized.Height = image.Height;
        optimized.Transform = image.Transform;

        optimized.Opacity = image.Opacity;
        optimized.BlendMode = image.BlendMode;
        optimized.Transform = image.Transform;

        return optimized;
    }
}

static float distance(PointF a, PointF b)
{
    return (float)Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
}