基于iTextpdf的pdf文件生成

1 篇文章 0 订阅
iTextPdf.jar maven坐标:

          <dependency>
              <groupId>com.itextpdf</groupId>
              <artifactId>itextpdf</artifactId>
              <version>5.4.2</version>
          </dependency>
          
示例代码如下:

import java.io.FileOutputStream;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

public class PDFDemo {
     public static void main(String[] args) throws Throwable {
          Document document = new Document(PageSize.A4, 50, 50, 50, 50);// 页面大小,左、右、上、下边距
          PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\ITextTest.pdf"));// 文档对象的引用,写入文件的绝对名称
          document.open();// 开启文档
          Anchor anchor = new Anchor("First page of the document.");// 锚标记
          anchor.setName("BackToTop");// 锚名字 可用来定位该瞄
          Paragraph paragraph1 = new Paragraph();// 段落
          paragraph1.setSpacingBefore(50);// 段落上边距
          paragraph1.add(anchor);// 绑定锚
          document.add(paragraph1);// 将段落添加到文档
          document.add(new Paragraph("Some more text on the first page with diff color and font type.",
                   FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 255, 0, 0))));// 匿名内部类的形式添加一个新的段落
          Paragraph title1 = new Paragraph("This is Section 1 in Chapter 1",
                   FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new CMYKColor(0, 255, 255, 17)));// 匿名内部类的形式添加一个任意字体和颜色的段落
          Chapter chapter1 = new Chapter(title1, 1);// 新建一个章 默认从一个新页开始且默认显示编号
          chapter1.setNumberDepth(0);// 不会在页面上显示章编号
          Paragraph title11 = new Paragraph("This is Section 2 in Chapter 1",
                   FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new CMYKColor(0, 255, 255, 17)));
          Section section1 = chapter1.addSection(title11);// 从一个章上新建一个节
          Paragraph sst = new Paragraph("This is text comes as part of section 1 of chapter 1.");
          section1.add(sst);// 节上增加一个段落
          sst = new Paragraph("Following is a 3 x 2 table.");
          section1.add(sst);// 节上增加一个段落
          PdfPTable table = new PdfPTable(3);// 新建一个3列的表格
          table.setSpacingBefore(25);// 表格上边距
          table.setSpacingAfter(25);// 表格下边距
          PdfPCell cell1 = new PdfPCell(new Phrase("Header1"));// 单元格
          table.addCell(cell1);// 将单元格添加到表格上 //默认填充完一行(这里是三部分)后开始填充下一行
          PdfPCell cell2 = new PdfPCell(new Phrase("Header2"));
          table.addCell(cell2);
          PdfPCell cell3 = new PdfPCell(new Phrase("Header3"));
          table.addCell(cell3);
          table.addCell("1.1");// 将文字添加到表格上
          table.addCell("1.2");
          table.addCell("1.3");
          section1.add(table);// 讲表格添加到节上
          List list = new List(false, true, 10);// 数字标号,字母标号,缩进 //新建一个表单
          list.add(new ListItem("First item of list"));// 表单上添加item
          list.add(new ListItem("Second item of list"));
          section1.add(list);// 将表单添加到节上
          Image image = Image.getInstance("d:\\Logo.bmp");// 新建一个图片对象
          image.scaleAbsolute(120f, 120f);// 缩放图像
          section1.add(new Paragraph("Following is a image"));
          section1.add(image);// 图像添加到节上
          Paragraph title2 = new Paragraph("Using anchor",
                   FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new CMYKColor(0, 255, 0, 0)));
          section1.add(title2);
          title2.setSpacingBefore(5000);
          Anchor anchor2 = new Anchor("Back To Top");// 新建一个锚对象
          anchor2.setReference("#BackToTop");// 此锚指向另一锚标记
          section1.add(anchor2);// 锚添加到节上
          document.add(chapter1);// 章添加到文档对象上 节是从章上生成的 默认绑定 此时将节上所有对象也添加到了文档对象上
          document.close();// 关闭文档对象 生成文档
          writer.close();// 关闭输出流
     }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值