change pdf version

This section describes to change pdf version in java.

change pdf version

This section describes to change pdf version in java.

change pdf version

change pdf version

     

In this program we are going to change  the version of pdf file  through java program.

In this example we need iText.jar file, without this jar file we never compile our application . The package we need to import is java.io.FileOutputStream, java.io.IOException, com.lowagie.text.Document, com.lowagie.text.DocumentException, com.lowagie.text.Paragraph, com.lowagie.text.Rectangle,com.lowagie.text.pdf.PdfWriter,  import com.lowagie.text.pdf.PdfReader
..

The java.io.FileOutputStream and java.io.IOException classes are used to create file and throw the exception, if there is any exception occurs while creating a file. The com.lowagie.text.Document class is used to create a document object,  com.lowagie.text.DocumentException is used to throw exception if there is any exception. com.lowagie.text.Paragraph classes is used to make paragraph , com.lowagie.text.Rectangle classes is used to create object of Rectangle class which will be used for setting the left, right, top, bottom margin, com.lowagie.text.pdf.PdfWriter class is used to write the document on a pdf file,com.lowagie.text.pdf.PdfReader class provide  a getPdfVersion() method which used to find the version of pdf file.

To make the program for changing the pdf version firstly we will make a class ChaningVersionPDF
. Remember the name of the file should be such that, if any other person sees the example then just by seeing the name of the program he can understand what the program is going to do without seeing the code. 

Inside this class declare the main method which must throws Exception. After that follow the simple steps find the version:

  1. Create Rectangle object to set length and width of page.
  2. Create Document object and inside the constructor of Document pass pageSize and four integer which set the left,right,top and bottom margin. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.
  3. Create object of PrintWiter to add the value to document for this we use getInstance() method in which we pass two arguments document and FileOutputStream("cahngingVersionPDF.pdf").
  4. Used the setPdfVersion(PdfWriter.VERSION_1_2) to set the version of pdf. 
  5. Open the document.
  6. Create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously created Document. Inside the constructor of Paragraph we have passed a String .Create paragraph and add it into document .
  7. Add the paragraph in the document.
  8. close the document. The closing of the document is important because it flushes and closes the OutputStream instance. 
  9. Create object of PdfReader to read the pdf file.
  10. To get the version of pdf file use a method getPdfVersion(). It is method of  PdfReader class.

The code of the program is given below:

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfReader;
public class ChaningVersionPDF {
public static void main(String[] args)throws Exception {
System.out.println("Example of Changing the PDF version of a document"); 
Rectangle pageSize = new Rectangle(288, 720);
Document document = new Document(pageSize, 36, 18, 72, 72);
PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("changedVersionPDF.pdf"));
writer.setPdfVersion(PdfWriter.VERSION_1_2);
document.open();
document.add(new Paragraph("Version Of PDF-->>Rose India"));
document.add(new Paragraph("-->>RoseIndai.net"));
document.close();
PdfReader reader = new PdfReader("changedVersionPDF.pdf");
System.out.println("Version of PDF:"+reader.getPdfVersion());
}}

The output of the program is given below:


Download this example.