First, Introduction
Itextpdf is an open source library that allows you to create and manipulate PDF documents. It enables developers to improve the web and other applications to dynamically generate or manipulate PDF documents. With the document and PDFWriter classes in Itext, we can create PDF documents at the source as databases, XML files, or other data sources. There are three ways of doing this:
- Use high-level objects such as chunk,phrase,paragraph,list to wait. These objects are often referred to as the basic building blocks of itext.
- Use low-level features. Using Pdfcontentbyte, a series of classes mapped to each operator's method, the operand can be in Adobe's imaging model. This class also has a number of handy ways to draw arcs, circles, rectangles, and text in absolute positions.
- An implementation class PDFGRAPHICS2D (not available in Itextsharp) that uses the Java version of Itext's abstract class graphics2d.
Itext comes with a number of classes that support different image types, color spaces, fonts, and portable collections. There are features to improve accessibility of PDF files to ensure that PDFs are available, and so on. Itextpdf's official website is as follows:
http://www.itextpdf.com/
Ii. examples
1. Related dependencies are as follows:
<!--PDF -<Dependency> <groupId>Com.itextpdf</groupId> <Artifactid>Itextpdf</Artifactid> <version>5.5.6</version> <type>Jar</type> </Dependency> <Dependency><groupId>Com.itextpdf.tool</groupId> <Artifactid>Xmlworker</Artifactid> <version>5.5.6</version></Dependency><!--Chinese Support -<Dependency> <groupId>Com.itextpdf</groupId> <Artifactid>Itext-asian</Artifactid> <version>5.2.0</version></Dependency>
2. Example
1 PackageCom.test.excel;2 3 Importjava.io.FileNotFoundException;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 7 Importorg.junit.Test;8 9 ImportCom.itextpdf.text.BaseColor;Ten ImportCom.itextpdf.text.Chunk; One Importcom.itextpdf.text.Document; A Importcom.itextpdf.text.DocumentException; - Importcom.itextpdf.text.Element; - ImportCom.itextpdf.text.Font; the Importcom.itextpdf.text.PageSize; - Importcom.itextpdf.text.Paragraph; - Importcom.itextpdf.text.Phrase; - ImportCom.itextpdf.text.Rectangle; + ImportCom.itextpdf.text.pdf.BaseFont; - Importcom.itextpdf.text.pdf.PdfPTable; + ImportCom.itextpdf.text.pdf.PdfWriter; A at /** - * Create PDF - */ - Public classPdfcreate { - - /** in * Create PDF - */ to @Test + Public voidcreatepdf () { - //Page Size theRectangle rect =NewRectangle (PageSize.B5.rotate ()); * //Page background Color $ Rect.setbackgroundcolor (basecolor.white);Panax Notoginseng //Create a document -Document document =NewDocument (rect); the Try { + //Get PDFWriter instance APDFWriter writer = pdfwriter.getinstance (document,NewFileOutputStream ("C:/test/testpdf.pdf")); the + //PDF Version (default 1.4) - writer.setpdfversion (pdfwriter.pdf_version_1_7); $ $ //Document Properties -Document.addtitle ("Test document title"); -Document.addauthor ("Zhang San"); theDocument.addsubject ("PDF Test"); -Document.addkeywords ("PDF");WuyiDocument.addcreator ("Zhang San"); the - //Margins Blank WuDocument.setmargins (10, 20, 30, 40); - About //Resolve Chinese support issues $Basefont Bfchinese = Basefont.createfont ("Stsong-light", "Unigb-ucs2-h", basefont.not_embedded); -Com.itextpdf.text.Font Fontchinese =NewCom.itextpdf.text.Font (Bfchinese, 12, Com.itextpdf.text.Font.NORMAL); - - //title AParagraph Toptile =NewParagraph ("title",NewFont (Bfchinese, 14, Font.normal)); + toptile.setalignment (paragraph.align_center); theToptile.add (NewParagraph ("")); - $ theParagraph pragraph =NewParagraph ("This is a paragraph, this is a paragraph, this is a paragraph, this is a paragraph, this is a paragraph, this is a paragraph, this is a passage, this is a paragraph, this is a paragraph, this is a paragraph, this is a paragraph, this is a passage, this is a paragraph", Fontchinese); the pragraph.setalignment (element.align_left); thePragraph.add (NewParagraph ("")); the - in the //Add a table thepdfptable table =NewPdfptable (4); AboutString content = "Hello:"; the for(inti = 0; I < 200; i++) theTable.addcell (NewPhrase (NewChunk (content +I, Fontchinese ))); the + //download using the HttpServletResponse - //Response.setcontenttype ("application/pdf; Charset=utf-8 "); the //Response.setheader ("content-disposition", "inline; Filename= "+ New String (" Test.pdf ". GetBytes ()," UTF-8 "));Bayi //pdfwriter.getinstance (document, Response.getoutputstream ()); the the //Open Document - Document.open (); - the //Add Content the Document.add (toptile); the Document.add (pragraph); the document.add (table); - //Close Document the document.close (); the}Catch(FileNotFoundException e) { the e.printstacktrace ();94}Catch(documentexception e) { the e.printstacktrace (); the}Catch(IOException e) { the e.printstacktrace ();98 } About } -}
ITEXTPD F generate PDF file