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文件的更多相关文章

  1. java写入excel文件poi

    java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...

  2. java生成pdf文件 --- Table

    Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...

  3. 【文件】java生成PDF文件

    package test; import java.awt.Color; import java.io.FileOutputStream; import org.junit.Test; import ...

  4. Java生成PDF文件(转)

    原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...

  5. [itext]Java生成PDF文件

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

  6. JAVA生成PDF文件

    生成PDF文件是主要应用的是ITEXT插件 import java.awt.Color; import java.io.File; import java.io.FileOutputStream; i ...

  7. Java 合并PDF文件

    处理PDF文档时,我们可以通过合并的方式,来任意合并几个不同的PDF文件,使我们方便的存储和管理文档.例如,在做毕业设计的时候,封面和论文正文往往是两个PDF文档,但是,上交电子档的时候,需要合二为一 ...

  8. JAVA中 PDF文件转成TIFF文件的2种方式

    由于在工作中使用到了PDF->TIFF的技术,所以稍微研究了一下实现方式,通过资料查阅,暂时发现了2种方式,2种方式有所区别:第一种方式转化后的tiff文件是黑白的,第二种方式转化后的tiff文 ...

  9. Java 创建PDF文件包的2种方法

    1. 概述 PDF文件包可方便在仅打开一个窗口的情况下阅读多个文档,通过将多个PDF文档或其他非PDF文档封装在一起,打开文件包后可以随意切换查看文件包中的文档,在需要编辑更改的情况,也可以打开文本包 ...

随机推荐

  1. js 清空 input[type=file]的值

    js 不能操作 input[type=file]但你可以将这个 input 的 dom 元素删除掉,再新增一个,或者替换掉 $("#UploadFile").replaceWith ...

  2. [GO]二维数组的介绍

    package main import "fmt" func main() { ][]int // 有几个方括号就是几维数据 // 有几个方括号就需要几重循环 k := ; i&l ...

  3. 关于Java异常一段很有意思的代码

    今天学习了Java的异常,讲到try-catch-finally时,老师演示了一段代码,觉得很有意思,很能反映出其执行的过程,让自己有点绕,特意记录一下. 只要代码执行到try代码内部, 不管有没有异 ...

  4. JAVA的IO处理【转】

    I/O简介 IO是输入和输出的简称,在实际的使用时,输入和输出是有方向的.就像现实中两个人之间借钱一样,例如A借钱给B,相对于A来说是借出,而相对于B来说则是借入.所以在程序中提到输入和输出时,也需要 ...

  5. 装饰(Decorator)模式

    一. 装饰(Decorator)模式 装饰(Decorator)模式又名包装(Wrapper)模式[GOF95].装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 二. 装饰模式 ...

  6. IE11下使用fixed定位时鼠标滚动不平滑

    很久不用IE了,近期做兼容性测试发现一个fixed定位的问题,当元素使用fixed定位时,其应该不随页面滚动,在chrome/firefox/edge下都很完美,元素完全不动,但是使用IE11时,如果 ...

  7. C#多线程编程实战1.7前台线程和后台线程

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  8. javascript js函数重名后面的覆盖前面的

    js 函数重名后面的覆盖前面的   var x = 1;    var y = 0;    var z = 0;    function add(n) { return n = n + 1; }    ...

  9. [转]解读Unity中的CG编写Shader系列3——表面剔除与剪裁模式

    在上一个例子中,我们得到了由mesh组件传递的信息经过数学转换至合适的颜色区间以颜色的形式着色到物体上.这篇文章将要在此基础上研究片段的擦除(discarding fragments)和前面剪裁.后面 ...

  10. Bootstrap 基本css样式

    1.标题1级标题<h1> 38px 是默认大小的2.7倍2级标题<h2> 32px 是默认大小的2.25倍3级标题<h3> 24px 是默认大小的1.70倍4级标题 ...