KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pj > samples > UncompressPdf


1 package com.etymon.pj.samples;
2
3 import java.io.*;
4 import java.util.*;
5 import com.etymon.pj.*;
6 import com.etymon.pj.exception.*;
7 import com.etymon.pj.object.*;
8
9 /**
10    Demonstrates uncompressing flate-compressed streams in a PDF file.
11    @author Nassib Nassar
12  */

13 public class UncompressPdf {
14
15     public static void main(String JavaDoc args[]) {
16         if (args.length < 2) {
17             System.out.println("UncompressPdf [infile] [outfile]");
18             return;
19         }
20         String JavaDoc infile = args[0];
21         String JavaDoc outfile = args[1];
22         System.out.println("This creates a new version of a PDF file and uncompresses");
23         System.out.println("any flate-compressed streams.");
24         try {
25             System.out.println("Reading and parsing input file...");
26             
27             // load the infile
28
Pdf pdf = new Pdf(infile);
29
30             System.out.println("Got it.");
31
32             if (pdf.getEncryptDictionary() != null) {
33                 System.out.println("File appears to be encrypted.");
34             }
35             
36             int y = pdf.getMaxObjectNumber();
37
38             for (int x = 1; x <= y; x++) {
39                 PjObject obj = pdf.getObject(x);
40                 if (obj instanceof PjStream) {
41                     try {
42                         pdf.registerObject(((PjStream)obj).flateDecompress(), x);
43                     }
44                     catch (InvalidPdfObjectException e) {
45                     }
46                 }
47
48             }
49             
50             System.out.println("Writing modified output file...");
51
52             pdf.writeToFile(outfile);
53
54             System.out.println("Done.");
55         }
56         catch (PjException pje) {
57             System.out.println(pje);
58         }
59         catch (IOException ioe) {
60             System.out.println(ioe);
61         }
62     }
63     
64 }
65
Popular Tags