How to compress a pdf file in java?

I have a program which will convert a Tiff image(900kb) to a PDF File(250-300KB), I want the pdf file to be compressed least(80-100KB) , inorder to transmit the file to the server and save it there,
I have tried using java.util.zip outputStreams its not doing so,
Why it is?
Or Give a alternate Solution for it?
Or Is there any free software that do it for me?
Please do suggest !

This is my code where I am passing a .tiff file and getting the .pdf file as output.
Now I want to reduce the output PDF File size.
public void convertTiff2PDf(String tiff_file)
               String      pdf_file = tiff_file.substring(0, tiff_file.lastIndexOf(".") + 1) + "pdf";
               Document document = new Document();
               try
                    PdfWriter writer = PdfWriter.getInstance(document,
                              new FileOutputStream(pdf_file));
                    int pages = 0;
                    document.open();
                    PdfContentByte cb = writer.getDirectContent();
RandomAccessFileOrArray ra = null;
int comps = 0;
try {
ra = new RandomAccessFileOrArray(tiff_file);
comps = TiffImage.getNumberOfPages(ra);
catch (Throwable e) {
System.out.println("Exception in " + tiff_file + " " + e.getMessage());
for (int c = 0; c < comps; ++c) {
try {
Image img = TiffImage.getTiffImage(ra, c + 1);
if (img != null) {
System.out.println("page " + (c + 1));
if (img.scaledWidth() > 500 || img.scaledHeight() > 700) {
img.scaleToFit(800, 850);
img.setAbsolutePosition(20, 20);
// document.add(new Paragraph(tiff_file + " - page " + (c + 1)));
cb.addImage(img);
document.newPage();
++pages;
catch (Throwable e) {
System.out.println("Exception " + tiff_file + " page " + (c + 1) + " " + e.getMessage());
                    ra.close();
                    document.close();
               } catch (Throwable e) {
                    e.printStackTrace();
     }

Similar Messages

  • How to compress a PDF file

    I just wanted to know how to compress a PDF file to send via email.

    Try the Colorsync Utility
    ~/Applications/Utilities/ColorSync Utility>Filter
    Reduce file size
    or
    create generic PDFX 3 DOC

  • How to compress a pdf file so I can e-mail

    how to compress a pdf file so I can e-mail it

    On a Windows you can try a program like WinZip or WinRar.

  • How to compress a pdf file without quality loss

    How to compress a pdf file without quality loss? Please help....

    With the way you worded the question, your only option is to zip (or equivalent) the PDF. There are options in terms of graphic compression (this is generally a loss of quality in reducing the pixels) and nonstorage of fonts (likely making the file unreadable to some). So there are techniques, but with a tradeoff. It all depends on what you mean by loss of quality.
    The other techniques that do not lose quality, but give up on some functionality are to remove links, bookmarks, and tags. The tags are often necessary only for converting a PDF back to another form and maintaining accessibility - important in some business situations. These are all tradeoffs that give something up.

  • How to open an PDF file from Java Application

    Hi
    I am developing a GUI application in java swing.
    on clicking one button, I want to open PDF file from my swing application.
    Is there any API through which I can achieve this?
    Tapan Maru
    Sun Certified Java Programmer

    Here's a way to do it (if I understand you
    correctly). Just let explorer.exe do it for you.
    import java.awt.*;
    public class openPDF
         Desktop desktop = Desktop.getDesktop();
         public openPDF()
              open("test.pdf");
         public void open(String path)
              try
                   Runtime.getRuntime().exec("explorer.exe "+path);
    } catch(Exception e) { System.out.println("NAI!
    I! ERROR!\n"+e); }
         public static void main(String[] args)
              openPDF myApplication = new openPDF();
    Why do you have a Desktop object as a member but instead of using it, you execute a command with Runtime (which is not platform independent!)???
    -Puce

  • Urgent !! How can I create pdf file in java ?

    Hello all...Respected sir/mam
    I have written a servlet in which it makes the connectivity with the database(mysql) and then draws the values from the database ..now i want to write that values into a pdf file...I used the FileWriter class and wrote that pdf..Now when I run the application the pdf was created but I was unable to open it directly in Acrobat...I opened it in notepad then it showed me all the data from the database...? What might be the problem ? Is it that my acrobat reader is not correctly installed or some settings need to be made in my application so that it can be read using acrobat reader..?
    Please help me ..
    Thanks ..

    Hi!
    Try using apache fop. If you are familiar with xml and xslt it is fairly easy to write some database records into a table in an pdf file. Have a look at http://xml.apache.org/fop/index.html
    Cheers,
    Steffen

  • How to compress the PDF file?

    Whenever I use Designer to design my forms, the size is almost always bigger than converting from MS Word and use Acrobat to make the form. Is there a way to compress the form so the size is not too big? If so, what are the steps?

    >I guess I'm a little confuse as to Designer form contains both PDF and Acrobat form.
    Sorry, that was confusing. I meant to say, contains BOTH an XML form,
    and a PDF form. The XML form is what Designer is based on; Designer
    forms are ideal if working in an XML workflow. The PDF form is what
    you see on screen, and what prints. Acrobat has to make both copies
    and keep them in sync with each other.
    Aandi Inston

  • How to view a PDF file

    Hey, i was wondering if anyone knew how to view a PDF file in Java? I'm trying to make a button that will open a PDF file. Is there any other way without downloading an extension?

    Kandyman wrote:
    The thing with downloading extensions is that they are not free. Some are trial version. iText and Jasper are probably free and stable to a great extent. Plus they allow you to play with PDFs, Excel, Bar charts, Pie charts, CSV etc etc without the hassles of the heavyweight Swing.
    - Vaibhav Beohar

  • How to read pdf files using java.io package classes

    Dear All,
    I have a certain requirement that i should read and write PDF files at runtime. With normal java file IO reading is not working. Can any one suggest me how to proceed probably with sample code block
    Thanks in advance.

    hi I also have the pbm. to read pdf file using JAVA
    can any body help meWhy is it so difficult to read the thread you posted in? They say: java.io is pointless, use iText. So why don't you?
    or also I want to read a binary encoded data into
    ascii,
    can anybody give me a hint how to do it.Depends on what you mean with "binary encoding". ASCII's binary encoding, too, basically.

  • How to enable commenting into pdf files using java?

    Hi All,
    Is there any way available to enable comments into pdf files through java. I have a Adobe Reader 9 and also I want put some comments into pdf file, but the reader is not allowed to place a comment into pdf file before enabling the Comments into pdf. After enabling the comments in pdf file then only we can place the comments and we can the pdf file with comments.
    Is there any way available to enable comments into pdf file to view acrobat reader.
    Thanks in advance.

    The end user who uses the web application has Acrobat installed in their mahcine needs it only for the purpose of enabling comments in pdf. If this task of enabling comments in pdf is done through my application there is no need for acrobat in such user's machine which was requested as a cost cuttnig measure.
    For this purpose, i need to know about how to enable comments in pdf through java api.
    I have used itext java api for other pdf related manipulation in java. But it does not have feature as per the requirement posted above. Can any body suggest relevant java api to achieve the task requested?
    Tahnks in Advance.

  • How to print PDF files using java print API

    Hi,
    I was goign throw lot of discusion and reading lot of forums related to print pdf files using java api. but nothing seems to be working for me. Can any one tell me how to print pdf files using java api.
    Thanks in advance

    Mike,
    Can't seem to get hold of the example described in your reply below. If you could let us have the URL to get then it would be great.
    My GUI application creates a pdf document which I need to print. I want to achieve this using the standard Java class PrinterJob (no 3rd party APIs I'm afraid, commercial restraints etc ..). I had a stab at it using the following code. When executed I get the pretty printer dialog then when I click ok to print, nothing happens!
    boolean showPrintDialog=true;
    PrinterJob printJob = PrinterJob.getPrinterJob ();
    printJob.setJobName ("Contract.pdf");
    try {
    if (showPrintDialog) {
    if (printJob.printDialog()) {
    printJob.print();
    else
    printJob.print ();
    } catch (Exception PrintException) {
                   PrintException.printStackTrace();
    Thank you and a happy new year.
    Cheers,
    Chris

  • How do i compress a .pdf file

    How Do I Compress A .    pdf File

    The PDF format itself contains significant amounts of compression applied to all PDF file contents. One can compress a PDF file into a ZIP file, but that ZIP file cannot be directly opened by the Adobe Reader.
              - Dov

  • How to open a pdf file in JFrame

    hi to all,
    i need to know how to open a pdf file in JFrame. Help is needed with this would grateful, and i used the code state below..
    InputStream in=this.getClass().getResourceAsStream("/images/help1.pdf");
    Process p = Runtime.getRuntime().exec(in);Thanks in Advance...

    this is the error which i'm getting while executing the code
    init:
    deps-jar:
    Compiling 1 source file to /home/VEL AREA/SampleApplication/build/classes
    /home/VEL AREA/SampleApplication/src/sampleapplication/Main.java:3: package com.sun.pdfview does not exist
    import com.sun.pdfview.PDFFile;
    /home/VEL AREA/SampleApplication/src/sampleapplication/Main.java:4: package com.sun.pdfview does not exist
    import com.sun.pdfview.PDFPage;
    /home/VEL AREA/SampleApplication/src/sampleapplication/Main.java:5: package com.sun.pdfview does not exist
    import com.sun.pdfview.PagePanel;
    /home/VEL AREA/SampleApplication/src/sampleapplication/Main.java:25: cannot find symbol
    symbol : class PagePanel
    location: class sampleapplication.Main
    PagePanel panel = new PagePanel();
    /home/VEL AREA/SampleApplication/src/sampleapplication/Main.java:25: cannot find symbol
    symbol : class PagePanel
    location: class sampleapplication.Main
    PagePanel panel = new PagePanel();
    /home/VEL AREA/SampleApplication/src/sampleapplication/Main.java:36: cannot find symbol
    symbol : class PDFFile
    location: class sampleapplication.Main
    PDFFile pdffile = new PDFFile(buf);
    /home/VEL AREA/SampleApplication/src/sampleapplication/Main.java:36: cannot find symbol
    symbol : class PDFFile
    location: class sampleapplication.Main
    PDFFile pdffile = new PDFFile(buf);
    /home/VEL AREA/SampleApplication/src/sampleapplication/Main.java:39: cannot find symbol
    symbol : class PDFPage
    location: class sampleapplication.Main
    PDFPage page = pdffile.getPage(0);
    8 errors
    BUILD FAILED (total time: 2 seconds)
    for the reply #4..

  • Reading PDF files in java

    Hi,
    can any one help me on how to read pdf files in java using itext. I have written some piece of code but it is of no use. It is giving some garbage.
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    import com.lowagie.text.pdf.PdfReader;
    public class PdfAccess
    public static void main(String[] args)
    try {
    String pdfFile = args[0];
    PdfReader reader = new PdfReader(pdfFile);
    int pageCount = reader.getNumberOfPages();
    System.out.println(pageCount);
    String content = " ";
    for(int i=1;i<=pageCount;i++) {
    byte[] pageContent = reader.getPageContent(i);
    content = content+(pageContent.toString());
    System.out.println(content.trim());
    } catch(Exception e) { }
    can any one help me on how to get contents of the file. Are there examples avalilable??

    * Try this by PDFBOX , it will execute well as per ur request..........
        public void getPdfText(String fileName) throws IOException {
            StringWriter sw = new StringWriter();
            PDDocument doc = null;
            try {
                doc = PDDocument.load(fileName);
                PDFTextStripper stripper = new PDFTextStripper();
                stripper.setStartPage(1);
                stripper.setEndPage(Integer.MAX_VALUE);
                stripper.writeText(doc, sw);
                OutputStream out=new FileOutputStream(new File("d://PDFText.txt"));
                PrintStream write=new PrintStream(out,true,"UTF-8");
                write.print(sw.toString());
                //System.out.println(sw.toString());
            } finally {
                if (doc != null) {
                    doc.close();
    Can..Can...If we Try...!

  • How to display a pdf file in jsp

    hi,
    How to display a pdf file in jsp iam having a class which will return fileinputstream of the file object. the pdf file is in server.
    regards
    Arul

    A JSP is a combo of HTML and Java, so you can't really "display" a PDF file in a JSP.
    You can provider a href link to the PDF file in your JSP.
    You can use some utility package to read the contents of the PDF, pull certain things out of it, and display that in your JSP as html
    In a servlet you can set the content type to application/pdf and write the binary data of the PDF back to the browser. Once the browser finishes reading in the data it should open the PDF.

Maybe you are looking for

  • Is there any way to get the canvases names on the form

    i have a form in Orcle 6i i want when the form load i get the names of the Canvases on the Form automatically to change their visual attribute is there any way to get the name of the canvases on the form automatically Thanks in advance

  • Music Player Albums?

    Hi, When I installed music player optional update,I see 2 albums,both named Unknown, can one name them if yes how? The 1st Unknown has songs whose details show artist names. But the 2nd Unknown album has songs with no artist names & can't type one wh

  • Lines Sync's but no download

    Hi, ADSL 2+ connection, very stable line I have a Billion 7800DXL router, which initially works great, however after about 3 days (varys but never longer than a week), the router stops downloading, even though the router is saying that it is still sy

  • Whitehouse.gov and the SUBMIT button missing

    Some buttons are not appearing on web pages such as on this page we cannot see the buttons for bold, insert a link, numbered list and the post question button at the bottom of this page.

  • 2 different Adobe products, a different Adobe ID for each, one computer -- Possible?

    I have a laptop that I use for personal matters and business.  I currently have Photoshop installed on it and linked to my personal Adobe ID.  I am about to install Adobe Digital Editions to use for work and would like to link it to a new Adobe ID I