`
天边一朵雲
  • 浏览: 34585 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Itext相关代码收藏

 
阅读更多

Itext具体实现

package com.jw.text; 

import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import java.awt.Color; 
import java.awt.Point; 
import java.io.*; 
import java.net.MalformedURLException; 
import sun.management.FileSystem; 
import com.jw.dao.BookDAO; 
import com.jw.po.Books; 
import com.lowagie.text.BadElementException; 
import com.lowagie.text.Cell; 
import com.lowagie.text.Chunk; 
import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Element; 
import com.lowagie.text.Font; 
import com.lowagie.text.FontFactory; 
import com.lowagie.text.HeaderFooter; 
import com.lowagie.text.Image; 
import com.lowagie.text.PageSize; 
import com.lowagie.text.Paragraph; 
import com.lowagie.text.Phrase; 
import com.lowagie.text.Rectangle; 
import com.lowagie.text.Table; 
import com.lowagie.text.Watermark; 
import com.lowagie.text.pdf.BaseFont; 
import com.lowagie.text.pdf.PdfWriter; 

public class Text { 
private Document document = null; 

// 第一种简单的文本 
public void create1() { 
  Rectangle pageSize = new Rectangle(144, 720); 
  pageSize.setBackgroundColor(new Color(0xFF, 0xFF, 0xDE)); 
  try { 
   document = new Document(pageSize); 
   PdfWriter.getInstance(document, new FileOutputStream("d:\\1.pdf")); 
   document.open(); 
   // 横向输出 
   document.add(new Paragraph( 
     "sdfsdfsdfsdfsddsfsdfsdsdfsdfsdsdfsdfasfsdfhelloworld")); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// 第2种简单的文本 
public void create2() { 
  document = new Document(PageSize.A4.rotate()); 
  try { 
   PdfWriter.getInstance(document, new FileOutputStream( 
     "d:\\Chap0103.pdf")); 
   document.open(); 
   for (int i = 0; i < 20; i++) { 
    // 竖向输出 
    document 
      .add(new Phrase( 
        "Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, 

Hello Land, Hello People. ")); 
   } 
  } catch (FileNotFoundException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } catch (DocumentException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// 第3种简单的文本 
public void create3() { 
  document = new Document(PageSize.A5, 36, 72, 108, 180); 
  try { 
   PdfWriter.getInstance(document, new FileOutputStream( 
     "d:\\Chap0104.pdf")); 
   document.open(); 
   Paragraph paragraph = new Paragraph(); 
   paragraph.setAlignment(Element.ALIGN_JUSTIFIED); 
   for (int i = 0; i < 20; i++) { 
    paragraph 
      .add("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, 

Hello People. "); 
   } 
   Date date = new Date(); 
   SimpleDateFormat form = new SimpleDateFormat("yyy-MM-dd hh:mm:ss"); 
   String mytime = form.format(date); 
   paragraph.add(mytime); 
   document.add(paragraph); 
  } catch (FileNotFoundException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } catch (DocumentException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// 第4种,设置文件的头和标题等...... 
public void create4() { 
  document = new Document(); 
  try { 
   PdfWriter.getInstance(document, new FileOutputStream( 
     "d:\\Chap0106.pdf")); 
   document.addTitle("Hello World example"); 
   document.addSubject("This example explains step 6 in Chapter 1"); 
   document.addKeywords("Metadata, iText, step 6, tutorial"); 
   document.addCreator("My program using iText#"); 
   document.addAuthor("Bruno Lowagie"); 
   document.addHeader("Expires", "0"); 
   document.open(); 
   document.add(new Paragraph( 
     "Hello World sdfgds f sd fsd f sd fsd f sd f sd fs ")); 
  } catch (FileNotFoundException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } catch (DocumentException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// 第5种,向pdf中打印水印图片 
public void create5() { 
  document = new Document(); 
  try { 
   String url = System.getProperty("user.dir").toString() + "\\a.jpg"; 
   PdfWriter.getInstance(document, new FileOutputStream( 
     "d:\\Chap0107.pdf")); 
   // 放入图片并定坐标,以及图片的路径 
   Watermark watermark = new Watermark(Image.getInstance(url), 200, 
     420); 
   document.add(watermark); 
   // 设置头信息 
   HeaderFooter header = new HeaderFooter(new Phrase( 
     "This is a header"), false); 
   document.setHeader(header); 
   document.open(); 
   document.setPageSize(PageSize.A4.rotate()); 

   // Watermark watermark2 = new Watermark(Image.getInstance(url), 320, 
   // 200); 
   // document.add(watermark2); 

   // 设置结尾 
   HeaderFooter footer = new HeaderFooter( 
     new Phrase("This is page: "), true); 
   document.setFooter(footer); 
   // 设置内容 
   document.add(new Paragraph("Hello World")); 

   // 创建第2的页面 
   document.newPage(); 
   document.add(new Paragraph("Hello Earth")); 
   document.resetHeader(); 

   // 创建第3的页面 
   document.newPage(); 
   document.add(new Paragraph("Hello Sun")); 
   document.add(new Paragraph("Remark: the header has vanished!")); 
   document.resetPageCount(); 

   // 创建第4的页面 
   document.newPage(); 
   document.add(new Paragraph("Hello Sun")); 
   document.add(new Paragraph("Remark: the header has vanished!")); 
  } catch (Exception e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// 第6种设置文本的布局 
public void create6() { 
  String url = System.getProperty("user.dir").toString() + "\\a.jpg"; 
  document = new Document(); 
  try { 
   PdfWriter writerA = PdfWriter.getInstance(document, 
     new FileOutputStream("d:\\Chap0108a.pdf")); 
   // 设置布局 
   writerA.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft); 

   PdfWriter writerB = PdfWriter.getInstance(document, 
     new FileOutputStream("d:\\Chap0108b.pdf")); 
   // 设置布局 
   writerB.setViewerPreferences(PdfWriter.HideMenubar 
     | PdfWriter.HideToolbar); 

   PdfWriter writerC = PdfWriter.getInstance(document, 
     new FileOutputStream("d:\\Chap0108c.pdf")); 
   // 设置布局 
   writerC.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft 
     | PdfWriter.PageModeFullScreen 
     | PdfWriter.NonFullScreenPageModeUseThumbs); 

   Watermark watermark = new Watermark(Image.getInstance(url), 200, 
     420); 
   document.add(watermark); 
   HeaderFooter header = new HeaderFooter(new Phrase( 
     "This is a header"), false); 
   document.setHeader(header); 
   document.open(); 

   document.setPageSize(PageSize.A4.rotate()); 
   HeaderFooter footer = new HeaderFooter( 
     new Phrase("This is page: "), true); 
   document.setFooter(footer); 
   document.add(new Paragraph("Hello World")); 

   document.newPage(); 
   document.add(new Paragraph("Hello Earth")); 
   document.resetHeader(); 

   document.newPage(); 
   document.add(new Paragraph("Hello Sun")); 
   document.add(new Paragraph("Remark: the header has vanished!")); 
   document.resetPageCount(); 

   document.newPage(); 
   document.add(new Paragraph("Hello Moon")); 
   document 
     .add(new Paragraph("Remark: the pagenumber has been reset!")); 
  } catch (Exception e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// 第7种--------------- 
public void create7() { 
  try { 
   document = new Document(PageSize.A4, 50, 50, 50, 50); 
   PdfWriter writer = PdfWriter.getInstance(document, 
     new FileOutputStream("d:\\Chap0109.pdf")); 
   // setEncryption方法中可以设置如下内容(这样打开pdf时需要输入口令!!!!!!!!!!!) 
   // PdfWriter.STRENGTH128BITS, "userpass", "ownerpass", 
   // PdfWriter.AllowCopy | PdfWriter.AllowPrinting 
   writer.setEncryption(PdfWriter.STRENGTH40BITS, "", "", 
     PdfWriter.AllowCopy); 
   document.open(); 
   document.add(new Paragraph("This document is Top Secret!")); 
   document.open(); 
  } catch (Exception e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// 第8种 
public void create8() { 
  String url = System.getProperty("user.dir").toString() + "\\a.jpg"; 
  document = new Document(); 
  try { 
   PdfWriter writerA = PdfWriter.getInstance(document, 
     new FileOutputStream("d:\\Chap0111a.pdf")); 
   PdfWriter writerB = PdfWriter.getInstance(document, 
     new FileOutputStream("d:\\Chap0111b.pdf")); 
   writerB.pause(); 
   Watermark watermark = new Watermark(Image.getInstance(url), 200, 
     420); 
   document.add(watermark); 
   writerB.resume(); 
   HeaderFooter header = new HeaderFooter(new Phrase( 
     "This is a header"), false); 
   document.setHeader(header); 
   document.open(); 

   document.setPageSize(PageSize.A4.rotate()); 
   HeaderFooter footer = new HeaderFooter( 
     new Phrase("This is page: "), true); 
   document.setFooter(footer); 
   document.add(new Paragraph("Hello World")); 

   document.newPage(); 
   document.add(new Paragraph("Hello Earth")); 
   writerA.pause(); 
   document.resetHeader(); 
   writerA.resume(); 

   document.newPage(); 
   document.add(new Paragraph("Hello Sun")); 
   writerA.pause(); 
   document.add(new Paragraph("Remark: the header has vanished!")); 

   writerA.resume(); 
   writerB.pause(); 
   document.resetPageCount(); 

   writerB.resume(); 
   document.newPage(); 

   document.add(new Paragraph("Hello Moon")); 

   writerB.pause(); 
   document 
     .add(new Paragraph("Remark: the pagenumber has been reset!")); 
   writerB.resume(); 
  } catch (Exception e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// ----------------------------第10种设置字体的样式和颜色-------------------------------------- 
public void create10() { 
  try { 
   document = new Document(); 
   PdfWriter.getInstance(document, new FileOutputStream( 
     "d:\\Chap0202.pdf")); 
   document.open(); 
   Phrase phrase0 = new Phrase(); 
   Phrase phrase1 = new Phrase("(1) this is a phrase\n"); 
   Phrase phrase2 = new Phrase( 
     24, 
     "(2) this is a phrase with leading 24. You can only see the difference if the line is 

long enough. Do you see it? There is more space between this line and the previous one.\n"); 
   Phrase phrase3 = new Phrase( 
     "(3) this is a phrase with a red, normal font Courier, size 20. As you can see the 

leading is automatically changed.\n", 
     FontFactory.getFont(FontFactory.COURIER, 20, Font.NORMAL, 
       new Color(255, 0, 0))); 
   Phrase phrase4 = new Phrase(new Chunk("(4) this is a phrase\n")); 
   Phrase phrase5 = new Phrase( 
     18, 
     new Chunk( 
       "(5) this is a phrase in Helvetica, bold, red and size 16 with a 

given leading of 18 points.\n", 
       FontFactory.getFont(FontFactory.HELVETICA, 16, 
         Font.BOLD, new Color(255, 0, 0)))); 
   Phrase phrase6 = new Phrase("(6)"); 
   Chunk chunk = new Chunk(" This is a font: "); 
   phrase6.add(chunk); 
   phrase6.add(new Chunk("Helvetica", FontFactory.getFont( 
     FontFactory.HELVETICA, 12))); 
   phrase6.add(chunk); 
   phrase6.add(new Chunk("Times New Roman", FontFactory.getFont( 
     FontFactory.TIMES_NEW_ROMAN, 12))); 
   phrase6.add(chunk); 
   phrase6.add(new Chunk("Courier", FontFactory.getFont( 
     FontFactory.COURIER, 12))); 
   phrase6.add(chunk); 
   phrase6.add(new Chunk("Symbol", FontFactory.getFont( 
     FontFactory.SYMBOL, 12))); 
   phrase6.add(chunk); 
   phrase6.add(new Chunk("ZapfDingBats", FontFactory.getFont( 
     FontFactory.ZAPFDINGBATS, 12))); 
   phrase6.add("\n"); 
   Phrase phrase7 = new Phrase( 
     "(7) if you don't Add a newline yourself, all phrases are glued to eachother!"); 

   document.add(phrase1); 
   document.add(phrase2); 
   document.add(phrase3); 
   document.add(phrase4); 
   document.add(phrase5); 
   document.add(phrase6); 
   document.add(phrase7); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// -----------------------设置字体的样式-------------------------------- 
public void create11() { 
  document = new Document(); 
  try { 
   PdfWriter.getInstance(document, new FileOutputStream( 
     "d:\\Chap0203.pdf")); 
   document.open(); 
   document.add(new Phrase("What is the " + (char) 945 
     + "-coefficient of the " + (char) 946 + "-factor in the " 
     + (char) 947 + "-equation?\n")); 
   int i = 0; 
   for (i = 913; i < 970; i++) { 
    document.add(new Phrase(" " + i + ": " + (char) i)); 
   } 
   document.add(new Phrase( 
     "-----------------------------------------\n")); 
   List l = new ArrayList(); 
   l.add("aaaaaaaaaaaaa"); 
   l.add("bbbbbbbbbbbbbbbbb"); 
   document.add(new Phrase(16, "\n\n\n")); 
   document 
     .add(new Phrase( 
       -16, 
       "Hello, this is a very long phrase to show you the somewhat odd 

effect of a negative leading. You can write from bottom to top. This is not fully supported. It's something between a feature 

and a bug.")); 
  } catch (FileNotFoundException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } catch (DocumentException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  document.close(); 
} 

// -------------------------------------创建表格--------------------------------------------- 
public void createtable() { 
  document = new Document(); 
  try { 
   PdfWriter.getInstance(document, new FileOutputStream( 
     "d:\\Chap0501.pdf")); 
   document.open(); 
   // 2 行,2列 
   Table aTable = new Table(4, 4); 
   aTable.setBorder(1); 
   // 第一行的列值 
   aTable.addCell("2.2"); 
   aTable.addCell("3.3"); 
   aTable.addCell("2.1"); 
   aTable.addCell("1.3"); 

   // 第2行的列值 
   aTable.addCell("2.2"); 
   aTable.addCell("3.3"); 
   aTable.addCell("2.1"); 
   aTable.addCell("1.3"); 
   document.add(aTable); 

   // 在下一页中建表格 
   document.newPage(); 
   aTable = new Table(4, 4); // 4 rows, 4 columns 
   aTable.addCell("2.2"); 
   aTable.addCell("3.3"); 
   aTable.addCell("2.1"); 
   aTable.addCell("1.3"); 
   document.add(aTable); 

   // ------------------------下一页是从数据库中查询的数据显示到表格中-------------------------- 
   document.newPage(); 
   BookDAO b = new BookDAO(); 
   List l = b.findbookall(); 
   Table mytable = new Table(6, l.size());//6是列数,l.size()得到的是行数 
   // 设置格子的颜色 
   mytable.setDefaultCellBorderColor(Color.blue); 
   mytable.setBorderColor(Color.blue); 
   mytable.setCellpadding(0); 
   // 解决中文问题------------- 
   BaseFont bfChinese = BaseFont.createFont("STSong-Light", 
     "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 
   // 文本内容 
   Font FonttextChinese = new Font(bfChinese, 12, Font.NORMAL); 
   // 表头 
   Font FontheadChinese = new Font(bfChinese, 15, Font.BOLD); 

   // 设置表头 
   Cell h1 = new Cell(new Phrase("编号", FontheadChinese)); 
   h1.setHorizontalAlignment(Element.ALIGN_CENTER); 
   h1.setBackgroundColor(Color.lightGray); 
   
   Cell h2 = new Cell(new Phrase("书籍名称", FontheadChinese)); 
   h2.setHorizontalAlignment(Element.ALIGN_CENTER); 
   h2.setBackgroundColor(Color.lightGray); 
   
   Cell h3 = new Cell(new Phrase("价格", FontheadChinese)); 
   h3.setHorizontalAlignment(Element.ALIGN_CENTER); 
   h3.setBackgroundColor(Color.lightGray); 

   Cell h4 = new Cell(new Phrase("开始时间", FontheadChinese)); 
   h4.setHorizontalAlignment(Element.ALIGN_CENTER); 
   h4.setBackgroundColor(Color.lightGray); 

   Cell h5 = new Cell(new Phrase("结束时间", FontheadChinese)); 
   h5.setHorizontalAlignment(Element.ALIGN_CENTER); 
   h5.setBackgroundColor(Color.lightGray); 

   Cell h6 = new Cell(new Phrase("书刊号", FontheadChinese)); 
   h6.setHorizontalAlignment(Element.ALIGN_CENTER); 
   h6.setBackgroundColor(Color.lightGray); 

   mytable.addCell(h1); 
   mytable.addCell(h2); 
   mytable.addCell(h3); 
   mytable.addCell(h4); 
   mytable.addCell(h5); 
   mytable.addCell(h6); 

   for (int i = 0; i < l.size(); i++) { 
    Books book = (Books) l.get(i); 
    Cell cell0 = new Cell(new Phrase(String.valueOf(book.getBid()), 
      FonttextChinese)); 
    cell0.setHorizontalAlignment(Element.ALIGN_CENTER); 

    Cell cell1 = new Cell(new Phrase(book.getBname(), 
      FonttextChinese)); 
    cell1.setHorizontalAlignment(Element.ALIGN_CENTER); 

    Cell cell2 = new Cell(new Phrase(String.valueOf(book 
      .getBprice()), FonttextChinese)); 
    cell2.setHorizontalAlignment(Element.ALIGN_CENTER); 

    Cell cell3 = new Cell(new Phrase(book.getStime(), 
      FonttextChinese)); 
    cell3.setHorizontalAlignment(Element.ALIGN_CENTER); 

    Cell cell4 = new Cell(new Phrase(book.getEtime(), 
      FonttextChinese)); 
    cell4.setHorizontalAlignment(Element.ALIGN_CENTER); 

    Cell cell5 = new Cell(new Phrase( 
      String.valueOf(book.getBnum()), FonttextChinese)); 
    cell5.setHorizontalAlignment(Element.ALIGN_CENTER); 

    mytable.addCell(cell0); 
    mytable.addCell(cell1); 
    mytable.addCell(cell2); 
    mytable.addCell(cell3); 
    mytable.addCell(cell4); 
    mytable.addCell(cell5); 
   } 
   document.add(mytable); 
  } catch (Exception de) { 
   de.printStackTrace(); 
  } 
  document.close(); 

} 

/** 
  * @param args 
  */ 
public static void main(String[] args) { 
  Text t = new Text(); 
  t.createtable(); 
} 

} 


代码来自:http://jonsion.iteye.com/blog/583539

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics