Java 写入pdf文件
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Chapter;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PDFDemo {
private Color black = new Color(, , ); // 黑色
private Color red = new Color(, , ); // 红色
private Color blue = new Color(, , ); // 蓝色
private int bold = Font.BOLD; // 粗体
private int normal = Font.NORMAL; // 正常字体
private int italic = Font.ITALIC; // 斜体
private int boldItalic = Font.BOLDITALIC; // 粗斜体
private float setting = ; // 首行缩进参数
public Document createDoc(String filename) throws Exception {
// 新建document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, , , , );
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter.getInstance(document, new FileOutputStream(filename));
return document;
}
public void writePdf(String filename, String imgPath) throws Exception {
Document document = createDoc(filename); // 打开文档
document.open(); // 文档里写入
document.add(convertParToChinese("红色字体", , bold, red));
document.add(new Paragraph("\n"));
document.add(convertParToChinese("黑色", , boldItalic, black));
document.add(new Paragraph("\n"));
document.add(convertParToChinese("蓝色", , normal, blue));
document.add(new Paragraph("\n"));
// 文档写入图片
if (checkFile(imgPath)) {
Image image = writeImg(imgPath);
document.add(image);
document.add(new Paragraph("\n"));
}
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n")); // 生成三列表格
PdfPTable table = new PdfPTable(); // 设置表格具体宽度
table.setTotalWidth(); // 设置每一列所占的长度
table.setWidths(new float[] { 50f, 15f, 25f });
PdfPCell cell1 = new PdfPCell();
Paragraph para = new Paragraph("aaaaa");
cell1.setPhrase(para);
table.addCell(cell1);
table.addCell(new PdfPCell(new Phrase("IText")));
table.addCell(new PdfPCell(new Phrase("IText")));
document.add(table);
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n")); // PDF同行显示
Paragraph par = new Paragraph();
Chunk chunk1 = new Chunk(convertChunkByChinese("考试分数:", , bold, black));
Chunk chunk2 = new Chunk(convertChunkByChinese("", , bold, red));
par.add(chunk1);
par.add(chunk2); // 设置整体缩进
par.setFirstLineIndent(setting); // 居中
Paragraph centerPar = convertParToChinese("剧中测试", , italic, black);
centerPar.setAlignment(Element.ALIGN_CENTER);
document.add(par); // 新建章节
// //节标题
Paragraph chapterTitle = new Paragraph(convertParToChinese("章节标题", , boldItalic, blue));
Chapter chapter1 = new Chapter(chapterTitle, );
chapter1.setNumberDepth();
for (int i = ; i < ; i++)
{
Paragraph p = new Paragraph((i + ) + "test!!!!!");
chapter1.add(p);
}
document.add(chapter1); // 5.关闭文档
document.close();
}
public Image writeImg(String imgPath) throws Exception {
Image img = Image.getInstance(imgPath); // 控制图片大小
img.scaleAbsolute(, );
return img;
}
public boolean checkFile(String path) {
File file = new File(path);
if (file.exists()) {
return true;
}
return false;
}
public static Paragraph convertParToChinese(String text, int fontsize, int fontStyle, Color color)
throws Exception {
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(baseFontChinese, fontsize, fontStyle, color);
Paragraph graph = new Paragraph(text, fontChinese);
return graph;
}
public Chunk convertChunkByChinese(String text, int fontsize, int fontStyle, Color color) throws Exception {
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(baseFontChinese, fontsize, fontStyle, color);
Chunk chunk = new Chunk(text, fontChinese);
return chunk;
}
public static void main(String[] args) throws Exception {
PDFDemo pdfDemo = new PDFDemo();
pdfDemo.writePdf("F:\\大数据\\test.pdf",
"C:\\Users\\lenovo\\Pictures\\心2.png");
} }
转载自:http://blog.sina.com.cn/s/blog_c4ffa28f0102wfeq.html
Java 写入pdf文件的更多相关文章
- java写入excel文件poi
java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...
- java生成pdf文件 --- Table
Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...
- 【文件】java生成PDF文件
package test; import java.awt.Color; import java.io.FileOutputStream; import org.junit.Test; import ...
- Java生成PDF文件(转)
原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...
- [itext]Java生成PDF文件
一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...
- JAVA生成PDF文件
生成PDF文件是主要应用的是ITEXT插件 import java.awt.Color; import java.io.File; import java.io.FileOutputStream; i ...
- Java 合并PDF文件
处理PDF文档时,我们可以通过合并的方式,来任意合并几个不同的PDF文件,使我们方便的存储和管理文档.例如,在做毕业设计的时候,封面和论文正文往往是两个PDF文档,但是,上交电子档的时候,需要合二为一 ...
- JAVA中 PDF文件转成TIFF文件的2种方式
由于在工作中使用到了PDF->TIFF的技术,所以稍微研究了一下实现方式,通过资料查阅,暂时发现了2种方式,2种方式有所区别:第一种方式转化后的tiff文件是黑白的,第二种方式转化后的tiff文 ...
- Java 创建PDF文件包的2种方法
1. 概述 PDF文件包可方便在仅打开一个窗口的情况下阅读多个文档,通过将多个PDF文档或其他非PDF文档封装在一起,打开文件包后可以随意切换查看文件包中的文档,在需要编辑更改的情况,也可以打开文本包 ...
随机推荐
- Sqlserver中的几把锁和.net中的事务级别
当数据表被事务锁定后,我们再进行select查询时,需要为with(锁选项)来查询信息,如果不加,select将会被阻塞,直到锁被释放,下面介绍几种SQL的锁选项 SQL的几把锁 NOLOCK(不加锁 ...
- 利用Thread.stop完成方法执行超时中断
示例代码可以从github上获取 https://github.com/git-simm/simm-framework.git 接上篇博客<FutureTask子线程取消执行的状态判断> ...
- ORCHARD学习教程-介绍
ORCHARD 是什么? Orchard 是由微软公司创建,基于 ASP.NET MVC 技术的免费开源内容管理系统: 可用于建设博客.新闻门户.企业门户.行业网站门户等各种网站 简单易用的后台界面 ...
- WebApi与MVC Route 问题整理
1. 为WebAPI添加 Area后,完成了CustomControllerSelector的制定. 跟踪WebAPI底层,整理WebAPI源码后发现几个问题: 1. 使用Area的controlle ...
- Socket网络通讯
网络编程 使用C#进行网络编程时,通常都需要用到System.Net命名空间.System.Net.Sockets命名空间和System.Net.Mail命名空间: 1. System.Net命名空间 ...
- OpenResty 最佳实践 (2)
此文已由作者汤晓静授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. lua 协程与 nginx 事件机制结合 文章前部分用大量篇幅阐述了 lua 和 nginx 的相关知识,包 ...
- <select>标签默认值设置
<td> <label>操作类型:</label> <select id="operation_type" class="com ...
- 手动给kvm虚机挂载lvm卷
1.查看计算节点上虚机挂载的卷 [root@xgto01n010243186070 ~]# virsh domblklist instance- Target Source ------------- ...
- HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple
题意大致是给你一个整数n,让你确定是否有三个正整数x,y,z既能被n整除,又能x+y+z=n,并使xyz最大 从中根据规律可以看出,只有被3或被4整除的数才能满足题目要求 被3整除的最大值为n^3/3 ...
- HDU6301-2018ACM暑假多校联合训练1004-Distinct Values
题意是一个长度为n的序列,给你m组区间(l,r),在这个区间里不能填入重复的数字,同时使整个序列字典序最小 同学用的优先队列,标程里使用的是贪心同时使用set维护答案序列 贪心是先采用pre数组来确定 ...