JAVA Read sample doc file, fill with data and generate PDF -
i triing create automatization programme in java. have sample doc file. need fill blank parts or <> "signed" parts database, create pdf files.
i tried read word :
import java.io.*; import org.apache.poi.hwpf.hwpfdocument; import org.apache.poi.hwpf.extractor.wordextractor; public class readdocfile { public static void main(string[] args) { file file = null; wordextractor extractor = null ; seek { file = new file("c:\\new.doc"); fileinputstream fis=new fileinputstream(file.getabsolutepath()); hwpfdocument document=new hwpfdocument(fis); extractor = new wordextractor(document); string [] filedata = extractor.getparagraphtext(); for(int i=0;i<filedata.length;i++){ if(filedata[i] != null) system.out.println(filedata[i]); } } catch(exception exep){} } }
but attemption bad in many way cause need write of parts, , method create single test doc.
so can advice me api write in word doc eg: after name :
or in 5 row write this:
, when finish word should generate pdf , 1 time again ...
i looking solution wich found xssfworkbook function ( generate pdf of doc ).
or read sample pdf , fill datas , save new pdf.
thx
use itext (http://sourceforge.net/projects/itext/) , apache poi project http://poi.apache.org/index.html
a sample code :
import java.io.bufferedinputstream; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import org.apache.poi.hwpf.extractor.wordextractor; import com.itextpdf.text.document; import com.itextpdf.text.documentexception; import com.itextpdf.text.paragraph; import com.itextpdf.text.pdf.pdfwriter; public static void main(string[] args) { string pdfpath = "c:/"; string pdfdocpath = null; seek { inputstream = new bufferedinputstream(new fileinputstream("c:/test.doc")); wordextractor wd = new wordextractor(is); string text = wd.gettext(); /* docx // import import org.apache.poi.openxml4j.exceptions.invalidformatexception; import org.apache.poi.xwpf.extractor.xwpfwordextractor; import org.apache.poi.xwpf.usermodel.xwpfdocument; // code xwpfdocument hdoc = new xwpfdocument(is); extractor = new xwpfwordextractor(hdoc); string text = extractor.gettext(); */ document document = new document(); pdfwriter.getinstance(document, new fileoutputstream(pdfpath + "viewdoc.pdf")); document.open(); document.add(new paragraph(text)); document.close(); pdfdocpath = pdfpath + "viewdoc.pdf"; system.out.println("pdf document path is" + pdfdocpath); } grab (filenotfoundexception e1) { system.out.println("file not exist."); } grab (ioexception ioe) { system.out.println("io exception"); } grab (documentexception e) { e.printstacktrace(); } }
java pdf
No comments:
Post a Comment