Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a pdf using iTextSharp

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.

like image 911
Ryan Avatar asked Nov 05 '22 20:11

Ryan


1 Answers

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.

like image 172
R Ubben Avatar answered Nov 14 '22 21:11

R Ubben