【抓取PDF内容改文件名】将PDF区域的内容抓取出来,修改PDF文件名,基于WPF和百度ocr的解决方案

作者:悠悠我心2025.03.06 14:49浏览量:12

简介:建出一套基于 WPF 和百度 OCR 从 PDF 区域抓取内容并修改文件名的有效解决方案

项目场景

在很多实际业务场景中,我们可能需要从 PDF 文件的特定区域提取文本信息,并根据这些信息修改 PDF 文件的名称。例如:
档案管理:在企业的档案管理系统中,PDF 文档可能包含特定的编号、日期等关键信息。通过提取这些信息,可以自动将 PDF 文件重命名,方便后续的检索和管理。
发票处理:财务部门在处理大量的 PDF 格式发票时,需要从发票上提取发票号码、日期等信息,然后根据这些信息对发票文件进行重命名,以便更好地进行分类和归档。

详细步骤

1. 创建 WPF 项目

首先,打开 Visual Studio,创建一个新的 WPF 应用程序项目。

2. 安装必要的 NuGet 包

iTextSharp:用于处理 PDF 文件,提取特定区域的内容。在 Visual Studio 的 “工具” -> “NuGet 包管理器” -> “管理解决方案的 NuGet 包” 中搜索 iTextSharp 并安装。
Baidu.Aip.Ocr:用于调用百度 OCR 接口进行文字识别。同样在 NuGet 包管理器中搜索 Baidu.Aip.Ocr 并安装。

3. 配置百度 OCR 服务

登录百度智能云官网,创建一个 OCR 应用,获取 API Key 和 Secret Key。

4. 编写代码实现功能

以下是一个示例代码,演示了如何从 PDF 文件的特定区域提取文本信息,并根据这些信息修改 PDF 文件的名称。
csharp
using System;
using System.IO;
using System.Windows;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using Baidu.Aip.Ocr;

namespace PdfTextExtractionAndRename
{
public partial class MainWindow : Window
{
private const string ApiKey = “your_api_key”;
private const string SecretKey = “your_secret_key”;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void ProcessPdfButton_Click(object sender, RoutedEventArgs e)
    {
        // 选择 PDF 文件
        Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
        openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
        if (openFileDialog.ShowDialog() == true)
        {
            string pdfFilePath = openFileDialog.FileName;

            // 提取 PDF 特定区域的内容
            string extractedText = ExtractTextFromPdf(pdfFilePath, 1, 100, 100, 200, 200);

            // 使用百度 OCR 识别文本
            string recognizedText = RecognizeText(extractedText);

            // 修改 PDF 文件名称
            RenamePdfFile(pdfFilePath, recognizedText);
        }
    }

    private string ExtractTextFromPdf(string pdfFilePath, int pageNumber, float left, float bottom, float right, float top)
    {
        using (PdfReader reader = new PdfReader(pdfFilePath))
        {
            Rectangle rect = new Rectangle(left, bottom, right, top);
            RenderFilter[] filters = { new RegionTextRenderFilter(rect) };
            ITextExtractionStrategy strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filters);
            return PdfTextExtractor.GetTextFromPage(reader, pageNumber, strategy);
        }
    }

    private string RecognizeText(string imageData)
    {
        var client = new Ocr(ApiKey, SecretKey);
        client.Timeout = 60000; // 设置超时时间

        var result = client.GeneralBasic(imageData);
        if (result.ContainsKey("words_result"))
        {
            string text = "";
            foreach (var item in result["words_result"].Array)
            {
                text += item["words"].ToString();
            }
            return text;
        }
        return "";
    }

    private void RenamePdfFile(string pdfFilePath, string newName)
    {
        string directory = Path.GetDirectoryName(pdfFilePath);
        string newFilePath = Path.Combine(directory, newName + ".pdf");
        File.Move(pdfFilePath, newFilePath);
        MessageBox.Show($"文件已重命名为: {newFilePath}");
    }
}

}

5. 设计 WPF 界面

在 MainWindow.xaml 中添加一个按钮,用于触发处理 PDF 文件的操作。
xml

<Window x:Class="PdfTextExtractionAndRename.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="PDF 文本提取与重命名" Height="350" Width="525">
<Grid>
<Button Content="处理 PDF 文件" HorizontalAlignment="Left" Margin="200,150,0,0" VerticalAlignment="Top" Width="120" Click="ProcessPdfButton_Click"/>
</Grid>
</Window>

6. 运行程序

编译并运行程序,点击 “处理 PDF 文件” 按钮,选择要处理的 PDF 文件,程序将自动提取指定区域的文本信息,使用百度 OCR 进行识别,并根据识别结果修改 PDF 文件的名称。
注意事项
请将 ApiKey 和 SecretKey 替换为你自己的百度 OCR 应用的 API Key 和 Secret Key。
示例代码中的 PDF 区域提取参数(left, bottom, right, top)需要根据实际情况进行调整,以确保提取到正确的区域内容。
百度 OCR 服务有一定的调用限制,请根据实际使用情况进行合理调用。

通过上述步骤,你已构建出一套基于 WPF 和百度 OCR 从 PDF 区域抓取内容并修改文件名的有效解决方案。这一方案能显著提升文档处理效率,助力企业或个人在档案管理、财务发票处理等领域实现智能化、规范化操作。在实际应用中,你可根据具体需求进一步优化代码,如调整 OCR 识别的参数以提升识别精度,或完善文件重命名逻辑使其更符合业务规则。希望本方案能切实解决你的问题,为你的工作和生活带来便利,开启高效处理 PDF 文档的新旅程。

相关文章推荐

发表评论