「java第一步pdf」java第一步,不是内部或外部命令 也不是可运行的程序
本篇文章给大家谈谈java第一步pdf,以及java第一步,不是内部或外部命令 也不是可运行的程序对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、如何运用Java组件itext生成pdf
- 2、怎么用java代码生成pdf文档
- 3、java中怎么利用poi和itext生成pdf文档
- 4、Java操作pdf表格数据
- 5、java itextpdf找到重复的关键字
- 6、怎么用java动态生成pdf文档
如何运用Java组件itext生成pdf
实现流程:
一、iText介绍
iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
iText的安装非常方便,在 - download
网站上下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。
二、建立第一个PDF文档
用iText生成PDF文档需要5个步骤:
①建立com.lowagie.text.Document对象的实例。
Document document = new Document();
②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));
③打开文档。
document.open();
④向文档中添加内容。
document.add(new Paragraph("Hello World"));
⑤关闭文档。
document.close();
通过上面的5个步骤,就能产生一个Helloworld.PDF的文件,文件内容为"Hello World"。
怎么用java代码生成pdf文档
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfTest
{
public static void main(String[] args) throws Exception
{
Document pdfDoc = new Document();
// 将要生成的 pdf 文件的路径输出流
FileOutputStream pdfFile =
new FileOutputStream(new File("F:/study/test/firstPdf.pdf"));
// pdf 文件中的一个文字段落
Paragraph paragraph = new Paragraph("My first PDF file with an image ...");
Image image = Image.getInstance("F:/study/test/洛克 李.jpg");
// 用 Document 对象、File 对象获得 PdfWriter 输出流对象
PdfWriter.getInstance(pdfDoc, pdfFile);
pdfDoc.open(); // 打开 Document 文档
// 添加一个文字段落、一张图片
pdfDoc.add(paragraph);
pdfDoc.add(image);
pdfDoc.close();
}
}
java中怎么利用poi和itext生成pdf文档
生成PDF文档代码如下:
package poi.itext;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.lowagie.text.pdf.BaseFont;
/**
* 创建Pdf文档
* @author Administrator
*
*/
public class HelloPdf
{
public static void main(String[] args)throws Exception
{
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
// 第一步,创建document对象
Rectangle rectPageSize = new Rectangle(PageSize.A4);
//下面代码设置页面横置
//rectPageSize = rectPageSize.rotate();
//创建document对象并指定边距
Document doc = new Document(rectPageSize,50,50,50,50);
Document document = new Document();
try
{
// 第二步,将Document实例和文件输出流用PdfWriter类绑定在一起
//从而完成向Document写,即写入PDF文档
PdfWriter.getInstance(document,new FileOutputStream("src/poi/itext/HelloWorld.pdf"));
//第3步,打开文档
document.open();
//第3步,向文档添加文字. 文档由段组成
document.add(new Paragraph("Hello World"));
Paragraph par = new Paragraph("世界你好",FontChinese);
document.add(par);
PdfPTable table = new PdfPTable(3);
for(int i=0;i12;i++)
{
if (i == 0)
{
PdfPCell cell = new PdfPCell();
cell.setColspan(3);
cell.setBackgroundColor(new Color(180,180,180));
cell.addElement(new Paragraph("表格头" , FontChinese));
table.addCell(cell);
}
else
{
PdfPCell cell = new PdfPCell();
cell.addElement(new Paragraph("表格内容" , FontChinese));
table.addCell(cell);
}
}
document.add(table);
}
catch (DocumentException de)
{
System.err.println(de.getMessage());
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
//关闭document
document.close();
System.out.println("生成HelloPdf成功!");
}
}
希望对你有帮助。
Java操作pdf表格数据
用Java简单的读取pdf文件中的数据:
第一步:下载PDFBox-0.7.2.jar。提供一个下载地址: ;RID=20cd8f94-1cee-40b6-a3df-0ef024f8e0d2解压后,把lib文件下的PDFBox-0.7.2.jar,PDFBox-0.7.2-log4j.jar放到你classpath路径下。(我把源码以及jar包都放到下面的附件里,方面你的使用。)
第二步:写个简单的读取pdf文件的程序。(PdfReader.java)
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class PdfReader {
public void readFdf(String file) throws Exception {
// 是否排序
boolean sort = false;
// pdf文件名
String pdfFile = file;
// 输入文本文件名称
String textFile = null;
// 编码方式
String encoding = "UTF-8";
// 开始提取页数
int startPage = 1;
// 结束提取页数
int endPage = Integer.MAX_VALUE;
// 文件输入流,生成文本文件
Writer output = null;
// 内存中存储的PDF Document
PDDocument document = null;
try {
try {
// 首先当作一个URL来装载文件,如果得到异常再从本地文件系统//去装载文件
URL url = new URL(pdfFile);
//注意参数已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
// 获取PDF的文件名
String fileName = url.getFile();
// 以原来PDF的名称来命名新产生的txt文件
if (fileName.length() 4) {
File outputFile = new File(fileName.substring(0, fileName
.length() - 4)
+ ".txt");
textFile = outputFile.getName();
}
} catch (MalformedURLException e) {
// 如果作为URL装载得到异常则从文件系统装载
//注意参数已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
if (pdfFile.length() 4) {
textFile = pdfFile.substring(0, pdfFile.length() - 4)
+ ".txt";
}
}
// 文件输入流,写入文件倒textFile
output = new OutputStreamWriter(new FileOutputStream(textFile),
encoding);
// PDFTextStripper来提取文本
PDFTextStripper stripper = null;
stripper = new PDFTextStripper();
// 设置是否排序
stripper.setSortByPosition(sort);
// 设置起始页
stripper.setStartPage(startPage);
// 设置结束页
stripper.setEndPage(endPage);
// 调用PDFTextStripper的writeText提取并输出文本
stripper.writeText(document, output);
} finally {
if (output != null) {
// 关闭输出流
output.close();
}
if (document != null) {
// 关闭PDF Document
document.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PdfReader pdfReader = new PdfReader();
try {
// 取得E盘下的SpringGuide.pdf的内容
pdfReader.readFdf("E:\\SpringGuide.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}
这样就简单的完成了从pdf中读取数据了。在你的pdf文件所在的目录下生成一个同名的txt文件。
java itextpdf找到重复的关键字
在要编辑的文档的PDF文件页面中弹出的查找页面中输入关键字。
1、第一步:通过在打开的PDF编辑器上使用鼠标拖动方法来打开要编辑的文档PDF文件。2、第二步:这样就将要编辑的文档PDF文件打开到PDF编辑器中了。3、第三步:接着点击打开编辑的文档的PDF编辑器菜单中的视图菜单下选择查找。4、第四步:在要编辑的文档的PDF文件页面中弹出的查找页面中输入关键字。5、这样就可以查找关键字了
怎么用java动态生成pdf文档
Flying-Saucer + iText + Velocity
1. 第一步
将jar包放到你的工程里,需要的jar如下:
bcprov-jdk15-140.jar
core-renderer.jar
iText-2.0.8.jar
iTextAsian.jar
velocity-1.4.jar
Jar包下载地址:
2. 第二步
设计模版,进行排版调整样式,css样式也可以导入@import 等,通过Velocity模版引擎动态替换 页面内容,以下是模版内容:
?xml version="1.0" encoding="UTF-8" ?
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /
titlePDF模版/title
style type="text/css"
!--
body {
font: 100% Verdana, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
text-align: center;
color: #000000;
}
.oneColLiqCtrHdr #container {
width: 100%;
margin: 0 auto;
text-align: left;
}
div.header-left {display: none}
div.header-right {display: none}
div.footer-left {display: none}
div.footer-right {display: none}
java第一步pdf的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java第一步,不是内部或外部命令 也不是可运行的程序、java第一步pdf的信息别忘了在本站进行查找喔。