Java用iText5生成PDF

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

document.add();

关闭文档

document.close();

段落Paragraph

Paragraph pt=new Paragraph(name,headfont);//设置字体样式

pt.setAlignment(1);//设置文字居中 0靠左 1,居中 2,靠右

pt.setIndentationLeft(12);// 左缩进

pt.setIndentationRight(12);// 右缩进

pt.setFirstLineIndent(24);// 首行缩进

paragraph.setLeading(20f); //行间距

paragraph.setSpacingBefore(5f); //设置段落上空白

paragraph.setSpacingAfter(10f); //设置段落下空白

表格table

Table table =new Table(4);//括号参数表示列

int width[] = {10,40,40};//设置每列宽度比例

table.setWidths(width);

table.setWidth(60);//占页面宽度比例

table.setAlignment(Element.ALIGN_LEFT);//居左

table.setAutoFillEmptyCells(true);//自动填满

table.setBorderWidth((float)0.1);//表格边框线条宽度

table.setPadding(1);//边距:单元格的边线与单元格内容的边距

table.setSpacing(0);//间距:单元格与单元格之间的距离

table.addCell(new Paragraph(“name”),“单元格”));//添加单元格内容

table.endHeaders();//每页都会显示表头

实例

生成一个只有标题的pdf

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;

import com.itextpdf.text.Chapter;

import com.itextpdf.text.Document;

import com.itextpdf.text.Font;

import com.itextpdf.text.PageSize;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.BaseFont;

import com.itextpdf.text.pdf.PdfWriter;

public class PdfJava

{

public void pdfTest(String filePath) throws Exception

{

Document document = new Document(PageSize.A4, 36, 36, 36, 36);

PdfWriter.getInstance(document, new FileOutputStream(filePath));

document.open();

//中文字体,解决中文不能显示问题

BaseFont bfChinese = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.EMBEDDED);

Font cusTitleFont = new Font(bfChinese);

cusTitleFont.setSize(16);

Font cusFont = new Font(bfChinese);

cusFont.setSize(9);

//添加标题开始

Font redFont = new Font(bfChinese);

redFont.setColor(BaseColor.RED);//设置颜色

redFont.setSize(20);//设置字体大小

Paragraph chapterTitle = new Paragraph(“java生成pdf文件”, redFont);//设置内容

chapterTitle.setAlignment(1);

Chapter chapter1 = new Chapter(chapterTitle, 1);

chapter1.setNumberDepth(0);

document.add(chapter1);

//添加标题结束

//关闭文档

document.close();

}

/**

  • @param args

*/

public static void main(String[] args)

{

try

{

String filePath=“d:\test1.pdf”;

new PdfJava().pdfTest(filePath);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

文件如下:

生成table内容

import java.io.FileOutputStream;

import java.util.List;

import com.itextpdf.text.BaseColor;

import com.itextpdf.text.Chapter;

import com.itextpdf.text.Document;

import com.itextpdf.text.Element;

import com.itextpdf.text.Font;

import com.itextpdf.text.PageSize;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.BaseFont;

import com.itextpdf.text.pdf.PdfPCell;

import com.itextpdf.text.pdf.PdfPRow;

import com.itextpdf.text.pdf.PdfPTable;

import com.itextpdf.text.pdf.PdfWriter;

public class PdfJava

{

public void pdfTest(String filePath) throws Exception

{

Document document = new Document(PageSize.A4, 36, 36, 36, 36);

PdfWriter.getInstance(document, new FileOutputStream(filePath));

document.open();

//中文字体,解决中文不能显示问题

BaseFont bfChinese = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.EMBEDDED);

Font cusTitleFont = new Font(bfChinese);

cusTitleFont.setSize(16);

Font cusFont = new Font(bfChinese);

cusFont.setSize(9);

//添加标题开始

Font redFont = new Font(bfChinese);

redFont.setColor(BaseColor.RED);//设置颜色

redFont.setSize(20);//设置字体大小

Paragraph chapterTitle = new Paragraph(“java生成pdf文件”, redFont);//设置内容

chapterTitle.setAlignment(1);

Chapter chapter1 = new Chapter(chapterTitle, 1);

chapter1.setNumberDepth(0);

document.add(chapter1);

//添加标题结束

//添加一个内容段落

document.add(new Paragraph(“表格信息”, cusTitleFont));

// 表格.

PdfPTable table = new PdfPTable(6);

table.setWidthPercentage(100); // 宽度100%填充

table.setSpacingBefore(10f); // 前间距

table.setSpacingAfter(10f); // 后间距

List listRow = table.getRows();

//设置列宽

float[] columnWidths = { 2f, 2f, 2f ,2f, 2f, 2f};

table.setWidths(columnWidths);

//行

PdfPCell cells1[] = new PdfPCell[6];

PdfPRow row1 = new PdfPRow(cells1);

//单元格

cells1[0] = new PdfPCell(new Paragraph(“第一行第1列:”, cusFont));//单元格内容

cells1[0].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

cells1[1] = new PdfPCell(new Paragraph(“第一行第1列值”, cusFont));

cells1[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左

cells1[2] = new PdfPCell(new Paragraph(“第一行第2列:”, cusFont));//单元格内容

cells1[2].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

cells1[3] = new PdfPCell(new Paragraph(“第一行第2列值”, cusFont));

cells1[3].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左

cells1[4] = new PdfPCell(new Paragraph(“第一行第3列:”, cusFont));//单元格内容

cells1[4].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

cells1[5] = new PdfPCell(new Paragraph(“第一行第3列值”, cusFont));

cells1[5].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左

//把第一行添加到集合

listRow.add(row1);

//行

PdfPCell cells2[] = new PdfPCell[6];

PdfPRow row2 = new PdfPRow(cells2);

//单元格

cells2[0] = new PdfPCell(new Paragraph(“第二行第1列:”, cusFont));//单元格内容

cells2[0].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

cells2[1] = new PdfPCell(new Paragraph(“第二行第1列值”, cusFont));

cells2[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左

cells2[2] = new PdfPCell(new Paragraph(“第二行第2列:”, cusFont));//单元格内容

cells2[2].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

总结

就写到这了,也算是给这段时间的面试做一个总结,查漏补缺,祝自己好运吧,也希望正在求职或者打算跳槽的 程序员看到这个文章能有一点点帮助或收获,我就心满意足了。多思考,多问为什么。希望小伙伴们早点收到满意的offer! 越努力越幸运!

金九银十已经过了,就目前国内的面试模式来讲,在面试前积极的准备面试,复习整个 Java 知识体系将变得非常重要,可以很负责任的说一句,复习准备的是否充分,将直接影响你入职的成功率。但很多小伙伴却苦于没有合适的资料来回顾整个 Java 知识体系,或者有的小伙伴可能都不知道该从哪里开始复习。我偶然得到一份整理的资料,不论是从整个 Java 知识体系,还是从面试的角度来看,都是一份含技术量很高的资料。

三面蚂蚁核心金融部,Java开发岗(缓存+一致性哈希+分布式)

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
就写到这了,也算是给这段时间的面试做一个总结,查漏补缺,祝自己好运吧,也希望正在求职或者打算跳槽的 程序员看到这个文章能有一点点帮助或收获,我就心满意足了。多思考,多问为什么。希望小伙伴们早点收到满意的offer! 越努力越幸运!**

金九银十已经过了,就目前国内的面试模式来讲,在面试前积极的准备面试,复习整个 Java 知识体系将变得非常重要,可以很负责任的说一句,复习准备的是否充分,将直接影响你入职的成功率。但很多小伙伴却苦于没有合适的资料来回顾整个 Java 知识体系,或者有的小伙伴可能都不知道该从哪里开始复习。我偶然得到一份整理的资料,不论是从整个 Java 知识体系,还是从面试的角度来看,都是一份含技术量很高的资料。

[外链图片转存中…(img-IYssDs8L-1714460259838)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 21
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JavaPDF指定坐标中写入文本,可以使用iText库。以下是一个简单的示例代码: ```java import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfWriter; public class PdfWriterExample { public static void main(String[] args) { try { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); Font font = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD); Paragraph paragraph = new Paragraph("Hello World!", font); paragraph.setAlignment(Element.ALIGN_LEFT); paragraph.setLeading(0, 1.2f); paragraph.setIndentationLeft(50); paragraph.setIndentationRight(50); paragraph.setSpacingBefore(50); cb.beginText(); cb.setFontAndSize(font.getBaseFont(), font.getSize()); cb.setTextMatrix(100, 700); cb.showText("Hello World!"); cb.endText(); document.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例中,我们使用了iText库创建了一个PDF文档,并向其中写入了一个"Hello World!"的文本。我们使用了PdfContentByte对象来操作PDF文档的内容,我们可以使用beginText()和endText()方法来定义文本的起始和结束位置,使用setFontAndSize()方法来设置文本的字体和大小,使用setTextMatrix()方法来指定文本的坐标位置,使用showText()方法来显示文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值