iTEXT官方网站:

https://developers.itextpdf.com/examples

常用属性:

public static void main(String[] args) throws DocumentException, IOException {//创建文件Document document = new Document();// 也可以自定义文件页面大小// Rectangle pagesize = new Rectangle(216f, 720f);// Document document = new Document(pagesize, 36f, 72f, 108f, 180f);//建立一个书写器PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/hello.pdf"));//打开文件document.open();//中文字体,解决中文不能显示问题BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);Font blackFont = new Font(bfChinese);blackFont.setColor(BaseColor.BLACK);  // 设置字体颜色为黑色Font blackFontB = new Font(bfChinese,8);  // 设置字体大小为8document.add(new Paragraph("你好",blackFont));  //添加内容document.addTitle("标题");  //标题document.addAuthor("作者");  //作者document.addSubject("主题");  //主题document.addKeywords("关键字");  //关键字document.addCreationDate();    //创建时间document.addCreator("应用程序"); //应用程序// 添加图片Image image1 = Image.getInstance("D:hello.JPG");image1.setAbsolutePosition(100f, 550f);   //设置图片位置的x轴和y轴image1.scaleAbsolute(200, 200);  //设置图片的宽度和高度document.add(image1);  //将图片1添加到pdf文件中//添加有序列表List orderList = new List(List.ORDERED);orderList.add(new ListItem("Item one"));orderList.add(new ListItem("Item two"));orderList.add(new ListItem("Item three"));document.add(orderList);//创建章节Paragraph chapterTitle = new Paragraph("段落标题xxxx", greenFont);Chapter chapter1 = new Chapter(chapterTitle, 1);chapter1.setNumberDepth(0);Paragraph sectionTitle = new Paragraph("部分标题", greenFont);Section section1 = chapter1.addSection(sectionTitle);Paragraph sectionContent = new Paragraph("部分内容", blueFont);section1.add(sectionContent);document.add(chapter1);// 设置段落Paragraph paragraph = new Paragraph("段落", titlefont);paragraph.setAlignment(1); //设置文字居中 0靠左   1,居中     2,靠右paragraph.setIndentationLeft(12); //设置左缩进paragraph.setIndentationRight(12); //设置右缩进paragraph.setFirstLineIndent(24); //设置首行缩进paragraph.setLeading(50f); //行间距paragraph.setSpacingBefore(5f); //设置段落上空白paragraph.setSpacingAfter(10f); //设置段落下空白document.add(paragraph);// 直线Paragraph p1 = new Paragraph(); p1.add(new Chunk(new LineSeparator()));document.add(p1);// 点线Paragraph p2 = new Paragraph(); p2.add(new Chunk(new DottedLineSeparator()));document.add(p2);// 超链接Anchor anchor = new Anchor("baidu");anchor.setReference("www.baidu.com");document.add(anchor);// 定位Anchor gotoP = new Anchor("goto"); gotoP.setReference("#top");document.add(gotoP);// 设置密码(要先设置密码,再打开文件document.open();)//用户密码String userPassword = "123456";//拥有者密码String ownerPassword = "hd";writer.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);// 打开文件document.open();//添加内容document.add(new Paragraph("password !!!!"));// 设置只读权限(也是先设置权限,再打开文件document.open();)writer.setEncryption("".getBytes(), "".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);//关闭文档document.close();//关闭书写器writer.close();
}

表格

public static void main(String[] args) throws DocumentException, IOException {//创建文件Document document = new Document();//建立一个书写器PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/hello.pdf"));//打开文件document.open();//中文字体,解决中文不能显示问题BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);Font blackFont = new Font(bfChinese);blackFont.setColor(BaseColor.BLACK);// 创建表格(1代表只有1列)PdfPTable table = new PdfPTable(1);table.setWidthPercentage(100);  // 表格宽度比例为100%table.setTotalWidth(500);  // 设置表格的宽度table.setTotalWidth(new float[] { 160, 70, 130, 100 });  // 设置每列宽度table.setLockedWidth(true);  // 锁住宽度table.setSpacingBefore(10f);  // 设置表格上面空白宽度table.getDefaultCell().setBorder(0);  // 设置表格默认为无边框PdfPCell cell = new PdfPCell(new Phrase("单元格"));  // 新建单元格cell.setBorderColor(BaseColor.BLUE);  // 设置边框颜色cell.setBackgroundColor(BaseColor.ORANGE);  // 设置背景颜色cell.setRowspan(2);  // 设置跨两行cell.setColspan(2);  // 设置占用列数2列cell.setPaddingLeft(10);  // 设置距左边的距离cell.setFixedHeight(20);  // 设置高度cell.setHorizontalAlignment(Element.ALIGN_CENTER);  // 设置内容水平居中显示cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  // 设置垂直居中cell.disableBorderSide(1);  //隐藏上边框cell.disableBorderSide(2);  //隐藏下边框cell.disableBorderSide(3);  //隐藏上、下边框cell.disableBorderSide(4);  //隐藏左边框cell.disableBorderSide(5);  //隐藏左、上边框cell.disableBorderSide(6);  //隐藏左、下边框cell.disableBorderSide(7);  //隐藏左、上、下边框cell.disableBorderSide(8);  //隐藏右边框cell.disableBorderSide(9);  //隐藏右、上边框cell.disableBorderSide(10);  //隐藏右、下边框cell.disableBorderSide(11);  //隐藏右、上、下边框cell.disableBorderSide(12);  //隐藏左、右边框cell.disableBorderSide(13);  //隐藏上、左、右边框cell.disableBorderSide(14);  //隐藏下、左、右边框cell.disableBorderSide(15);   //隐藏全部table.addCell(cell);  // 将单元格加入表格中// 增加一个条形码到表格Barcode128 code128 = new Barcode128();code128.setCode("14785236987541");code128.setCodeType(Barcode128.CODE128);// 生成条形码图片Image code128Image = code128.createImageWithBarcode(cb, null, null);// 加入到表格PdfPCell cellcode = new PdfPCell(code128Image, true);cellcode.setHorizontalAlignment(Element.ALIGN_CENTER);cellcode.setVerticalAlignment(Element.ALIGN_MIDDLE);cellcode.setFixedHeight(30);table.addCell(cellcode);document.add(table);//关闭文档document.close();//关闭书写器writer.close();
}

其中表格是可以嵌套的,也就是说一个表格可以加入到另一个表格的单元格中。

压缩PDF文件

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;public class PDFZip {public static void main(String[] args)throws DocumentException, IOException {// 压缩多个PDF文件ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(RESULT));for (int i = 1; i <= 3; i++) {ZipEntry entry = new ZipEntry("test" + i + ".pdf");zip.putNextEntry(entry);Document document = new Document();PdfWriter writer = PdfWriter.getInstance(document, zip);writer.setCloseStream(false);document.open();document.add(new Paragraph("testpdfzip " + i));document.close();zip.closeEntry();}zip.close();}
}

iTEXT常用属性设置相关推荐

  1. 列标题 如何删除gridcontrol_DEV控件GridControl常用属性设置(转)

    1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView.Ad ...

  2. [转载]DEV控件:gridControl常用属性设置

    1.隐藏最上面的GroupPanel   gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值   sValue=Table.Rows ...

  3. DEV控件:gridControl常用属性设置

    1.隐藏最上面的GroupPanel   gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值   sValue=Table.Rows ...

  4. chart.js使用学习——柱状图(2:常用属性设置)

      本文介绍柱状图常用属性及效果.柱状图中有部分常用属性与折线图用法相同,本文仅列出这些属性的简要说明,不再详细说明. base   设置图形绘制时的基准值,数值型,默认值为空,设置的值为数值轴上的值 ...

  5. android中用代码设置edittext属性为密码,Android中EditText常用属性设置

    EditText继承关系:View–>TextView–>EditText 常用属性如下:android:layout_gravity="center_vertical" ...

  6. DevExpress的TreeList的常用属性设置以及常用事件

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  7. qcustomplot圆_QCustomPlot使用心得五:坐标轴常用属性设置

    先看轴部分的名称约定,根据名称就可以修改对应的属性了 1.显示坐标轴 默认只显示左y轴和下边的x轴,调用setVisible(bool)设置轴是否显示 1 customplot->yAxis2- ...

  8. QCustomPlot使用心得五:坐标轴常用属性设置

    先看轴部分的名称约定,根据名称就可以修改对应的属性了 1.显示坐标轴 默认只显示左y轴和下边的x轴,调用setVisible(bool)设置轴是否显示 customplot->yAxis2-&g ...

  9. 第四次网页前端培训笔记(css常用属性设置)

    4.1.背景 4.1.1.background-color(设置元素的背景颜色) <style type="text/css">#div1{width: 100px;h ...

  10. rap2接口mock数据初始值和常用属性设置

    常用mock数据初始值 id: "@id()",//得到随机的id,对象 name: "@cname()",//随机生成中文名字 username:/[a-z] ...

最新文章

  1. Eigen:C++中Eigen库的安装与学习
  2. 深度神经网络在NLP的应用!
  3. HTML5 Web app开发工具Kendo UI Web中图像浏览器的使用
  4. Android Camera架构分析
  5. 克隆需要验证_GeneCopoeia基因克隆
  6. c linux time微秒_qt linux系统获取当前时间(精确到毫秒、微秒)-Go语言中文社区...
  7. Linux手动释放缓存的方法
  8. 读取速度500m/s和1000m/s的硬盘,装系统使用起来有区别吗?
  9. JetS3t使用说明
  10. 选择排序SelectSort
  11. mybatis使用collection查询集合属性规则
  12. 先测试再开发?TDD测试驱动开发了解一下?
  13. 提高项目39-电子词典
  14. 积分形式的詹森不等式_均值不等式及其积分形式
  15. 道德经和译文_道德经全文和译文
  16. Logback日志配置和简单使用
  17. VuePress 开发技术文档网站,管理.md文件,生成静态网站
  18. 图论复习之强连通分量以及缩点—Tarjan算法
  19. ElasticSearch系列03:ES的数据类型
  20. 【C++入门必看】C++从0到1入门编程

热门文章

  1. Python3.WRF的投影转换
  2. notes for 电子技术技术(模拟部分) 康华光
  3. 迅雷 iOS 版终于复活,不限速,完美支持BT磁力下载
  4. 微信小程序搭建tabbar
  5. Hive从身份证号中提取相关信息_性别_年龄_出生日期_详细地址
  6. laydate点击输入框闪一下不见了_解决layui laydate 时间控件一闪而过的问题
  7. nbu备份nas文件服务器,NBU备份恢复实践
  8. brew的安装以及使用
  9. 交互设计精髓之理解输出
  10. 工具DebugView、PCHunter、Procexp、Procmon