skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, September 17, 2012

PDF Utility Code

Posted by Admin at 11:59 AM – 0 comments
 

Through this code one can merge any number f pdf files to single pdf file. Also it can convert string of any size to pdf.



/*
For running this code we need iText.jar which is free and can be downloaded from internet.
*/

public boolean simpleTxtToPDF(String text){
  
//text String can be of any size
   
File targetFolder = new File("C:/pdfFolder1");  // destination folder in which pdf file will be created
String newPdfFileName = "newPpdfFile.pdf";   // final generated pdf file
  
try{
 Document document = new Document(PageSize.A4, 36, 72, 108, 180); // creating empty pdf document
 PdfWriter.getInstance(document,new FileOutputStream(targetFolder+"/"+newPdfFileName));
 document.open();  // opening the document
 document.add(new Paragraph(text));  // adding text
 document.close();  // clsing the document
}
catch(Exception e){
 // print the stack trace to console
 e.printStackTrace();
 }
}
 


public File mergePdfs(File pdfFolder, ArrayList inputPdfFilesName, String outputPdfFile){
  
/* where
 * pdfFolder = its an local folder which contains pdf files to be merged(i.e C:/pdfFolder)
 * inputPdfFilesName = this is an array list which contains names of all the files(like file1.pdf, file2.pdf etc) and these    files must exist in pdfFolder
 * outputPdfFile = this is an output file name i.e outputPdfFile.pdf
 */

File outPutFile = null;
try{
 int pageOffset = 0;
 ArrayList master = new ArrayList();
 int f = 0;
 Document document = null;
 PdfCopy  writer = null;
 int filesCount = inputPdfFilesName.size();
 while (f < filesCount) {                   
  PdfReader reader = new PdfReader(pdfFolder+"\\"+inputPdfFilesName.get(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(pdfFolder + "\\" + outputPdfFile));                       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();
}
catch(Exception e)
{
  e.printStackTrace();
}

outPutFile = new File(pdfFolder + "\\" + outputPdfFile);
return outPutFile;
}




Labels: PDF Generation Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger