基本信息
源码名称:C#获取pdf中的文字和图片
源码大小:6.82M
文件格式:.zip
开发语言:C#
更新时间:2018-06-11
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
此程序主要利用PDFBOX提取PDF中文字及图片。C#解析PDF相关资料太少了,研究了好多天,现在放上来,希望朋友们少走些弯路。适用于未加密的PDF。


以下是获取的文字截图:


using System;
using System.Collections.Generic;
using System.Text;
//
using org.pdfbox.pdmodel;
using org.pdfbox.util;
using org.pdfbox.cos;
using org.pdfbox.pdmodel.graphics.xobject;
using org.pdfbox.pdmodel.interactive.documentnavigation.outline;

namespace PDFParser
{
    public class PDFParser
    {
        /// <summary>
        /// 解析PDF,获取PDF文字
        /// </summary>
        /// <param name="file_path_name">PDF文件路径</param>
        public static string GetText(string file_path_name)
        {
            PDDocument doc = PDDocument.load(file_path_name);
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(doc);
        }
        /// <summary>
        /// 解析PDF,获取PDF文字
        /// </summary>
        /// <param name="file_path_name">PDF文件路径</param>
        /// <param name="startPage">起始页,从第1页开始</param>
        /// <param name="endPageValue">结束页</param>
        public static string GetText(string file_path_name,int startPageValue,int endPageValue)
        {
            PDDocument doc = PDDocument.load(file_path_name);
            PDFTextStripper stripper = new PDFTextStripper();
            // 设置是否排序
            stripper.setSortByPosition(true);

            stripper.setStartPage(startPageValue);
            stripper.setEndPage(endPageValue);
            return stripper.getText(doc);
        }
        /// <summary>
        /// 解析PDF,当pdf是由图片构成时,导出每张图片
        /// </summary>
        /// <param name="file_path_name">PDF文件路径</param>
        /// <param name="outFilePath">输出图片路径</param>
        public static void GetImage(string file_path_name,string outFilePath)
        {
            // 内存中存储的PDF Document
            PDDocument document = null;
            try
            {
                // 装载文件
                document = PDDocument.load(file_path_name);
                PDDocumentCatalog cata = document.getDocumentCatalog();
                //
                COSDictionary cosDictionary = cata.getCOSDictionary();

                // 得到所有页面的List
                java.util.List pages = cata.getAllPages();
                // 用来记录每个页面的图片名
                int count = 1;
                // 遍历每一页
                for (int i = 0; i < pages.size(); i )
                {
                    PDPage page = (PDPage)pages.get(i);
                    if (null != page)
                    {
                        // 得到每个页面资源
                        PDResources res = page.findResources();
                        // 获取页面图片信息   
                        java.util.Map imgs = res.getImages();
                        if (null != imgs)
                        {
                            java.util.Set keySet = imgs.keySet();
                            java.util.Iterator it = keySet.iterator();
                            while (it.hasNext())
                            {
                                Object obj = it.next();
                                PDXObjectImage img = (PDXObjectImage)imgs.get(obj);
                                // 保存图片
                                img.write2file(outFilePath "page" (i 1) "_image" count);
                                count ;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (null != document)
                    document.close();
            }

        }

        /// <summary>
        /// 解析PDF,得到pdf的元数据
        /// </summary>
        /// <param name="file_path_name">PDF文件路径</param>
        public static PDDocumentInformation GetMetadata(string file_path_name)
        {
            //导入pdf文档
            PDDocument document = PDDocument.load(file_path_name);
            PDDocumentInformation info = document.getDocumentInformation();
            document.close();
            return info;
        }
        /// <summary>
        /// 解析PDF,得到一二级目录书签结构
        /// </summary>
        /// <param name="file_path_name">PDF文件路径</param>
        private static void GetAllCata(string file_path_name)
        {
            // 内存中存储的PDF Document
            PDDocument document = null;
            try
            {
                document = PDDocument.load(file_path_name);
                // 获得该pdf的目录对象
                PDDocumentOutline root = document.getDocumentCatalog().getDocumentOutline();
                if (root != null)
                {
                    // 一级目录
                    PDOutlineItem item = root.getFirstChild();
                    while (item != null)
                    {
                        Console.WriteLine("Item:" item.getTitle());
                        // 二级目录
                        PDOutlineItem child = item.getFirstChild();
                        while (child != null)
                        {
                            Console.WriteLine("    Child:" child.getTitle());
                            // 下一个二级目录
                            child = child.getNextSibling();
                        }
                        // 下一个一级目录
                        item = item.getNextSibling();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                if (null != document)
                    document.close();
            }
        }

        /// <summary>
        /// 解析PDF,得到pdf的元数据
        /// </summary>
        /// <param name="file_path_name">PDF文件路径</param>
        private static PDDocumentInformation GetMetadata2(string file_path_name)
        {
            //导入pdf文档
            PDDocument document = null;
            PDDocumentInformation info = null;
            try
            {
                document = PDDocument.load(file_path_name);
                info = document.getDocumentInformation();
                // 输出总页数
                Console.WriteLine("总页数=" document.getNumberOfPages());
                // 输出该文档标题
                Console.WriteLine("文档标题Title=" info.getTitle());
                // 输出该文档作者
                Console.WriteLine("文档作者Author=" info.getAuthor());
                // 输出该文档主题
                Console.WriteLine("文档主题Subject=" info.getSubject());
                // 输出该文档关键字
                Console.WriteLine("文档关键字Keywords=" info.getKeywords());
                // 输出该文档创建者
                Console.WriteLine("文档创建者Creator=" info.getCreator());
                // 输出该文档制作者
                Console.WriteLine("文档制作者Producer=" info.getProducer());
                // 输出该文档创建日期
                Console.WriteLine("文档创建日期Creation Date=" info.getCreationDate());
                // 输出该文档修改日期
                Console.WriteLine("文档修改日期Modification Date=" info.getModificationDate());
                // 输出该文档
                Console.WriteLine("输出该文档Trapped=" info.getTrapped());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                if (null != document)
                    document.close();
            }
            return info;
        }
    }
}