概述

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(0, 0, 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,70, 120, 100);

测试代码

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

参考

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

itext-2.0.8.jar

itext实现横向pdf打印相关推荐

  1. iText PDF 打印pdf

    iText是一个流行的Java库,可用于处理PDF文件.使用iText,您可以将PDF文档打印到打印机上. 以下是在Java中使用iText打印PDF文档的基本步骤: 创建PdfReader对象,用于 ...

  2. IText实现对PDF文档属性的基本设置

    一.Itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...

  3. [itext]Java生成PDF文件

    一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...

  4. 分享一个VS写的PDF打印控件

    控件CLSID A04AC669-CB70-4E8B-9CC0-9B73FC153784 功能描述 自己写的一个控件,某人寿公司的需求,可惜最后没用到. 需求如下: 1.给你一个一个PDF文档,让你打 ...

  5. 《SpringBoot2.0 实战》系列-整合FlyingSaucer + thymeleaf 实现模板文件转pdf打印

    前言 最近,接到一个模板打印pdf的任务,后来网上找了很多案例,本文做下记录. 如何开始 添加依赖包 <!-- thymeleaf --> <dependency><gr ...

  6. 使用IText组件在PDF文档上绘制椭圆形印章的算法分析及代码分享

    1. 引言 PDF是一种和操作系统及平台无关的.可移植的电子文件格式,其以PostScript语言图像模型为基础,无论在哪种打印机上,都可保证精确的颜色和准确的打印效果.PDF将真实地再现原稿的每一个 ...

  7. iText如何提取PDF中的数据——1. 总览

    作者:CuteXiaoKe 微信公众号:CuteXiaoKe | 原文   最近收到大家很多的私信提问,也是大家比较关心的问题:如果我有一个PDF,我该如何使用iText获取PDF里面的内容呢,比如文 ...

  8. win32print设置打印机属性进行pdf打印

    win32print设置打印机属性 设置打印机的属性来进行pdf打印,比如纸张大小,纸张类型,横\纵向,输入纸盘,着实费了一些时间去搜索资料.主要参考以下 http://timgolden.me.uk ...

  9. HTML生成PDF模板(Java iText+FreeMarker生成PDF(HTML转PDF))

    Java iText+FreeMarker生成PDF(HTML转PDF) 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等.方便用户查看,下载,打 ...

  10. 使用iText库创建PDF文件

    前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...

最新文章

  1. php手工注入语句,PHP+MySQL 手工注入语句大全
  2. python背包问题并行_python基于递归解决背包问题详解
  3. SAP Fiori My task里complete checkbox的处理
  4. vant表单组件+iconfont组合使用 - 代码篇
  5. python爬视频网站数据_python爬虫基础应用----爬取无反爬视频网站
  6. 华三交换机配置access命令_华3交换机配置命令大全
  7. 怎么打开微信的定位服务器地址,打开微信附近的人经常看到无法确定你的位置信息是怎么回事...
  8. 物联网|ZETA技术助力远超抄表实现智能化、精细化
  9. 李诞是怎么把吐槽变成一门生意的?
  10. 弱占优策略--Weakly Dominant Strategy
  11. QQ20岁:20年版本迭代只做一件事情!
  12. 网络安全课程设计Java实现DES加密算法(可视化界面)代码+设计文档
  13. win11 删除自带的微软输入法
  14. NVM简单使用及出现NVM安装后没有npm的解决方法。
  15. LOTO课5:三极管音频放大电路实践
  16. 360路由器远程连接服务器,“怎样用动态域名实现路由器的远程配置”的解决方案...
  17. npm私有库(nexus)-安装nexus
  18. postman查看完整的请求信息
  19. maya_mel语言中多边形点的提取和遍历操作方法
  20. 减半行情背后:矿业面临哪些新机遇?

热门文章

  1. 华为鸿蒙OS精品资料汇总,持续更新中
  2. 系统架构师笔记——数据库
  3. ebay增强可用性的4个原则
  4. python中outside function_运行提示SyntaxError: 'yield' outside function
  5. nginx is outside location
  6. centos7.9安装zabbix+添加局域网下其他客户机
  7. C++基础(C++Primer学习)
  8. INTO CORRESPONDING FIELDS OF、去掉前导零,增加前导0。不显示物料号的前导零
  9. python龙旋风图形代码_python – 无法使用gunicorn运行龙卷风应用程序
  10. ecshop 添加php标签,ecshop模板调用标签大全