WPF PDF发票信息取得

时间:2019-02-19
本文章向大家介绍WPF PDF发票信息取得,主要包括WPF PDF发票信息取得使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

利用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;
                });
            }
        }
    }
}