Quantcast

How to reduce size of PDF with form fields ?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to reduce size of PDF with form fields ?

Mathieu
Hi,
i'm willing to reduce the size of my PDF (generated with iText-1.3 on Java). I'm using existing PDF template file with internal fields (form). I fill them using AcroFields, with a PdfStamper.
My final PDF weighs about 350Ko.

I tryied the following code to compress the file size :
<code>
final PdfStamper stamper = new PdfStamper(reader, baos, PdfWriter.VERSION_1_5, false);
[...]
stamper.setFormFlattening(true);
stamper.setFullCompression();
stamper.close();
</code>
But i only gain about 10Ko less on my final file.

Then i tested these lines to remove unused objects from my PDF file :
<code>
stamper.getReader().removeAnnotations();
stamper.getReader().removeFields();
stamper.getReader().removeUnusedObjects();
</code>
Here, i managed to reduce the file size by 200Ko (file size: 130Ko - Great!). But the down side is that my file isn't well formed anymore. I've got an error from my PDF viewer when opening the file (Acrobat reader says "This file contains an error..."), and the text shown in the PDF contains bad encoded characters (the text is in French and contains some accents ('È ' instead of 'é', '‡' instead of 'à').

My question is: Is there any way to reduce the size of a PDF file without corrupting it ?
Any help would be appreciated. I really need to decrease the size of these generated PDF files. I tryied to print my PDF into another PDF using PDFCreator (just for test) and the size decreased from 350Ko to 244Ko without any loss in the PDF file. So i think there have to be a way to do so with iText, isn't it?

Thanks!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to reduce size of PDF with form fields ?

Mark Storer-2
Welcome to the 21st century.  You /really/ need to get a more recent version of iText.  The bug you've encountered in 1.3 has certainly been fixed long ago.

The current version is 5.0.6.  Note that the packages AND LICENSE have changed.  Everything is now in "com.itextpdf.*", and licensed under the AGPL.  The AGPL requires that anyone with access to the output of the program likewise have access to the source.  With iText, that means that anyone who can download a PDF should be able to download the source.

There's also commercial licensing available through itextsoftware.com.  

PS: the last version to use the MPL/LGPL was 2.1.7, which you can still find if you look hard enough.

PPS: Acrobat Pro (you can download a timed free trial if need be) can run a Space Audit on your file to see what's taking up all your space.  Knowing that can save you quite a bit of effort.  Premature optimizations and all that.

--Mark Storer
  Senior Software Engineer
  Cardiff.com
 
import legalese.Disclaimer;
Disclaimer<Cardiff> DisCard = null;
 
 

> -----Original Message-----
> From: Mathieu [mailto:[hidden email]]
> Sent: Tuesday, March 15, 2011 7:09 AM
> To: [hidden email]
> Subject: [iText-questions] How to reduce size of PDF with
> form fields ?
>
> Hi,
> i'm willing to reduce the size of my PDF (generated with
> iText-1.3 on Java).
> I'm using existing PDF template file with internal fields
> (form). I fill them using AcroFields, with a PdfStamper.
> My final PDF weighs about 350Ko.
>
> I tryied the following code to compress the file size :
>
> final PdfStamper stamper = new PdfStamper(reader, baos,
> PdfWriter.VERSION_1_5, false); [...]
> stamper.setFormFlattening(true);
> stamper.setFullCompression(); stamper.close();
>
> But i only gain about 10Ko less on my final file.
>
> Then i tested these lines to remove unused objects from my PDF file :
>
> stamper.getReader().removeAnnotations();
> stamper.getReader().removeFields();
> stamper.getReader().removeUnusedObjects();
>
> Here, i managed to reduce the file size by 200Ko (file size:
> 130Ko - Great!). But the down side is that my file isn't well
> formed anymore. I've got an error from my PDF viewer when
> opening the file (Acrobat reader says "This file contains an
> error..."), and the text shown in the PDF contains bad
> encoded characters (the text is in French and contains some
> accents ('È ' instead of 'é', '‡' instead of 'à').
>
> My question is: Is there any way to reduce the size of a PDF
> file without corrupting it ?
> Any help would be appreciated. I really need to decrease the
> size of these generated PDF files. I tryied to print my PDF
> into another PDF using PDFCreator (just for test) and the
> size decreased from 350Ko to 244Ko without any loss in the
> PDF file. So i think there have to be a way to do so with
> iText, isn't it?
>
> Thanks!
>
> --
> View this message in context:
> http://itext-general.2136553.n4.nabble.com/How-to-reduce-size-
> of-PDF-with-form-fields-tp3356683p3356683.html
> Sent from the iText - General mailing list archive at Nabble.com.
>
> --------------------------------------------------------------
> ----------------
> Colocation vs. Managed Hosting
> A question and answer guide to determining the best fit for
> your organization - today and in the future.
> http://p.sf.net/sfu/internap-sfd2d
> _______________________________________________
> iText-questions mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
> iText(R) is a registered trademark of 1T3XT BVBA.
> Many questions posted to this list can (and will) be answered
> with a reference to the iText book:
> http://www.itextpdf.com/book/ Please check the keywords list
> before you ask for examples: http://itextpdf.com/themes/keywords.php
>

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to reduce size of PDF with form fields ?

jagadeesh.1221@gmail.com
Hello Mark,

Could you please provide the peace of code that solves the issue.
because im using the latest version of ITEXT. still getting the same
problem...





------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58041391&iu=/4140/ostg.clktrk
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
mkl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to reduce size of PDF with form fields ?

mkl
In reply to this post by Mathieu
Mathieu,

doing
stamper.getReader().removeAnnotations();
stamper.getReader().removeFields();
stamper.getReader().removeUnusedObjects();
[...]
stamper.setFormFlattening(true);
stamper.setFullCompression();
stamper.close();
is a bad idea because you at the same time tell the stamper to eventually flatten a form and remove the information concerning the form fields. Thus, no wonder you get a corrupt document after the form is flattened based on removed objects.

I would propose to do all that in two steps, first stamp with form flattening and store the result (in memory if possible), then read it again, remove unused objects in a second stamping.

The background: PdfReader.removeUnusedObjects can only remove objects unused in the PDF as present then in the reader, but the form flattening happens during stamping, actually at the very end of it, while closing the stamper.

Regards,   Michael
Loading...