前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【图片PDF区域识别改名】基于WPF和腾讯云API实现PDF文档扫描、指定区域文字识别、固定位置文字识别以及文件批量重命名功能

【图片PDF区域识别改名】基于WPF和腾讯云API实现PDF文档扫描、指定区域文字识别、固定位置文字识别以及文件批量重命名功能

原创
作者头像
不负众望
发布2025-02-25 09:26:36
发布2025-02-25 09:26:36
8600
代码可运行
举报
运行总次数:0
代码可运行

项目背景

票据处理:例如发票、收据等,在这些票据上特定位置通常包含关键信息,如发票号码、金额、日期等。通过指定识别区域,可以快速准确地提取这些信息并整理到 Excel 表格中,便于财务人员进行数据统计和管理。

表单数据提取:各种业务表单(如调查问卷、申请表等)上,不同位置有不同的字段内容。利用该程序可以批量从表单 PDF 文件中提取指定区域的信息,提高数据录入效率。

文档数据汇总:对于一些格式固定的文档,如合同、报告等,其中某些特定区域包含重要的数据或条款。可以通过指定识别区域将这些数据提取出来,方便进行分析和汇总。

以下是基于 WPF 和腾讯云 API 实现 PDF 文档扫描、指定区域文字识别、固定位置文字识别以及文件批量重命名功能的详细步骤和代码示例。

步骤概述

  1. 创建 WPF 项目:使用 Visual Studio 创建一个新的 WPF 应用程序项目。
  2. 安装腾讯云 SDK:通过 NuGet 包管理器安装腾讯云的 OCR SDK。
  3. 配置腾讯云凭证:在腾讯云控制台获取 API 密钥,并在代码中配置。
  4. 实现 PDF 文档扫描和文字识别功能:使用腾讯云 OCR API 对 PDF 文档进行处理。
  5. 实现指定区域和固定位置文字识别功能:通过设置识别区域参数实现。
  6. 实现文件批量重命名功能:根据识别结果对文件进行重命名。

详细步骤和代码

1. 创建 WPF 项目

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

2. 安装腾讯云 SDK

在 Visual Studio 的 “工具” -> “NuGet 包管理器” -> “管理解决方案的 NuGet 程序包” 中,搜索并安装 TencentCloudSDK

3. 配置腾讯云凭证

在腾讯云控制台获取 API 密钥(SecretId 和 SecretKey),并在代码中配置。

4. 实现 WPF 界面

MainWindow.xaml 中添加以下代码:

代码语言:javascript
代码运行次数:0
复制
<Window x:Class="TencentCloudOCRDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PDF OCR and File Rename" Height="450" Width="800">
    <Grid>
        <Button Content="选择PDF文件" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="150" Click="SelectPDFButton_Click"/>
        <TextBox x:Name="ResultTextBox" HorizontalAlignment="Left" Height="350" Margin="20,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="750"/>
    </Grid>
</Window>
5. 实现后台代码

MainWindow.xaml.cs 中添加以下代码:

代码语言:javascript
代码运行次数:0
复制
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Ocr.V20181119;
using TencentCloud.Ocr.V20181119.Models;

namespace TencentCloudOCRDemo
{
    public partial class MainWindow : Window
    {
        private const string SecretId = "your_secret_id";
        private const string SecretKey = "your_secret_key";

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void SelectPDFButton_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Filter = "PDF文件 (*.pdf)|*.pdf";
            if (openFileDialog.ShowDialog() == true)
            {
                string filePath = openFileDialog.FileName;
                string result = await PerformOCR(filePath);
                ResultTextBox.Text = result;

                // 示例:文件批量重命名
                string newFileName = $"Renamed_{Path.GetFileName(filePath)}";
                string newFilePath = Path.Combine(Path.GetDirectoryName(filePath), newFileName);
                File.Move(filePath, newFilePath);
            }
        }

        private async System.Threading.Tasks.Task<string> PerformOCR(string filePath)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = SecretId,
                    SecretKey = SecretKey
                };
                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = "ocr.tencentcloudapi.com";
                clientProfile.HttpProfile = httpProfile;

                OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);

                var req = new PdfOcrRequest();
                req.FileUrl = filePath;

                // 指定区域文字识别示例
                // var textDetectionPoints = new TextDetectionPoint[]
                // {
                //     new TextDetectionPoint { X = 100, Y = 100 },
                //     new TextDetectionPoint { X = 200, Y = 100 },
                //     new TextDetectionPoint { X = 200, Y = 200 },
                //     new TextDetectionPoint { X = 100, Y = 200 }
                // };
                // req.TextDetectionPoints = textDetectionPoints;

                var resp = await client.PdfOcr(req);

                string resultText = string.Join("\n", resp.TextDetections.Select(t => t.DetectedText));
                return resultText;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"OCR识别出错: {ex.Message}");
                return string.Empty;
            }
        }
    }

注意事项

  • 确保你的腾讯云账户有足够的权限和额度使用 OCR 服务。
  • 由于使用了网络请求,代码中使用了 async/await 来避免阻塞 UI 线程。
  • 代码中的 FileUrl 参数可以是本地文件路径或远程文件 URL,根据实际情况进行调整。

通过以上步骤和代码,你可以实现基于 WPF 和腾讯云 API 的 PDF 文档扫描、指定区域文字识别、固定位置文字识别以及文件批量重命名功能。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目背景
  • 步骤概述
  • 详细步骤和代码
  • 注意事项
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档