Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Concatenate pdf files using command argument 
 

In this section, you will learn about how to concatenate pdf files files using command argument in java.

 

Concatenate pdf files using command argument

                         

In this program we are going to concatenate pdf files into a destination pdf files through java program. The all data of files  are concatenated into
destination pdf. You can concentrate two or more file into a single file.   

In this example we need iText.jar file, without this jar file we never compile our application . The package we need to import are java.io. FileOutputStream,java.util.ArrayList,java.util.List,com.lowagie.text.Document,com.lowagie.text.pdf.PRAcroForm, com.lowagie.text.pdf.PdfCopy,com.lowagie.text.pdf.PdfImportedPage,com.lowagie.text.pdf.PdfReader,com.lowagie.
text.pdf.SimpleBookmark.


The java.io.FileOutputStream  class is used to create file.The java.util.ArrayList and java.util.List classes are used to create object of an array list and a list.  The com.lowagie.text.Document class is used to create a document object, com. lowagie.text.pdf.PRAcroForm class extends DpfDictionary. The PRAcroForm class captures an AcroForm on input.
AcroForm is  a collection of fields that is used for gathering information interactively from the user. com. lowagie.text.pdf.PdfCopy  class is used to make copies of  PDF documents and It extends PdfWriter.The com.lowagie.text.pdf. PdfImportedPage class is used to import an page. The PdfImportedPage class extends PdfTemplate. The com.lowagie.text.pdf.PdfReader class is used to read pdf from specified file. PdfReader class throws an IOException  and extends Object class. The class  com.lowagie.text.pdf.SimpleBookmark is to create an instance of SimpleBookmark.  

To make the program for concatenate pdf  files  firstly we will make a class Concanenate. 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. 
The steps to execute this program:

  1. Makes minimum two pdf file.
  2. Download the  iTex.jar file .
  3. Copy iText.jar into lib directory and Set the class.
  4.  Make or Download the source code of program.
  5. Put the source code and pdf files into same directory.
  6. Compile the source code.
  7. Pass the name of two (or more files) and  and destination pdf file name.
  8. Press enter to execute your program. 

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

  1. First check the number of arguments passed in command line. If it is less than three then print "arguments: file1 [file2 ...] destfile" else  proceed  next.  
  2. Create an object of ArrayList   and store the destination   file name an String object outFile.
  3. Initialize document and PdfWriter null .
  4. Start a while loop upto destination file name.
  5. Read the file  from command argument and find number of pages.
  6. Store the file into an object boomarks 
  7. And add 
  8.  close the document

The code of the program is given below:

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.SimpleBookmark;
public class Concatenate {
 public static void main(String args[])throws Exception {
    if (args.length < 2) {
   System.err.println("arguments: file1 [file2 ...] destfile");
        
}
        else {
     
System.out.println("PdfCopy example");          
     int pageOffset = 0;
     ArrayList master = new ArrayList();
     int f = 0;
     String outFile = args[args.length-1];
     Document document = null;
     PdfCopy  writer = null;
     while (f < args.length-1) {                   
     PdfReader reader = new PdfReader(args[f]);
     reader.consolidateNamedDestinations();
     
int n = reader.getNumberOfPages();
     List bookmarks = SimpleBookmark.
getBookmark
(reader);
     if (bookmarks != null) {
     if (pageOffset != 0)
     SimpleBookmark.shiftPageNumbers
(
bookmarks, pageOffset, null);
     master.addAll(bookmarks);
     }
     pageOffset += n;
     if (f == 0) {                      
     document = new Document(reader.
getPageSizeWithRotation
(1));                  
     writer = new PdfCopy(document, 
new 
FileOutputStream(outFile));                       
      document.open();     
                  
}
      
PdfImportedPage page;
        
for (int i = 0; i < n; ) {
          ++i;
           page = writer.getImportedPage(reader, i);
           writer.addPage(page);
              }
           PRAcroForm form = reader.getAcroForm();
             if (form != null)
            writer.copyAcroForm(reader);
                f++;
              }
             if (!master.isEmpty())
                writer.setOutlines(master);
                    document.close();           
        }
    }
}

Download file1 example.

Download file2 example.

The output of the program is given below:

Download this example.

                         

» View all related tutorials
Related Tags: c web pdf file stl text application import jar diff io make packages port this package itext app if tex

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.