views:

1770

answers:

1

I have a bunch of PDFs that I'm just trying to open, resize the page, and then save. I'm also hoping that the file sizes will shrink significantly doing so. I am using iTextSharp and the resizing works just fine, but the file size is nearly identical, ever so slightly larger in fact. Here's the function I have now:

    Dim reader As New PdfReader(inPDF)
    Dim doc As New Document(PageSize.LETTER)
    Document.Compress = True
    Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(outPDF, FileMode.Create))
    doc.Open()
    Dim cb As PdfContentByte = writer.DirectContent

    Dim page As PdfImportedPage
    For pageNumber As Long = 1 To reader.NumberOfPages
        page = writer.GetImportedPage(reader, pageNumber)
        cb.AddTemplate(page, PageSize.LETTER.Width / reader.GetPageSize(pageNumber).Width, 0, 0, PageSize.LETTER.Height / reader.GetPageSize(pageNumber).Height, 0, 0)
        doc.NewPage()
    Next pageNumber
    doc.Close()

Does anyone know what I may be missing to actually get the file size down as well?

Thanks.

+1  A: 

Reducing the size of the page just reduces the size of the page; you are not removing any content, so the file size will not change.

There are three ways to reduce the file size; remove content, make sure that text objects are compressed, and/or make sure the images are efficient.

By efficient images, I mean that images are not bigger than they need to be, and that the right kind of compression is used. You can use a huge image, say 1800 pixels wide, and scale it to be 2.5" in the pdf. If you do this, it will still be 1800 pixels wide even though it is displays as 2.5 inches wide. You can reduce the file size by resizing the image - in this case, for 300 dpi print resolution, it only needs to be 750 pixels wide. For standard pdf 72 dpi resolution, you would only need an image 202 pixels wide.

Many programs which generate pdfs automatically use jpegs. If the image is 2 color, like an invoice or check scan, using tiffs with G4 compression will make a much smaller file size than a jpeg.

R Ubben
I'm going to extrapolate from here and say that to achieve my goal then, I need to extract the images and then manipulate that and put those back together into a new PDF. Luckily you helped address that problem here: http://stackoverflow.com/questions/802269/itextsharp-extract-images
Ryan
Yes. You'll have to be careful not to disturb the formatting.
R Ubben