栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java提取PDF文件中的数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java提取PDF文件中的数据

Java提取PDF文件中的数据 PDFBox PDFBox是一个开源的Apache的开源Java库,用来支持PDF文档的开发和转换,内载了对于PDF执行的各种操作方法.Apache PDFBox 还包括几个命令行实用程序。Apache PDFBox 在 Apache License v2.0 下发布。 PDFBox的组件
组件内容
PDFBox这是PDFBox的主要部分。 它包含与内容提取和操作相关的类和接口
FontBox它包含与font相关的类和接口,使用这些类我们可以修改PDF文档的文本字体
XmpBox包含处理XMP元数据的类和接口
Preflight此组件用于根据PDF/A-1b标准验证PDF文件
Maven项目导入依赖


        org.apache.pdfbox
        pdfbox
        2.0.21


        org.apache.pdfbox
        fontbox
        2.0.21


        org.apache.pdfbox
        jempbox
        1.8.13


         org.apache.pdfbox 
         xmpbox 
         2.0.0 

 
         org.apache.pdfbox 
         preflight 
         2.0.0 
 
 
         org.apache.pdfbox 
         pdfbox-tools 
         2.0.0 


读取文字型PDF文档的例子
public class ReadChar
{
	private String pdfpath="";//PDF文件的路径
	private String outputPath="";//以txt文件输出的路径
	private File[] files=null;
	public static void main(String[] args)
	{
		try
		{
				File pdfpath=new File(pdfpath);
           		files=pdfpath.listFiles();
           		for(File file:files)
           		{
					 PDdocument doc=PDdocument.load(file);
               		 int pagenum=doc.getNumberOfPages();
          		     FileOutputStream fos=new FileOutputStream( outputPath+"/"+file.getName().replace(".pdf","")+".txt");
       		         Writer writer=new OutputStreamWriter(fos,"UTF-8");
       		         PDFTextStripper stripper=new PDFTextStripper();
     		         stripper.setSortByPosition(true);
     		         stripper.setStartPage(0);
    		         stripper.setEndPage(pagenum);
     		         stripper.writeText(doc,writer);
     		         writer.close();
     		         doc.close();
				}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
读取图片型PDF文档的例子
public class ReadImg
{
	private String pdfpath="";//PDF文件的路径
	private String outputPath="";//以图片文件输出的路径
	private File[] files=null;
	public static void main()
	{
		try
		{
			String imgpath=outputPath+"/"+file.getName().replace(".pdf","");
			 File img_temp=new File(imgpath);
			 		if(!img_temp.exists())
                    {
                        img_temp.mkdir();
                    }
             imgpath=imgpath+"/"+file.getName().replace(".pdf","");
             imgpath=imgpath+"_"+i+".png";
             outputStream = new BufferedOutputStream(new FileOutputStream(imgpath));
             BufferedImage image = pdfRenderer.renderImageWithDPI(i, 90, ImageType.RGB);
             ImageIO.write(image, "png", outputStream);
             outputStream.close();
             System.out.println("The "+file.getName()+"'s"+i+"' has complete");
		}
			catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号