导航:首页 > 编程语言 > java第一步pdf

java第一步pdf

发布时间:2024-05-06 01:29:31

A. java操作pdf表格数据

用Java简单的读取pdf文件中的数据:
第一步:下载PDFBox-0.7.2.jar。提供一个下载地址: http://pdfhome.hope.com.cn/Resource.aspx?CID=63844604-5253-4ae1-b023-258c9e324061&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文件。

B. java中怎么将word转pdf

C. java处理pdf文件

FileInputStream 读取文件流就OK 至于在页面显示

1、客户机上要有PDF2、URL url =new URL("file:///"+ 你的文件路径);response.setContentType(url.openConnection().getContentType());response.setHeader("Content-Disposition", "inline; filename="+ 文件名);或在jsp页面中加入 <% response.setHeader("Content-disposition", "attachment; filename=*.pdf"); %> 以上会提示下载、保存 <% response.setHeader("Content-disposition", "filename=*.pdf"); %> 不要attachment; 就会直接打开,显示pdf了

D. java怎么输出pdf格式的文件

java导出pdf需要用到iText库,iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf
的文档,而且可以将XML、Html文件转化为PDF文件。
iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用
iText类库了。
代码如下:

public class createPdf {
//自己做的一个简单例子,中间有图片之类的
//先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document
Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);
public void getPDFdemo() throws DocumentException, IOException{
//这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见:
//首先
//字体的定义:这里用的是自带的jar里面的字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
// 当然你也可以用你电脑里面带的字体库
//BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//定义字体 注意在最新的包里面 颜色是封装的
Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54));
//生成pdf的第一个步骤:
//保存本地指定路径
saveLocal();
document.open();
ByteArrayOutputStream ba = new ByteArrayOutputStream();
// PdfWriter writer = PdfWriter.getInstance(document, ba);
document.open();
//获取此编译的文件路径
String path = this.getClass().getClassLoader().getResource("").getPath();
//获取根路径
String filePath = path.substring(1, path.length()-15);
//获取图片路径 找到你需要往pdf上生成的图片
//这里根据自己的获取的路径写 只要找到图片位置就可以
String picPath = filePath +"\\WebContent" +"\\images\\";
//往PDF中添加段落
Paragraph pHeader = new Paragraph();
pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1)));
//pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 );
document.add(pHeader);//在文档中加入你写的内容
//获取图片
Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png");
//定义图片在文档中显示的绝对位置
img2.scaleAbsolute(137.0F, 140.0F);
img2.setAbsolutePosition(330.0F, 37.0F);
//将图片添加到文档中
document.add(img2);
//关闭文档
document.close();
/*//设置文档保存的文件名
response.setHeader("Content-
disposition", "attachment;filename=\""+ new String(("CCF会员资格确认
函.pdf").getBytes("GBK"),"ISO-8859-1") + "\"");
//设置类型
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();*/
}
public static void main(String[]args) throws DocumentException, IOException{
createPdf pdf= new createPdf();
pdf.getPDFdemo();
}

//指定一个文件进行保存 这里吧文件保存到D盘的text.pdf
public void saveLocal() throws IOException, DocumentException{
//直接生成PDF 制定生成到D盘test.pdf
File file = new File("D:\\text2.pdf");
file.createNewFile();
PdfWriter.getInstance(document, new FileOutputStream(file));

}
}

E. 怎么用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包下载地址:http://code.google.com/p/flying-saucer/downloads/list
2. 第二步
设计模版,进行排版调整样式,css样式也可以导入@import 等,通过Velocity模版引擎动态替换 页面内容,以下是模版内容:
<?xml version="1.0" encoding="UTF-8" ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PDF模版</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}

F. 如何用纯java代码实现word转pdf

几种方案:
方法来一:用apache pio 读取doc文件源,然后转成html文件用Jsoup格式化html文件,最后用itext将html文件转成pdf。

方法2:使用jdoctopdf来实现,这是一个封装好的包,可以把doc转换成pdf,html,xml等格式,调用很方便
地址:
需要注意中文字体的写入问题。

方法3:使用jodconverter来调用openOffice的服务来转换,openOffice有个各个平台的版本,所以这种方法跟方法1一样都是跨平台的。
jodconverter的下载地址:
首先要安装openOffice,下载地址:
安装完后要启动openOffice的服务,具体启动方法请自行google

方法4:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf)
Office版本是2007,因为SaveAsPDFandXPS是微软为office2007及以上版本开发的插件
SaveAsPDFandXPS下载地址:
jacob 包下载地址:

G. Java如何使用Java创建一个空的PDF文档

package com.yii;import java.io.IOException;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;// 需要下 apache pdfbox包和apache.commons.loggin乌,下载地址:http://pdfbox.apache.org/download.cgi 和 http://commons.apache.org/proper/commons-logging/download_logging.cgi// 在本示例中下载使用的是:pdfbox-2.0.7.jar // 将下载的pdfbox-2.0.7.jar添加到Eclipse项目依懒库中。// 右键点击:"java_apache_pdf_box"->"Bulid Path"->"Add External Artchives...",然后选笃下载的"pdfbox-2.0.7.jar"和"commons-logging-1.2.jar"文件 public class CreatingEmptyPdf {
public static void main(String args[]) throws IOException {

// Creating PDF document object
PDDocument document = new PDDocument();

// Add an empty page to it
document.addPage(new PDPage());

// Saving the document
document.save("F:/worksp/javaexamples/java_apache_pdf_box/BlankPdf.pdf");
System.out.println("PDF created");

// Closing the document
document.close();
}}

H. 濡備綍杩愮敤Java缁勪欢itext鐢熸垚pdf

绗涓姝:涓嬭浇 iText5.5.6鐨勫帇缂╂枃浠,瑙e帇寰楀埌鏍稿績jar鍖卛textpdf-5.5.6.jar

涓嬭浇 extrajars-2.3.zip,瑙e帇鍚,寰楀埌鏀鎸佷腑鏂囩殑itext.asian.jar

绗浜屾:椤圭洰Build Path娣诲姞鍒氬垰鐨勪袱涓猨ar鍖

绗涓夋:寮濮嬪啓浠g爜

importjava.io.FileOutputStream;
importcom.itextpdf.text.Document;
importcom.itextpdf.text.Font;
importcom.itextpdf.text.Paragraph;
importcom.itextpdf.text.pdf.BaseFont;
importcom.itextpdf.text.pdf.PdfWriter;

publicclassPDFDemo{
//main鍑芥暟鎶涘嚭寮傚父,褰撶劧涔熷彲浠trycatch杩涜屽勭悊
publicstaticvoidmain(String[]args)throwsException{
//---------------绗涓闃舵靛噯澶-------------------------
//鍒涘缓涓涓狣ocument瀵硅薄
Documentdocument=newDocument();
//鍒涘缓PDF鍐欏叆鍣锛岄氳繃PDF鍐欏叆鍣ㄥ皢鏂囨。瀵硅薄鍐欏叆纾佺洏(绗涓涓鍙傛暟:鏂囨。瀵硅薄,绗浜屼釜鍙傛暟,杈撳嚭娴)
PdfWriterpdfWriter=PdfWriter.getInstance(document,newFileOutputStream("c:\abcd.pdf"));
//鎵撳紑Document鏂囨。
document.open();
//鍚慏ocument鏂囨。涓娣诲姞鍐呭
//---------------绗浜岄樁娈靛啓鍏-------------------------
//鏂板缓娈佃惤绗涓娈
Paragraphp=newParagraph();
p.add("HelloWorldHappy");

//璁剧疆涓鏂囧瓧浣
BaseFontbaseFont=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",true);
Fontfont=newFont(baseFont);
//鏂板缓娈佃惤绗浜屾,鏀鎸佷腑鏂
Paragraphp2=newParagraph();
p2.setFont(font);
p2.add("闈炲父椋庝簯");
document.add(p);
document.add(p2);
//---------------绗涓夐樁娈垫敹灏-------------------------
//娣诲姞瀹屾瘯,鍏抽棴鏂囨。
document.close();
}
}

鏁堟灉灞曠ず

I. 怎么用java代码生成pdf文档

用java代码生成pdf文档
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
// 创建一个Document对象
Document document = new Document();

try
{
// 生成名为 HelloWorld.pdf 的文档
PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
// 添加PDF文档的一些信息
document.addTitle("Hello World example");
document.addAuthor("Bruno Lowagie");
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello World, step 3, metadata");
document.addCreator("My program using iText");
// 打开文档,将要写入内容
document.open();
// 插入一个段落
document.add(new Paragraph("Hello World!"));

}
catch (DocumentException de)
{
System.err.println(de.getMessage());
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
// 关闭打开的文档
document.close();
}
}
编译运行以后,我们可以在运行的目录发现生成的HelloWorld.pdf,打开可以看到我们写的文字:

J. java导出PDF文档

java导出pdf需要用到iText库,iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf
的文档,而且可以将XML、Html文件转化为PDF文件。
iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用
iText类库了。
代码如下:

public class createPdf {
//自己做的一个简单例子,中间有图片之类的
//先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document
Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);
public void getPDFdemo() throws DocumentException, IOException{
//这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见:
//首先
//字体的定义:这里用的是自带的jar里面的字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
// 当然你也可以用你电脑里面带的字体库
//BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//定义字体 注意在最新的包里面 颜色是封装的
Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54));
//生成pdf的第一个步骤:
//保存本地指定路径
saveLocal();
document.open();
ByteArrayOutputStream ba = new ByteArrayOutputStream();
// PdfWriter writer = PdfWriter.getInstance(document, ba);
document.open();
//获取此编译的文件路径
String path = this.getClass().getClassLoader().getResource("").getPath();
//获取根路径
String filePath = path.substring(1, path.length()-15);
//获取图片路径 找到你需要往pdf上生成的图片
//这里根据自己的获取的路径写 只要找到图片位置就可以
String picPath = filePath +"\\WebContent" +"\\images\\";
//往PDF中添加段落
Paragraph pHeader = new Paragraph();
pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1)));
//pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 );
document.add(pHeader);//在文档中加入你写的内容
//获取图片
Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png");
//定义图片在文档中显示的绝对位置
img2.scaleAbsolute(137.0F, 140.0F);
img2.setAbsolutePosition(330.0F, 37.0F);
//将图片添加到文档中
document.add(img2);
//关闭文档
document.close();
/*//设置文档保存的文件名
response.setHeader("Content-
disposition", "attachment;filename=\""+ new String(("CCF会员资格确认
函.pdf").getBytes("GBK"),"ISO-8859-1") + "\"");
//设置类型
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();*/
}
public static void main(String[]args) throws DocumentException, IOException{
createPdf pdf= new createPdf();
pdf.getPDFdemo();
}

//指定一个文件进行保存 这里吧文件保存到D盘的text.pdf
public void saveLocal() throws IOException, DocumentException{
//直接生成PDF 制定生成到D盘test.pdf
File file = new File("D:\\text2.pdf");
file.createNewFile();
PdfWriter.getInstance(document, new FileOutputStream(file));

}
}

阅读全文

与java第一步pdf相关的资料

热点内容
苹果在qq上下载的文件在哪里可以找到 浏览:576
win10平板蓝牙鼠标 浏览:791
小精灵软件怎么导入数据 浏览:252
linux卸载sendmail 浏览:62
免费大数据分析 浏览:448
word2007怎么调整页码 浏览:629
做系统镜像文件 浏览:518
qq炫舞生名道演唱会 浏览:927
手机图片怎么转文件夹 浏览:838
附近数据线厂在哪里 浏览:294
类似秋霞影院的网站有哪些 浏览:489
thinkphp读取配置文件 浏览:911
个税app在哪里填写赡养父母 浏览:341
打开cad时总弹出一个文件 浏览:87
删除一个文件夹找不到了 浏览:654
电脑桌面文件管理哪个软件好 浏览:188
苹果数据线头歪了 浏览:135
ghostwin764位系统镜像文件 浏览:443
传感器视频教程下载 浏览:95
flash源文件贺卡下载 浏览:434

友情链接