您的位置 首页 java

Java对PDF数据的读写详细总结(二)

一、创建表格

PDF支持对表格的处理,通过com.lowagie.text.Table和com.lowagie.text.pdf.PdfPTable类来实现。其中Table用来处理简单的表格,PdfPTable类通常处理一些复杂的表格

下面通过一个例子说明,相见”3、PDF处理表格源代码”:

package com.test;

import java. io .FileOutputStream;

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.PdfPTable;

import com.itextpdf.text.pdf.PdfWriter;

public class TablePdf {

public static void main(String[] args) throws Exception {

String[][] context = { { “第一节”, “语文”, “数学”, “外语”, “语文”, “数学” },

{ “第二节”, “数学”, “语文”, “语文”, “数学”, “外语” },

{ “第三节”, “自然”, “美术”, “数学”, “自然”, “体育” },

{ “第四节”, “音乐”, “英语”, “体育”, “英语”, “劳动” } };

// 创建一个对中文字符集支持的基础字体

BaseFont bfChinese = BaseFont.createFont(“STSong-Light”,

“UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);

// 使用基础字体对象创建新字体对像,粗体12号红色字

Font font = new Font(bfChinese, 12, Font.BOLD);

font.setColor(255, 0, 0); //设置文字颜色

Document document = new Document(PageSize.A4); // 创建 document对象

PdfWriter. getInstance (document, new FileOutputStream(“tablePDF.pdf”));

document. open (); // 打开文档

String title = “XX小学一年二班课程表”; // 文档内容

Paragraph paragraph = new Paragraph(title, font); // 创建段落,并设置字体

paragraph.setAlignment(Paragraph.ALIGN_CENTER); // 设置段落居中

document.add(paragraph); // 将段落添加到文档中

PdfPTable table = new PdfPTable(6); // 建立一个6列的空白表格对象

table.setSpacingBefore(30f); // 设置表格上面空白宽度

String[] tableTitle = { “”, “星期一”, “星期二”,

“星期三”, “星期四”, “星期五” };

for ( int i = 0; i < tableTitle.length; i++) {

paragraph = new Paragraph(tableTitle[i], new Font(bfChinese, 10,

Font.BOLD));

PdfPCell cell = new PdfPCell(paragraph); // 建立一个单元格

// 设置内容水平居中显示

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

// 设置垂直居中

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

}

for ( int i = 0; i < context.length; i++) {

for ( int j = 0; j < context[i].length; j++) {

PdfPCell cell = new PdfPCell( new Paragraph(context[i][j],

new Font(bfChinese, 10))); // 建立一个单元格

// 设置内容水平居中显示

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中

table.addCell(cell);

}

}

document.add(table);

document. close ();

}

}

二、创建图片

在iText组件中支持jpeg、png、gif格式的图片,通过使用com.lowagie.text.Image类的getInstance()方法来构造一个图片对象然后将其添加到PDF文档中。同时还提供一些对图片进行处理的方法如:设置图片的位置、大小等,下面通过例子演示见”4、PDF的图片处理源代码”:

package com.test;

import java.io.FileOutputStream;

import com.itextpdf.text.Document;

import com.itextpdf.text.Image;

import com.itextpdf.text.pdf.PdfWriter;

public class ImgPdf {

public static void main(String[] args) throws Exception {

//创建document对象

Document document = new Document();

//创建Image图片对象

Image img = Image.getInstance(“test.bmp”);

//缩放到百分之50大小

img.scalePercent(50);

//旋转180度

//img.setRotationDegrees(180);

//剧中显示

img.setAlignment(Image.ALIGN_CENTER);

PdfWriter.getInstance(document, new FileOutputStream(“imgPdf.pdf”));

document.open(); //打开document文档对象

document.add(img); //将图片添加到文档中

document.close(); //关闭文档

}

}

三、使用PDFBox组件解析PDF文档

PDFBox组件可以从进行下载,使用PDFBox组件解析一个PDF的文档非常简单,下面举个例子说明见”5、读取PDF文档内容源代码”:

package com.test;

import java.io.File;

import java.io.FileInputStream;

import org.apache.pdfbox.pdfparser.PDFParser;

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.util.PDFTextStripper;

public class ReadPdf {

public static void main(String[] args) {

//创建对象

File file = new File(“firstPdf.pdf”);

//声明文件输入流

FileInputStream in = null ;

try {

//获取文件输入流

in = new FileInputStream(file);

//创建 一个PDF解析器

PDFParser parser = new PDFParser(in);

//对PDF文档进行解析

parser.parse();

//获取解析后的文档对象

PDDocument document = parser.getPDDocument();

//创建PDF文档剥离器

PDFTextStripper stripper = new PDFTextStripper();

//获取文档内容

String result = stripper.getText(document);

System.out.println(“文件内容如下:”);

//在控制台中输出文档内容

System.out.println(result);

in.close();

} catch (Exception e){

e.printStackTrace();

}

}

}

Java对PDF数据的读写详细总结(二)

《大数据和人工智能交流》的宗旨

1、将大数据和人工智能的专业数学:概率数理统计、线性代数、决策论、优化论、博弈论等数学模型变得通俗易懂。

2、将大数据和人工智能的专业涉及到的数据结构和算法:分类、聚类 、回归算法、概率等算法变得通俗易懂。

3、最新的高科技动态:数据采集方面的智能传感器技术;医疗大数据智能决策分析;物联网智慧城市等等。

根据初学者需要会有C语言、Java语言、Python语言、Scala函数式等目前主流计算机语言。

根据读者的需要有和人工智能相关的计算机科学与技术、电子技术、芯片技术等基础学科通俗易懂的文章。

文章来源:智云一二三科技

文章标题:Java对PDF数据的读写详细总结(二)

文章地址:https://www.zhihuclub.com/184482.shtml

关于作者: 智云科技

热门文章

网站地图