itext实现横向pdf打印

概述

Document是itext的基础,你可以添加文档数据(用户阅读的信息)和元数据(pdf内部使用的信息)。在创建document对象时,你可以定义page size,page color and page margins。

 

构造函数

 

 

查看一下API,Document的构造函数有三个。

 

其中第一个Document给size,color,margins都设置了默认值。查看源代码,默认为Document(PageSize.A4, 36, 36, 36, 36);

 

第二个构造函数就可以自定义页面的大小了,例如:

Java代码   收藏代码
  1. Rectangle rect = new Rectangle(800,600);  
  2. Document document = new Document(rect);  

 Rectangle指定了宽为800,高位600的页面。

 

Rectangle

 

在这里有必要看看Rectangle

我们看看一个函数做了什么

 

Java代码   收藏代码
  1. /** 
  2.  * Constructs a <CODE>Rectangle</CODE> -object starting from the origin 
  3.  * (0, 0). 
  4.  *  
  5.  * @param urx 
  6.  *            upper right x 
  7.  * @param ury 
  8.  *            upper right y 
  9.  */  
  10. public Rectangle(float urx, float ury) {  
  11.     this(00, urx, ury);  
  12. }  

 哦,原来是左下角(0,0)为起点,右上角为宽高。如图所示:

 

当然,通过public Rectangle(float llx, float lly, float urx, float ury)可以随意改变左下角的位置。

 

Java代码   收藏代码
  1. /** 
  2.      * Constructs a <CODE>Rectangle</CODE> -object. 
  3.      *  
  4.      * @param llx 
  5.      *            lower left x 
  6.      * @param lly 
  7.      *            lower left y 
  8.      * @param urx 
  9.      *            upper right x 
  10.      * @param ury 
  11.      *            upper right y 
  12.      */  
  13.     public Rectangle(float llx, float lly, float urx, float ury) {  
  14.         this.llx = llx;  
  15.         this.lly = lly;  
  16.         this.urx = urx;  
  17.         this.ury = ury;  
  18.     }  

 Page Size

理论上将,你可以随意的创建页面的大小,但是不同的PDF规范,强制规范了页面的大小。这一点,比较抽象,我就不详细介绍了,具体可以翻阅itext_in_action_2006 2.1.1小结。

 

Itext提供了一个很实用的类PageSize,它的作用就是返回static final Rectangle对象的集合。提供了标准化的页面大小。例如:

 

Java代码   收藏代码
  1. Document document = new Document(PageSize.A4)  

横向打印

 

接下来有个很有趣的函数rotate()。

 

在打印的时候,经常需要横向打印。有了rotate,这下方便了。

 

Java代码   收藏代码
  1. Document document = new Document(PageSize.A4.rotate());  

 

还有Page color和Page Margins,

 

Java代码   收藏代码
  1. Rectangle rect = PageSize.A4;  
  2. rect.setBackgroundColor(Color.BLUE);  
  3. Document document = new Document(rect);  
 

 

Java代码   收藏代码
  1. Document document = new Document(PageSize.A4, 36,70120100);  
 

 

  测试代码

 

Java代码   收藏代码
  1. import java.awt.Color;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4.   
  5. import com.lowagie.text.Document;  
  6. import com.lowagie.text.DocumentException;  
  7. import com.lowagie.text.PageSize;  
  8. import com.lowagie.text.Paragraph;  
  9. import com.lowagie.text.Rectangle;  
  10. import com.lowagie.text.pdf.PdfWriter;  
  11.   
  12. import junit.framework.TestCase;  
  13. /** 
  14.  * @blog http://reymont.iteye.com/ 
  15.  * @MSN reymont.li@hotmail.com 
  16.  * @author reymont.li 
  17.  * @version create time:2011-7-29 上午10:01:44 
  18.  */  
  19. public class DocumentStudy extends TestCase{  
  20.     public void testNewDocumentMargin(){  
  21.         Document document = new Document(PageSize.A4, 36,70120100);  
  22.         try {  
  23.             PdfWriter.getInstance(  
  24.                     document,  
  25.                     new FileOutputStream("resource/NewDocumentMargin.pdf"));  
  26.             document.open();  
  27.             document.add(new Paragraph("Hello World"));  
  28.         } catch (DocumentException de) {  
  29.             System.err.println(de.getMessage());  
  30.         } catch (IOException ioe) {  
  31.             System.err.println(ioe.getMessage());  
  32.         }  
  33.         document.close();  
  34.     }  
  35.       
  36.     public void testNewDocumentColor(){  
  37.         Rectangle rect = PageSize.A4;  
  38.         rect.setBackgroundColor(Color.BLUE);  
  39.         Document document = new Document(rect);  
  40.         try {  
  41.             PdfWriter.getInstance(  
  42.                     document,  
  43.                     new FileOutputStream("resource/NewDocumentColor.pdf"));  
  44.             document.open();  
  45.             document.add(new Paragraph("Hello World"));  
  46.         } catch (DocumentException de) {  
  47.             System.err.println(de.getMessage());  
  48.         } catch (IOException ioe) {  
  49.             System.err.println(ioe.getMessage());  
  50.         }  
  51.         document.close();  
  52.     }  
  53.       
  54.     public void testNewDocumentRotate(){  
  55.         Document document = new Document(PageSize.A4.rotate());  
  56.         try {  
  57.             PdfWriter.getInstance(  
  58.                     document,  
  59.                     new FileOutputStream("resource/NewDocumentRotate.pdf"));  
  60.             document.open();  
  61.             document.add(new Paragraph("Hello World"));  
  62.         } catch (DocumentException de) {  
  63.             System.err.println(de.getMessage());  
  64.         } catch (IOException ioe) {  
  65.             System.err.println(ioe.getMessage());  
  66.         }  
  67.         document.close();  
  68.     }  
  69.       
  70.     public void testNewDocument2(){  
  71.         Rectangle rect = new Rectangle(500,500,800,600);  
  72.         Document document = new Document(rect);  
  73.         try {  
  74.             PdfWriter.getInstance(  
  75.                     document,  
  76.                     new FileOutputStream("resource/NewDocument2.pdf"));  
  77.             document.open();  
  78.             document.add(new Paragraph("Hello World"));  
  79.         } catch (DocumentException de) {  
  80.             System.err.println(de.getMessage());  
  81.         } catch (IOException ioe) {  
  82.             System.err.println(ioe.getMessage());  
  83.         }  
  84.         document.close();  
  85.     }  
  86.       
  87.     public void testNewDocument1(){  
  88.         Rectangle rect = new Rectangle(0,0,800,600);  
  89.         Document document = new Document(rect);  
  90.         try {  
  91.             PdfWriter.getInstance(  
  92.                     document,  
  93.                     new FileOutputStream("resource/NewDocument1.pdf"));  
  94.             document.open();  
  95.             document.add(new Paragraph("Hello World"));  
  96.         } catch (DocumentException de) {  
  97.             System.err.println(de.getMessage());  
  98.         } catch (IOException ioe) {  
  99.             System.err.println(ioe.getMessage());  
  100.         }  
  101.         document.close();  
  102.     }  
  103.       
  104.     public void testNewDocument(){  
  105.         Rectangle rect = new Rectangle(800,600);  
  106.         Document document = new Document(rect);  
  107.         try {  
  108.             PdfWriter.getInstance(  
  109.                     document,  
  110.                     new FileOutputStream("resource/NewDocument1.pdf"));  
  111.             document.open();  
  112.             document.add(new Paragraph("Hello World"));  
  113.         } catch (DocumentException de) {  
  114.             System.err.println(de.getMessage());  
  115.         } catch (IOException ioe) {  
  116.             System.err.println(ioe.getMessage());  
  117.         }  
  118.         document.close();  
  119.     }  
  120.       
  121. }  

 

参考

IText.Manning.iText.in.Action.Dec.2006.pdf

itext-2.0.8.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值