Itext POI Learning Tour (1) Create pdf

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.