Learn itext from scratch create pdf
1. Create a PDF with a stream
ImportJava.io.File;ImportJava.io.FileOutputStream;Importcom.itextpdf.text.*;ImportCom.itextpdf.text.pdf.PdfWriter; Public classPdftest { Public Static voidMain (string[] args)throwsException {/*** PDF Preparation defines the document file output stream and other files. pdf*/Document Pdfdoc=NewDocument (); //path output stream of the PDF file to be generatedFileOutputStream Pdffile =NewFileOutputStream (NewFile ("D:/firstpdf.pdf")); //a text paragraph in a PDF fileParagraph Paragraph =NewParagraph ("My first PDF file with an image ..."); Image Image=Image. getinstance ("C:/users/administrator/desktop/itext Create Pdf.jpg"); //get PDFWriter output stream object with Document object, File objectpdfwriter.getinstance (Pdfdoc, pdffile); Pdfdoc.open (); //Open Document Documents//Add a text paragraph, a picturePdfdoc.add (paragraph); Pdfdoc.add (image); Pdfdoc.close (); }}
2. pdf created according to the template
Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importcom.itextpdf.text.Document;Importcom.itextpdf.text.DocumentException;Importcom.itextpdf.text.Paragraph;ImportCom.itextpdf.text.pdf.PdfWriter; Public classTestpdf { Public Static voidMain (string[] args) {Try{Document Document=NewDocument (); PDFWriter. getinstance (document,NewFileOutputStream ("F:\\test.pdf" )); Document.open (); Document.add (NewParagraph ("Pride in me!" )); Document.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(documentexception e) {e.printstacktrace (); } }}
Itext POI Learning Tour (1) Create pdf