在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#文件解析和处理 → C#获取pdf中的文字和图片

C#获取pdf中的文字和图片

C#文件解析和处理

下载此实例
  • 开发语言:C#
  • 实例大小:6.82M
  • 下载次数:234
  • 浏览次数:2723
  • 发布时间:2018-06-11
  • 实例类别:C#文件解析和处理
  • 发 布 人:jueshi44
  • 文件格式:.zip
  • 所需积分:2
 相关标签: pdf C# 导入

实例介绍

【实例简介】此程序主要利用PDFBOX提取PDF中文字及图片。C#解析PDF相关资料太少了,研究了好多天,现在放上来,希望朋友们少走些弯路。适用于未加密的PDF。

【实例截图】

from clipboard


以下是获取的文字截图:

from clipboard


【核心代码】

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;
        }
    }
}



标签: pdf C# 导入

实例下载地址

C#获取pdf中的文字和图片

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 564297091 发表于: 2023-01-30 13:52 51
解析正确率只有60%

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警