Tuesday, 10 March 2015

Reading DOC file and writing in PDF using HWPF,LOWAGIE objects

Hi,

I came across writing into PDF using HWPF,LOWAGIE  objects in CF as well as in JAVA. Here I am reading a DOC file and copying contents to a PDF. It can be used alternate to CFDOCUMENT for CF8 below versions.

Required JARS are




 <cfscript>  
      // Creating JAVA fileinput stream object  
      fileinput = CreateObject("java", "java.io.FileInputStream");  
      // Calling InputStream constructor  
      fileinput.init("C:\Users\admin\Adobe ColdFusion Builder workspace\Practise\word.doc");  
      //Creating HWPF object  
      document = CreateObject("java", "org.apache.poi.hwpf.HWPFDocument");  
      // Calling HWPF constructor  
      document.init(fileinput);  
      // Creating a WordExtractor Object  
      reader = CreateObject("java", "org.apache.poi.hwpf.extractor.WordExtractor");  
      // Calling WordExtractor constructor  
      reader.init(document);  
      //Each line is captured in docfile as an array  
      docfile = reader.getParagraphText();  
      //Dumpingto see Docfile in array format  
      writeDump(docfile);  
      docfilefull = '';  
      //convertingarray to string  
      for(a in docfile)  
      {  
           docfilefull = docfilefull & a & chr(13) & chr(10);  
      }  
      //Creating lowagie object  
      document = CreateObject("java", "com.lowagie.text.Document");  
      // Calling lowagie constructor  
      document.init();  
      // Creating FileOutputStream object  
      fileIO = CreateObject("java", "java.io.FileOutputStream");  
      // Calling FileOutputStream constructor with path/name of file to written  
      fileIO.init("C:\Users\admin\Desktop\word19.pdf");  
      // Creating PdfWriter object  
      writer = CreateObject("java", "com.lowagie.text.pdf.PdfWriter");  
      //Calling getInstance method  
      writer.getInstance(document, fileIO);  
      //Calling open method of lowgie object  
      document.open();  
      // Creating Paragraph object  
      paragraph = CreateObject("java", "com.lowagie.text.Paragraph");  
      //Calling paragraph constructor passing the string which have the doc file content  
      paragraph.init(docfilefull);  
      //Writing the PDF document with DOC contents  
      document.add(paragraph);  
      //Closing document  
      document.close();  
 </cfscript>  

No comments:

Post a Comment