I use PDF output statements iText practice

  I use PDF output statements iText practice 

  Recently, work, the output PDF format used iText statements, iText familiar with the process of applying, here to share with you. 
  IText text output as a java source code, providing PDF, Html, Rtf variety of file formats, such as the output function.    For the output of the text provides a good package.    The PDF output statements, which only used a small open-source code as a whole a very small part.    Include: 
  1, the PDF file output element 2, PDF documents Some of the settings 3, the output of four Chinese table settings Below, I will make four, I use on January 1 in the process of feelings. 

  1. PDF file output elements of the first, as the most basic program design practice, the most classic example is still Hello World, the most, I am also a Hello world output.    Code like this: 

  Package com.lowagie.examples.general; 

  Import java.io.FileOutputStream; 
  Import java.io.IOException; 

  Import com.lowagie.text .*; 
  Import com.lowagie.text.pdf.PdfWriter; 

  / ** 
  * Generates a simple 'Hello World' PDF file. 
*
  * @ Author blowagie 
  * / 

  Public class HelloWorld ( 

  / ** 
  * Generates a PDF file with the text 'Hello World' 
*
  * @ Param args no arguments needed here 
  * / 
  Public static void main (String [] args) ( 

  System.out.println ( "Hello World"); 

  / / Step a: creation of a document object - 
  Document document = new Document (); 
  Try ( 
  / / Step b: 
  / / We create a writer that listens to the document 
  / / And directs a PDF-stream to a file 
  PdfWriter.getInstance (document, the new FileOutputStream ( "HelloWorld.pdf")); 

  / / Step c: we open the document 
  Document.open (); 
  / / Step d: we add a paragraph to the document 
  Document.add (new Paragraph ( "Hello World")); 
  ) Catch (DocumentException de) ( 
  System.err.println (de.getMessage ()); 
  ) Catch (IOException ioe) ( 
  System.err.println (ioe.getMessage ()); 
  ) 

  / / Step e: we close the document 
  Document.close (); 
  ) 
  ) 

  See a PDF file output, a total of only five steps needed to create a Document a. examples Document document = new Document (); 
  B. Document will be examples and documentation output streams of bundled together with PdfWriter PdfWriter.getInstance (document, the new FileOutputStream ( "HelloWorld.pdf")); 
  C. open the file document.open (); 
  D. Add text in the document document.add (new Paragraph ( "Hello World")); 
  E. close the document document.close (); 
  This five steps, we can generate a PDF file of the. 
  The key is two steps 2 and 4, and the second step, we will be a well-known document flow bind to the PDF files, the first of four road to documents added some words, in these two minor steps changes to a Pdf files can be done out. 

  2. PDF document as a statement of a number of settings, the regular circumstances, the need to print out statements, which involves a set of pages, as the statements of the PDF files, printers must be suitable for print output.    Here, I have two statements, PDF files set to A4 size pages: 

  Rectangle rectPageSize = new Rectangle (PageSize.A4); / / definition of A4 size pages / / rectPageSize = rectPageSize.rotate ();// This can be achieved with the A4 pages Transverse Document doc = new Document (rectPageSize, 50,50,50,50); / / The remaining four parameters, set up a page four border 

  IText API documentation available, we can see that the size of Rectangle in the provision of custom settings, can spread like a PageSize attribute, which provides several common page style, if necessary, can go to the iText API file enquiries. 
  Here, we can make a size to meet the requirements of the Pdf files. 

  3. Chinese output of java may be due to historical issues, and may be due to this source code is a foreigner done, inevitable, the Chinese output, we have to do some things. 
  In order to solve the problem of Chinese exports, the need to download a package called iTextAsian.jar the JAR.    Inside this package with the Chinese definition of a number of documents related to the output. 
  In order to output Chinese, I used the following statement: 

  BaseFont bfChinese = BaseFont.createFont ( "STSong-Light," "UniGB-UCS2-H" and BaseFont.NOT_EMBEDDED); 
  Font FontChinese = new Font (bfChinese, 12, Font.NORMAL); 
  Paragraph par = new Paragraph ( "Hello World" FontChinese) 
  Document.add (par); 

  The first line, the definition of a Chinese font basis, the second line with the basis of this Chinese fonts examples of a type font, the third line, type font used in a paragraph, fourth line, will be added to the paragraphs of text file. 
  In these firms, we first concern is often the first line of "STSong-Light", the definition of the use of the Chinese font, iTextAsian.jar provided several documents available fonts, properties are at the end of documents. 
  We have to take note that the first line of "UniGB-UCS2-H", the definition of the text coding standards and style GB we all know, is representative of H horizontal characters, V representative Shubai word iTextAsian.jar document by the end of the cmap Several documents are on the coding and pattern definition. 
  These two places are filled out documents without extension names, the two local languages you need parameters, we can allow Chinese to meet the needs of your normal output of the situation. 
  Here iText is necessary to point out a feature of the error, there will be no tips, will be dancing the part of the past mistakes, for example, in the absence of the Chinese fonts Chinese output circumstances, the Chinese part of the paragraph is blank , another example: If a document of some mistakes, the same output or PDF files, but the entire document is blank.    The FEC, I have not yet found a better way of his investigation and only his own code. 

  4. Forms of the above settings through those, I can have a normal Chinese exports, coupled with some suitable form can be a very good output of the PDF statements. 
  To a clear, iText in a document (Document), there are many forms (PdfPTable), there can be many a table cell (PdfPCell), a cell can be put inside many paragraphs (Paragraph), a paragraph Some text can be put inside. 
  It should be noted here is not to the concept of a direct form inside cells directly put, if a three tables into six cells, then it is two lines of the form. 
  The second is to the attention of the local, if one of the three most basic forms Add to the five did not set out any inter-cell, forms will be wrong, as said earlier, forms add less than fundamental documents, and will not have any tips. 
  The following code can achieve a three to three forms: 

  PdfPTable table = new PdfPTable (3); 
  For (int i = 0; i <9; i + +) ( 
  PdfPCell cell = new PdfPCell (); 
  Cell.addElement (new Paragraph ( "aaa")); 
  Table.addCell (cell); 
  ) 

  For a form, the other often use to cross-out is a problem, here, as long as PdfPCell targets will be set examples of inter-out figures on it.    As follows: 
  Cell.setColSpan (3); / / Note that this cell on the equivalent of three cells added to the table 

  In statements, and sometimes will encounter great form, if you wish to visit the second page, can still see the table on the need to use the first form of the first set, a simple example of the type called PdfPTable setHeaderRows ( ) method can be set on the first form of a few lines.    As follows: 
  Table.setHeaderRows (2) / / set up the first of the two forms to the attention of the head here is, if you set a n will form the first, but the final outcome, or to form just is not enough n n trip circumstances, form will be wrong, resulting in the entire form could not write the document. 
  Here, and those who use the above technologies, we have completed a simple statement of the iText related categories, more than related aspects.    In the article, it is impossible for 11, if needed can be found in the following address iText API documentation and tutorial. 

  Http://www.lowagie.com/iText/tutorial/index.html (iText tutorial) 
  Http://www.lowagie.com/iText/download.html (iText core packages) 
  Http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948 (iTextArea packet) 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Recommend Articles

Comments

Leave a Reply