导航:首页 > 文档加密 > java二级PDF

java二级PDF

发布时间:2025-02-10 17:16:56

java解析pdf文件,求大神提供代码,请注意是java语言的

给你提供一个参考例子,你可以在这个例子上试试,修改修改。也是解析PDF的。

importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.OutputStreamWriter;
importjava.io.Writer;
importjava.net.MalformedURLException;
importjava.net.URL;
importorg.apache.pdfbox.pdmodel.PDDocument;
importorg.apache.pdfbox.util.PDFTextStripper;
publicclassPdfReader{
publicvoidreadFdf(Stringfile)throwsException{
//是否排序
booleansort=false;
//pdf文件名
StringpdfFile=file;
//输入文本文件名称
StringtextFile=null;
//编码方式
Stringencoding="UTF-8";
//开始提取页数
intstartPage=1;
//结束提取页数
intendPage=Integer.MAX_VALUE;
//文件输入流,生成文本文件
Writeroutput=null;
//内存中存储的PDFDocument
PDDocumentdocument=null;
try{
try{
//首先当作一个URL来装载文件,如果得到异常再从本地文件系统//去装载文件
URLurl=newURL(pdfFile);
//注意参数已不是以前版本中的URL.而是File。
document=PDDocument.load(pdfFile);
//获取PDF的文件名
StringfileName=url.getFile();
//以原来PDF的名称来命名新产生的txt文件
if(fileName.length()>4){
FileoutputFile=newFile(fileName.substring(0,fileName
.length()-4)
+".txt");
textFile=outputFile.getName();
}
}catch(MalformedURLExceptione){
//如果作为URL装载得到异常则从文件系统装载
//注意参数已不是以前版本中的URL.而是File。
document=PDDocument.load(pdfFile);
if(pdfFile.length()>4){
textFile=pdfFile.substring(0,pdfFile.length()-4)
+".txt";
}
}
//文件输入流,写入文件倒textFile
output=newOutputStreamWriter(newFileOutputStream(textFile),
encoding);
//PDFTextStripper来提取文本
PDFTextStripperstripper=null;
stripper=newPDFTextStripper();
//设置是否排序
stripper.setSortByPosition(sort);
//设置起始页
stripper.setStartPage(startPage);
//设置结束页
stripper.setEndPage(endPage);
//调用PDFTextStripper的writeText提取并输出文本
stripper.writeText(document,output);
}finally{
if(output!=null){
//关闭输出流
output.close();
}
if(document!=null){
//关闭PDFDocument
document.close();
}
}
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
PdfReaderpdfReader=newPdfReader();
try{
//取得E盘下的SpringGuide.pdf的内容
pdfReader.readFdf("d:\b.pdf");
}catch(Exceptione){
e.printStackTrace();
}
}
}

❷ JAVA操作PDF的几个问题,是高手的进。

1、我不清楚,没做过相关东西
2、通过js可以实现禁止右键行为
3、页面无法打印好像实现不了,因为浏览器有打印功能会将页面打印出来

❸ 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();
}}

❹ java程序下载pdf文件

主要是 URL 和 HttpURLConnection 类的运用,看代码:


importjava.io.DataInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.net.HttpURLConnection;
importjava.net.URL;

publicclassHttpDownloader{

_FILE_URL="http://211.103.156.163/u/cms/www/201511/25051940i6ou.pdf";
privatestaticfinalStringLOCAL_FILE_PATH="D:/some.pdf";//改成你保存文件的路径

publicstaticvoidmain(String[]args){
newHttpDownloader(REMOTE_FILE_URL,LOCAL_FILE_PATH).download();
}

privateStringremoteFileUrl;
privateStringlocalFilePath;

publicHttpDownloader(StringremoteFileUrl,StringlocalFilePath){
this.remoteFileUrl=remoteFileUrl;
this.localFilePath=localFilePath;
}

publicvoiddownload(){
try{
URLurl=newURL(remoteFileUrl);

=(HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(5*1000);//5000毫秒内没有连接上则放弃连接
httpURLConnection.connect();//连接
System.out.println("连接URL成功~");

intfileLenght=httpURLConnection.getContentLength();
System.out.println("文件大小:"+(fileLenght/1024.0)+"KB");

System.out.println("开始下载...");
try(DataInputStreamdis=newDataInputStream(httpURLConnection.getInputStream());
FileOutputStreamfos=newFileOutputStream(localFilePath)){
byte[]buf=newbyte[10240];//根据实际情况可以增大buf大小
for(intreadSize;(readSize=dis.read(buf))>0;){
fos.write(buf,0,readSize);
}
System.out.println("下载完毕~");
}catch(IOExceptionex){
System.out.println("下载时出错");
}

httpURLConnection.disconnect();
}catch(IOExceptionex){
System.out.println("URL不存在或者连接超时");
}
}
}
阅读全文

与java二级PDF相关的资料

热点内容
腾讯云连接自己的服务器地址 浏览:216
硕士英语综合教程pdf 浏览:46
分段加密的安全性 浏览:507
咪咕直播为什么没有适配安卓系统 浏览:172
php模版大全 浏览:102
没车能解压吗 浏览:634
php开发oa系统源码 浏览:759
怎么安装苹果ios的app 浏览:581
app拉新如何机刷 浏览:480
zendeclipseforphp 浏览:480
同时有几个微信如何加密微信 浏览:86
大众20t压缩比 浏览:566
程序员要记住的500个单词 浏览:830
wq快捷方式在哪个文件夹 浏览:965
云南到河北源码 浏览:92
安卓手机怎么玩造梦3 浏览:60
多玩我的世界盒子怎么创造服务器地址 浏览:986
手机如何下载米家app 浏览:96
未知来源app在哪里 浏览:206
命令与征服3合集 浏览:752