Java exports word (with images) and java exports word Images

Source: Internet
Author: User

Java exports word (with images) and java exports word Images

1 public class CreateWordDemo {2 public void createDocContext (String file) throws extends entexception, IOException {3 // set the paper size 4 Document = new document (PageSize. a4); 5 // create a Writer to associate with the document object. The Writer can write the document to the disk RtfWriter2.getInstance (document, new FileOutputStream (file )); 6 document. open (); 7 // set the Chinese font 8 BaseFont bfChinese = BaseFont. createFont ("STSongStd-Light", "UniGB-UCS2 -H ", BaseFont. NOT_EMBEDDED); 9 // title Font Style 10 Font titleFont = new Font (bfChinese, 12, Font. BOLD); 11 // text Font style 12 Font contextFont = new Font (bfChinese, 10, Font. NORMAL); 13 Paragraph title = new Paragraph ("title"); 14 // set the title format alignment mode 15 title. setAlignment (Element. ALIGN_CENTER); 16 title. setFont (titleFont); 17 document. add (title); 18 String contextString = "iText is a java class library that can quickly generate PDF files. "19 +" \ n "// newline +" iText java class is useful for read-only documents that need to generate tables containing text, "20 +", and images. Its Class Library is particularly well-formed with java Servlet. "21 +" using iText and PDF allows you to properly control Servlet output. "; 22 Paragraph context = new Paragraph (contextString); 23 // The text format is left aligned with 24 context. setAlignment (Element. ALIGN_LEFT); 25 context. setFont (contextFont); 26 // number of rows left blank in the previous paragraph (title) 27 context. setSpacingBefore (5); 28 // set the number of columns in the first row to 29 context. setFirstLineIndent (20); 30 document. add (context); 31 // use the FontFactory class in combination with Font and Color to set a variety of Font styles 32 33 Paragraph underline = new Paragraph ("underline implementation", FontFactory. getFont (FontFactory. HELVETICA_BOLDOBLIQUE, 18, Font. UNDERLINE, new Color (0, 0,255); 34 document. add (underline); 35 // set Table 36 Table aTable = new Table (3); 37 int width [] = {25, 25, 50}; 38 aTable. setWidths (width); // set the percentage of each column to 39 aTable. setWidth (90); // The page width is 90% 40 aTable. setAlignment (Element. ALIGN_CENTER); // The center displays 41 aTable. setAlignment (Element. ALIGN_MIDDLE); // the vertical center shows 42 aTable. setAutoFillEmptyCells (true); // automatically fills up 43 aTable. setBorderWidth (1); // The Border width is 44 aTable. setBorderColor (new Color (0,125,255); // The border Color is 45 aTable. setPadding (2); // the padding. You can see what the effect means by 46 aTable. setSpacing (3); // The distance between cells is 47 aTable. setBorder (2); // border // set the header to 48 49 Cell haderCell = new Cell ("Table Header"); 50 haderCell. setHeader (true); 51 haderCell. setColspan (3); 52 aTable. addCell (haderCell); 53 aTable. endHeaders (); 54 Font fontChinese = new Font (bfChinese, 12, Font. NORMAL, Color. GREEN); 55 Cell cell = new Cell (new Phrase ("this is a test of 3*3 Table data", fontChinese); cell. setVerticalAlignment (Element. ALIGN_TOP); 56 cell. setBorderColor (new Color (255, 0, 0); 57 cell. setRowspan (2); 58 aTable. addCell (cell); 59 aTable. addCell (new Cell ("#1"); 60 aTable. addCell (new Cell ("#2"); 61 aTable. addCell (new Cell ("#3"); 62 aTable. addCell (new Cell ("#4"); 63 Cell cell3 = new Cell (new Phrase ("one row and three columns", fontChinese); 64 cell3.setColspan (3 ); 65 cell3.setVerticalAlignment (Element. ALIGN_CENTER); 66 aTable. addCell (cell3); 67 document. add (aTable); 68 document. add (new Paragraph ("\ n"); 69 // add an Image. getInstance can be placed in the path and binary byte stream 70 Image img = Image. getInstance ("d: \ im00001800.jpg"); 71 img. setAbsolutePosition (0, 0); 72 img. setAlignment (Image. RIGHT); // set the image display position 73 img. scaleAbsolute (60, 60); // directly set the display size 74 // img. scalePercent (50); // indicates that the displayed size is 50% of the original size. // img. scalePercent (25, 12); // display ratio of image height and width 76 // img. setRotation (30); // image rotation angle 77 document. add (img); 78 document. close (); 79} 80 81 public static void main (String [] args) {82 CreateWordDemo word = new CreateWordDemo (); 83 String file = "d:/demo1.doc "; 84 try {85 word. createDocContext (file); 86} catch (incluentexception e) {87 e. printStackTrace (); 88} catch (IOException e) {89 e. printStackTrace (); 90} 91} 92}

 

Related Article

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.