WPF PDF发票信息取得

CSDN代码

利用Spire获取PDF版发票信息

xaml(基础UI)

<Window x:Class="storage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:storage"
        mc:Ignorable="d" ResizeMode="CanMinimize"
        Title="MainWindow" Height="200" Width="435">
    <Grid>
        <TextBlock x:Name="Display" Width="200" Height="25" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="15" Margin="0,20,0,0" TextAlignment="Center">TextBlock</TextBlock>
        <Button x:Name="Open" Width="70" Height="25" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="40,60,0,0">Open</Button>
        <Button x:Name="Check" Width="70" Height="25" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,60,40,0">Check</Button>
        <ProgressBar x:Name="processBar" Width="350" Height="30" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,20"></ProgressBar>
    </Grid>
</Window>

C#(后台)

using Microsoft.Win32;
using Spire.Pdf;
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;

namespace storage
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string[] pdfFileName;

        public MainWindow()
        {
            InitializeComponent();
            Open.Click += Open_Click;
            Check.Click += Check_Click;
            Display.Text = string.Empty;
        }

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择文件";
            openFileDialog.Filter = "pdf File|*.pdf";
            openFileDialog.FileName = "选择文件夹.";

            openFileDialog.Multiselect = true; //允许同时选择多个文件

            bool? result = openFileDialog.ShowDialog();

            if (result == true)
            {
                pdfFileName = openFileDialog.FileNames;
                Display.Text = $"Total:{pdfFileName.Length} files";
            }
        }

        private void Check_Click(object sender, RoutedEventArgs e)
        {
            processBar.Value = 0;
            Thread tr = new Thread(ReadSource);
            tr.Start();
            Open.IsEnabled = false;
            Check.IsEnabled = false;
        }

        private void ReadSource()
        {
            try
            {
                string pdfText = string.Empty;
                double total = 0.0;
                PdfDocument document = new PdfDocument();
                for (int i = 0; i < pdfFileName.Length; i++)
                {
                    document.LoadFromFile(pdfFileName[i]);

                    StringBuilder content = new StringBuilder();
                    content.Append(document.Pages[0].ExtractText());

                    string fileName = pdfFileName[i].Replace(".pdf", ".txt");
                    File.WriteAllText(fileName, content.ToString());

                    string pdfSource = File.ReadAllText(fileName);
                    int textStart = pdfSource.IndexOf("(小写)");
                    pdfSource = pdfSource.Substring(textStart);
                    int textEnd = pdfSource.IndexOf(" ");
                    pdfSource = pdfSource.Substring(5, textEnd - 7);

                    string storageNumber = pdfFileName[i].Substring(pdfFileName[i].Length - 12, 8);
                    pdfText += "税号: " + storageNumber + "  价格: " + pdfSource + "\r\n";
                    total += double.Parse(pdfSource);

                    Dispatcher.Invoke(() => { processBar.Value += 100 / (double)pdfFileName.Length; });
                }

                Dispatcher.Invoke(() => { Display.Text = "completed"; });
                pdfText += "总价格: " + total.ToString();
                File.WriteAllText("Pdf.txt", pdfText);
                System.Diagnostics.Process.Start("Pdf.txt");
            }
            catch (Exception ex)
            {
                Dispatcher.Invoke(() => { Display.Text = ex.Message; });
                return;
            }
            finally
            {
                Dispatcher.Invoke(() =>
                {
                    Open.IsEnabled = true;
                    Check.IsEnabled = true;
                });
            }
        }
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值