Sunday, June 16, 2013

Split Pdf

The SplitPdf program can be used to split a pdf file to many pdf files. You can split the pdf file in every any page that you want. For example, if you have 100-page pdf file and you split it in every two pages, you will get fifty pdf files. Each file has two pages.
The API used to split a pdf file is iText. It is a useful and  free library. For more information about iText, you can visit its web site listed at the right side bar of this post.

split pdf in Java

SplitPdf source code:

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;

public class SplitPdf {
public static void main(String[] args){
try{

splitPdf(args[0],Integer.parseInt(args[1]));

}catch(ArrayIndexOutOfBoundsException aiobe){System.out.println("Invalid parameters input");}

}

public static void splitPdf(String src, int every_n_page){
try{
Document doc=null; //declare document
PdfReader pr=new PdfReader(src); //create pdf reader object
PdfCopy pc=null; //declare pdf copy object
int start=0; //initialize the start variable

for(int i=1;i<=pr.getNumberOfPages();i++){ //loop through the pdf document
//split the pdf document in every n page
if(i%every_n_page==0){
doc=new Document();
pc=new PdfCopy(doc, new FileOutputStream("split"+i+".pdf"));
doc.open();
for(int page=start+1;page<=i;page++)
pc.addPage(pc.getImportedPage(pr, page));
start+=every_n_page;
doc.close();
}
}
}catch(Exception e ) {}
}



}

To split a Pdf file to many Pdf files. Firstly, you need to have a PdfReader object to get all pages in the source Pdf file. Once you have the PdfReader object, you can use the PdfCopy class to get every Pdf page to be added to the Document object and write the Document out to the output file.

9 comments:

  1. you can try this free online pdf to text converter to convert pdf to text online.

    ReplyDelete
  2. That is an awesome site. Exceptionally useful material i found. feel free to check Buzz Applications now.

    ReplyDelete
  3. when i am trying this gives exception getOutputStream() for this response

    ReplyDelete

  4. The war between humans, orcs and elves continues earn to die . Lead your race through a series of epic battles, using your crossbow to fend off foes and sending out units to destroy castleshappy wheels . Researching and upgrading wisely will be crucial to your success! There are 5 ages total and each one will bring you new units to train to fight in the war for you cause.
    earn to die 2
    Whatever you do, don’t neglect your home base because you cannot repair it and once it is destroyed, you lose! Age of War is the first game of the series and really sets the tone for the Age of War games . Also try out the Age of Defense series as it is pretty similar.
    In this game, you start at the cavern men’s age, then evolvetank trouble ! There is a total of 5 ages, each with its units and turrets. Take control of 16 different units and 15 different turrets to defend your base and destroy your enemy.
    The goal of the game also differs depending on the level. In most levels the goal is to reach a finish line or to collect tokens. Many levels feature alternate or nonexistent goals for the player.

    ReplyDelete
  5. A good blog. Thanks for sharing the information. It is very useful for my future. keep sharing
    duck life 3 | Slither io |Red Ball 3 |

    ReplyDelete
  6. But how to get this iText library?

    ReplyDelete
  7. Split multiple PDF files in a single processing
    session. https://3steppdf.com/features-split.php

    ReplyDelete
  8. I wanted to thank you for this excellent read!! I definitely loved every little bit of it.Cheers for the info!!!! & This is the perfect blog for anyone who wants to know about this topic. I like play games five nights at freddy’s 4 | game word cookies answers | hotmail sign in

    ReplyDelete