(一)创建一个简单的PDF文件      pdf_file

package com.pdf.file;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;

public class Test {

public static void main(String[] args) throws FileNotFoundException, DocumentException {

// 1.新建document对象

Document document = new Document();

// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。

// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:/image/test.pdf"));

// 3.打开文档

document.open();

// 4.添加一个内容段落

document.add(new Paragraph("Hello Everyone!we are family"));

// 5.关闭文档

document.close();

}

}

(二)给PDF文件设置文件属性      pdf_content

package com.pdf.content;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;

public class Test {

public static void main(String[] args) throws FileNotFoundException, DocumentException {

// 创建文件

Document document = new Document();

// 建立一个书写器

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:/image/content.pdf"));

// 打开文件

document.open();

// 添加内容

document.add(new Paragraph("Some content here"));

// 设置属性

// 标题

document.addTitle("this is a title");

// 作者

document.addAuthor("H__D");

// 主题

document.addSubject("this is subject");

// 关键字

document.addKeywords("Keywords");

// 创建时间

document.addCreationDate();

// 应用程序

document.addCreator("hd.com");

// 关闭文档

document.close();

// 关闭书写器

writer.close();

}

}

(三)PDF中添加图片              pdf_image

package com.pdf.image;

import java.io.FileOutputStream;

import java.io.IOException;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Image;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;

public class Test {

public static void main(String[] args) throws DocumentException, IOException {

// 创建文件

Document document = new Document();

// 建立一个书写器

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:/image/chanpin/22.pdf"));

// 打开文件

document.open();

// 添加内容

document.add(new Paragraph("my image"));

// 图片

Image image= Image.getInstance("E:/image/chanpin/22.jpg");

// 设置图片位置的x轴和y周

image.setAbsolutePosition(100f, 550f);

// 设置图片的宽度和高度

image.scaleAbsolute(200, 200);

// 将图片添加到pdf文件中

document.add(image);

// 关闭文档

document.close();

// 关闭书写器

writer.close();

}

}

(四)PDF中创建表格              pdf_table

package com.pdf.table;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.util.List;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Element;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfPCell;

import com.lowagie.text.pdf.PdfPRow;

import com.lowagie.text.pdf.PdfPTable;

import com.lowagie.text.pdf.PdfWriter;

public class Test {

public static void main(String[] args) throws DocumentException, FileNotFoundException {

// 创建文件

Document document = new Document();

// 建立一个书写器

PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("E:/image/table.pdf"));

// 打开文件

document.open();

// 添加内容

document.add(new Paragraph("-----------------table----------------"));

// 3列的表

PdfPTable table = new PdfPTable(3);

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

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

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

List listRow = table.getRows();

// 设置列宽

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

table.setWidths(columnWidths);

// 行1

PdfPCell cells1[] = new PdfPCell[3];

PdfPRow row1 = new PdfPRow(cells1);

// 单元格

// 单元格内容

cells1[0] = new PdfPCell(new Paragraph("name"));

// 边框验证

// cells1[0].setBorderColor(BaseColor.BLUE);

// 左填充20

cells1[0].setPaddingLeft(20);

// 水平居中

cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);

// 垂直居中

cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);

// 单元格内容

cells1[1] = new PdfPCell(new Paragraph("pwd"));

// 左填充20

cells1[1].setPaddingLeft(20);

// 水平居中

cells1[1].setHorizontalAlignment(Element.ALIGN_CENTER);

// 垂直居中

cells1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);

// 单元格内容

cells1[2] = new PdfPCell(new Paragraph("birthday"));

// 左填充20

cells1[2].setPaddingLeft(20);

// 水平居中

cells1[2].setHorizontalAlignment(Element.ALIGN_CENTER);

// 垂直居中

cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);

// 行2

PdfPCell cells2[] = new PdfPCell[3];

PdfPRow row2 = new PdfPRow(cells2);

// 单元格内容

cells2[0] = new PdfPCell(new Paragraph("zhangsan"));

// 左填充20

cells2[0].setPaddingLeft(20);

// 水平居中

cells2[0].setHorizontalAlignment(Element.ALIGN_CENTER);

// 垂直居中

cells2[0].setVerticalAlignment(Element.ALIGN_MIDDLE);

// 单元格内容

cells2[1] = new PdfPCell(new Paragraph("666"));

// 左填充20

cells2[1].setPaddingLeft(20);

// 水平居中

cells2[1].setHorizontalAlignment(Element.ALIGN_CENTER);

// 垂直居中

cells2[1].setVerticalAlignment(Element.ALIGN_MIDDLE);

// 单元格内容

cells2[2] = new PdfPCell(new Paragraph("1993-02-05"));

// 左填充20

cells2[2].setPaddingLeft(20);

// 水平居中

cells2[2].setHorizontalAlignment(Element.ALIGN_CENTER);

// 垂直居中

cells2[2].setVerticalAlignment(Element.ALIGN_MIDDLE);

// 行3

PdfPCell cells3[] = new PdfPCell[3];

PdfPRow row3 = new PdfPRow(cells3);

// 单元格内容

cells3[0] = new PdfPCell(new Paragraph("lisi"));

// 左填充20

cells3[0].setPaddingLeft(20);

// 水平居中

cells3[0].setHorizontalAlignment(Element.ALIGN_CENTER);

// 垂直居中

cells3[0].setVerticalAlignment(Element.ALIGN_MIDDLE);

// 单元格内容

cells3[1] = new PdfPCell(new Paragraph("222"));

// 左填充20

cells3[1].setPaddingLeft(20);

// 水平居中

cells3[1].setHorizontalAlignment(Element.ALIGN_CENTER);

// 垂直居中

cells3[1].setVerticalAlignment(Element.ALIGN_MIDDLE);

// 单元格内容

cells3[2] = new PdfPCell(new Paragraph("1993-02-05"));

// 左填充20

cells3[2].setPaddingLeft(20);

// 水平居中

cells3[2].setHorizontalAlignment(Element.ALIGN_CENTER);

// 垂直居中

cells3[2].setVerticalAlignment(Element.ALIGN_MIDDLE);

// 把第一行添加到集合

listRow.add(row1);

listRow.add(row2);

listRow.add(row3);

// 把表格添加到文件中

document.add(table);

// 关闭文档

document.close();

// 关闭书写器

writer.close();

}

}

(五)读取/修改已有PDF文件       pdf_reader

package com.pdf.reader;

import java.io.FileOutputStream;

import java.io.IOException;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Image;

import com.lowagie.text.pdf.PdfContentByte;

import com.lowagie.text.pdf.PdfReader;

import com.lowagie.text.pdf.PdfStamper;

public class Test {

public static void main(String[] args) throws DocumentException, IOException {

// 读取pdf文件

PdfReader pdfReader = new PdfReader("E:/image/test.pdf");

// 修改器

PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("E:/image/reader.pdf"));

Image image = Image.getInstance("E:/image/watermark.png");

image.scaleAbsolute(50, 50);

image.setAbsolutePosition(0, 700);

for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {

PdfContentByte content = pdfStamper.getUnderContent(i);

content.addImage(image);

}

pdfStamper.close();

}

}

(六)PDF水印              pdf_watermark

package com.pdf.mark;

import java.io.*;

import com.lowagie.text.Document;

import com.lowagie.text.Font;

import com.lowagie.text.Image;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Rectangle;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.PdfContentByte;

import com.lowagie.text.pdf.PdfReader;

import com.lowagie.text.pdf.PdfStamper;

import com.lowagie.text.pdf.PdfWriter;

/**

* PDF生成

*/

public class PDFBuild {

public static void buidPDF(String pdfFile, String imageFile, String waterMarkName, int permission) {

try {

// 创建临时文件

File file = File.createTempFile("tempFile", ".pdf");

// 生成PDF

if (createPDFFile(file)) {

// 添加水印

waterMark(file.getPath(), imageFile, pdfFile, waterMarkName, permission);

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 创建PDF文件

*

*/

public static boolean createPDFFile(File file) {

Rectangle rect = new Rectangle(PageSize.A4);

Document doc = new Document(rect, 50.0F, 50.0F, 50.0F, 50.0F);

try {

PdfWriter.getInstance(doc, new FileOutputStream(file));

doc.open();

// 使用系统字体

BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);

Font font = new Font(bf, 20.0F, 0);

// 设置样式

font.setStyle(37);

// 设置字体

font.setFamily("宋体");

Paragraph p = new Paragraph("我的水印图\r\n", font);

p.setAlignment(1);

doc.add(p);

doc.close();

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

public static void waterMark(String inputFile, String imageFile, String outputFile, String waterMarkName,

int permission) {

try {

PdfReader reader = new PdfReader(inputFile);

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));

// 使用系统字体

BaseFont base = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);

int total = reader.getNumberOfPages() + 1;

Image image = Image.getInstance(imageFile);

// 图片位置

image.setAbsolutePosition(100, 280);

PdfContentByte under;

int j = waterMarkName.length();

char c = 0;

int rise = 0;

for (int i = 1; i < total; i++) {

rise = 400;

under = stamper.getUnderContent(i);

under.beginText();

under.setFontAndSize(base, 30);

if (j >= 15) {

under.setTextMatrix(200, 120);

for (int k = 0; k < j; k++) {

under.setTextRise(rise);

c = waterMarkName.charAt(k);

under.showText(c + "");

}

} else {

under.setTextMatrix(240, 100);

for (int k = 0; k < j; k++) {

under.setTextRise(rise);

c = waterMarkName.charAt(k);

under.showText(c + "");

rise -= 18;

}

}

// 添加水印文字

under.endText();

// 添加水印图片

under.addImage(image);

}

stamper.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

// 水印图片路径

String imageFilePath = "E:/image/watermark.png";

// 文件生成路径

String pdfFilePath = "E:/image/watermark.pdf";

buidPDF(pdfFilePath, imageFilePath, "正版授权", 20);

}

}

java转pdf_Java转PDF(基础)相关推荐

  1. java 绘制pdf_Java 在PDF文档中绘制图形

    本篇文档将介绍通过Java编程在PDF文档中绘制图形的方法.包括绘制矩形.椭圆形.不规则多边形.线条.弧线.曲线.扇形等等.针对方法中提供的思路,也可以自行变换图形设计思路,如菱形.梯形或者组合图形等 ...

  2. java读取pdf_Java 读取PDF中的文本和图片的方法

    本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取. 使用工具:Free Spire.PDF for Java ...

  3. java 修改pdf_Java 操作PDF书签详解 - 添加、修改、读取和删除

    目录前言 Free Spire.PDF for Java库概述和安装 给PDF文档添加书签 修改现有书签 设置PDF文档打开时展开或折叠书签 读取书签标题 从PDF文档中删除书签 前言 书签在一些PD ...

  4. java生成pdf_Java实现PDF文件生成并且打印pdf文件 demo

    ## Java实现PDF生成并且打印pdf文件(附demo) #### 目录: 0. 效果预览 1. 准备环境 2. Java如何调用打印机进行打印 3. Java如何生成pdf打印文件 4. 实现p ...

  5. java 操作pdf_java操作PDF(PDFBOX和Itext框架)

    java有很多可以操作pdf的框架,pdfbox和itext就是其中的两种 pdfbox有如下作用 提取文本,包括Unicode字符.和Jakarta Lucene等文本搜索引擎的整合过程十分简单.加 ...

  6. java response pdf_JAVA导出pdf实例

    一.直接导出成PDF Java代码 1. import java.io.FileNotFoundException; 2. import java.io.FileOutputStream; 3. 4. ...

  7. java读取pdf_java读取pdf总结

    第三方软件 1.pdfbox PDFBox 0.7.3.PDFBox是一个开源的对pdf文件进行操作的库. PDFBox-0.7.3.jar加入classpath.同时FontBox1.0.jar加入 ...

  8. java 操作pdf_java操作pdf

    新项目里有个pdf的生成文件,折腾了两天,终于可以 用了,现在 记下来,方便后来者. 有些代码重复的地方,大家凑合着看了,项目太急,也不想优化了. 里面大部分地方都做了注释,一看就会明白了. 废话不多 ...

  9. Java awt pdf_java生成pdf

    简述 生成pdf思路主要是分两种情况,情况一:如果模板里面不需要数组参数,那么可以根据html模板直接生成pdf:情况二:如果模板里面需要数组参数,需要先利用freemarker生成html模板,再根 ...

  10. java生成pdf_JAVA 生成PDF 并导出

    /** *首先啥也不干,先写一个转换中文的方法,话说谁要整一个全英文数字的就不 * 要写这个方法了.... *str :要转换的内容     c:字体大小 */ private static Para ...

最新文章

  1. 2021年春季学期-信号与系统-第十一次作业参考答案-第六小题
  2. 关于助动词和过去分词的一点见解
  3. Challenge: Machine Learning Basics
  4. dos中的for命令简单使用
  5. 线性代数行列式计算之升阶法
  6. [spfa][差分约束] 洛谷 P3084 照片Photo
  7. postman--安装及Interceptor插件
  8. 原创 | 万万没想到,JVM内存结构的面试题可以问的这么难?
  9. MySQL免安装版安装
  10. icon 做成html形式,一段生成iconfont预览html的代码
  11. Shell学习笔记一
  12. 《Python参考手册(第4版•修订版)》——1.8 集合
  13. 班尼路信息化系统基础选型的简单分析
  14. JDK8编译,JDK8运行错误
  15. 【Java-GC】集合Collection的clear()和GC垃圾回收测验
  16. 微信小程序token使用(首页获取不到token)
  17. c# 枚举的定义,枚举的用法,获取枚举值
  18. Unity-安卓端透明背景设置
  19. linux程序设计项目报告,Linux程序设计实验报告大作业
  20. HMC7044调试说明

热门文章

  1. 多媒体计算机教室的使用与管理,多媒体教室计算机管理论文
  2. U盘的基础知识介绍量产
  3. 方正台式计算机保护卡密码忘记了,方正电脑E系列忘记还原卡密码处理方法
  4. 安徽掀起新一轮大规模清房行动 官员急抛房产
  5. SpringMvc 最新jar包下载
  6. 用记事本写表白html,抖音电脑弹窗表白代码怎么弄_记事本vbs告白代码写法介绍_抖音表白套路方法分享...
  7. win10下安装mysql5.7_【详细】Win10 安装MySQL 5.7 详细教程
  8. css3径向渐变详解-遁地龙卷风
  9. 大数据Hadoop生态系统介绍
  10. PAT L1-019. 谁先倒