Friday, February 15, 2008

A dozen free & essential apps for Windows

A dozen free & essential apps for Windows by ZDNet's George Ou -- Every time I build a new Windows computer, there are a dozen free and essential applications that I always install for other people. These applications all seem to fill essential functions and they all seem to be well-behaved installers and uninstallers, in other words it won’t crash your computer or drag it down with gunk. [...]

Wednesday, February 13, 2008

Small utility program to unlock PDF documents

Ever downloaded a PDF document from internet which doesn’t have print option or its disabled? I hope you must have come across this situation.

Try this small java program to unlock PDF document for printing etc.

I am using iText API for doing it. They have a very rich collection of classes and methods to play with PDF files.

 

You need itext-1.4.6.jar to run this program. You can download this jar from http://www.lowagie.com/iText/download.html . If you are using eclipse IDE then set this jar in your project build path or if you are running this code from command prompt then you need to set this jar in your classpath.

 

So here is the code… just copy it and run it. You can see we are passing PDF file name to unlockPdf() method though command prompt. inputFile name is your original file which you want to unlock and outputFile name can be anything which you like.

 

package my.examples;

 

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

import com.lowagie.text.DocumentException;

import com.lowagie.text.pdf.PdfEncryptor;

import com.lowagie.text.pdf.PdfReader;

import com.lowagie.text.pdf.PdfWriter;

 

public class UnlockPdf

{

  public static void main(String[] args)

  {

    if (args.length < 2)

    {

      System.err.println("usage: java my.examples.UnlockPdf inputfile.pdf outputfile.pdf");

    }

   else

    {

      new UnlockPdf().unlock(args[0], args[1]);

    }

  }

 

  private void unlock(String inputFile, String outputFile)

  {   

 

    try

    {

      PdfReader reader = new PdfReader(inputFile);

      PdfEncryptor.encrypt(reader, new FileOutputStream(outputFile), null, null,

  PdfWriter.AllowAssembly | PdfWriter.AllowCopy

              | PdfWriter.AllowDegradedPrinting | PdfWriter.AllowFillIn

              | PdfWriter.AllowModifyAnnotations | PdfWriter.AllowModifyContents

              | PdfWriter.AllowPrinting | PdfWriter.AllowScreenReaders, false);

    }

    catch (FileNotFoundException e)

    {

      e.printStackTrace();

    }

    catch (IOException e)

    {

      e.printStackTrace();

    }

    catch (DocumentException e)

    {

      e.printStackTrace();

    }

  }

}

 

 

 

 

 

Popular Posts