「java读pdf」java读pdf图片
本篇文章给大家谈谈java读pdf,以及java读pdf图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java 如何读取PDF文件内容
- 2、Java 如何实现打开PDF文件,并把文件显示出来;就是实现文件打开的功能。急,急....
- 3、用Java 读取 PDF 遇到中文标签该怎么处理
- 4、如何用java读取pdf文档的部分内容
- 5、怎么用java读取pdf文件内容
java 如何读取PDF文件内容
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();
}
}
}
Java 如何实现打开PDF文件,并把文件显示出来;就是实现文件打开的功能。急,急....
java打开PDF需要借助其他的jar包的,如果我没记错的话,然后查看相应的API接口。应该是以流的形式进行读取,之前写过一点是往里面写数据的,生成html文然后写入PDF文件 工具有:
ItextPdf、FlyingAndItext、pd4ml 这些是将html文转换为PDF文件的,读取的话直接用bufferread读取试试(我没写过!)
用Java 读取 PDF 遇到中文标签该怎么处理
直接使用系统字体读取或创建带中文的pdf,需要注意jar的版本。
dependency
groupIdcom.itextpdf/groupId
artifactIditextpdf/artifactId
version5.5.8/version
/dependency
dependency
groupIdcom.itextpdf/groupId
artifactIditext-asian/artifactId
version5.2.0/version
/dependency
dependency
groupIdcom.itextpdf.tool/groupId
artifactIdxmlworker/artifactId
version5.5.6/version
/dependency123456789101112131415
代码如下,覆写XMLWorkerFontProvider$getFont即可读取中文
public void createPdf(String src, String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(src), null, new XMLWorkerFontProvider(){ public Font getFont(final String fontname, final String encoding,
final boolean embedded, final float size, final int style,
final BaseColor color) {
BaseFont bf = null;
try {
bf = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font font = new Font(bf, size, style, color);
font.setColor(color);
return font;
}
});
document.close();
}1234567891011121314151617181920212223
创建时,使用系统(windows下)的字体即可
BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont);
如何用java读取pdf文档的部分内容
你需要用到PDFbox api
例子如下
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
try {
PDDocument document = null;
document = PDDocument.load(new File("test.pdf"));
document.getClass();
if (!document.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper Tstripper = new PDFTextStripper();
String st = Tstripper.getText(document);
System.out.println("Text:" + st);
}
} catch (Exception e) {
e.printStackTrace();
}
怎么用java读取pdf文件内容
你可以把pdf转成word在进行读取
推荐使用转转大师pdf转word转换器,免费的在线工具
百度搜索下,在线免费转换就行了,不用下载注册,很方便
java读pdf的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java读pdf图片、java读pdf的信息别忘了在本站进行查找喔。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。