C# 中的發票處理:使用 .NET 產生、擷取並自動化 PDF 發票

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

使用 IronPDF 在C# .NET中進行發票處理,涵蓋了文件的整個生命週期:從 HTML 模板生成專業的PDF 發票,符合ZUGFeRD和Factur-X電子發票標準,使用文字解析和AI 驅動的處理從接收到的發票中提取結構化資料,以及構建與 QuickBooks、Xero 和 SAP 等會計系統集成的批量自動化管道。

TL;DR:快速入門指南

本教學涵蓋了在 C# .NET 中產生、提取和自動化 PDF 發票,包括電子發票合規性、AI 驅動的解析和會計系統整合。

  • 適用對象:建立發票模組、應付帳款自動化或電子發票合規的 .NET 開發人員。
  • 您將建立: HTML 模板化的發票生成,包括項目明細和稅額計算,支付連結的 QR 碼,符合 ZUGFeRD/Factur-X 的 PDF/A-3 輸出,使用正則表達式的文字萃取,AI 驅動的發票解析,以及與會計系統整合的批次處理。
  • 運行環境: .NET 10、.NET 8 LTS、.NET Framework 4.6.2+ 和 .NET Standard 2.0。沒有外部服務依賴。
  • 何時使用這種方法: 當您需要生成發票 PDF,符合歐盟電子發票法規,或從供應商發票中提取數據以處理應付帳款時。
  • 技術重要性: IronPDF 可精確將HTML 渲染為 PDF,支援嵌入 XML 的 PDF/A-3,並提供與正則表達式或 AI 搭配的文字萃取 API,以將非結構化的發票轉換為結構化數據。

只需幾行程式碼即可產生您的第一份 PDF 發票:

  1. 1使用 IronPDF 安裝 NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2複製並運行這段程式碼。

    var renderer = new IronPdf.ChromePdfRenderer();
    var pdf = renderer.RenderHtmlAsPdf("<h1>Invoice #1001</h1><p>Total: $500.00</p>");
    pdf.SaveAs("invoice.pdf");
  3. 3部署到您的生產環境進行測試

    立即在您的項目中開始使用 IronPDF,並使用 免費試用
    arrow pointer

購買或註冊 IronPDF 的 30 天試用版後,請在應用程式開始時新增您的授權金鑰。

IronPdf.License.LicenseKey = "KEY";

立即在您的項目中開始使用 IronPDF,免費試用。

第一步:
arrow pointer
NuGet使用NuGet安裝

PM > Install-Package IronPdf

在 NuGet 包管理器控制台中運行上述命令安裝 IronPDF,或在 NuGet 包管理器中搜索該包。
目錄
NuGet使用NuGet安裝

PM > Install-Package IronPdf

在 NuGet 包管理器控制台中運行上述命令安裝 IronPDF,或在 NuGet 包管理器中搜索該包。

發票生命週期是什麼?為什麼 PDF 仍然是標準格式?

在深入研究程式碼之前,了解發票在現代業務系統中的完整流程會很有幫助。 發票生命週期包括五個不同的階段:產生、分發、接收、資料提取和會計整合。

發票流程始於產生發票。 企業創建發票,其中包括明細項目、價格、稅金計算、付款條款和品牌識別。 發票必須看起來專業,並符合所有法律要求。 接下來是分發環節,即透過電子郵件、客戶入口網站或傳統郵件將發票發送給客戶。 客戶收到文件後,應付帳款團隊會接收該文件並準備處理。 資料提取從發票中提取關鍵信息,例如供應商詳細信息、明細項目、總計和到期日,以便與採購訂單進行核對和匹配。 最後,會計整合會將這些資料匯入 QuickBooks、Xero 或 SAP 等財務系統,用於支付和記錄保存。

為什麼多年來PDF仍然是使用最廣泛的格式? 歸根結底,是多種優勢的獨特組合。 無論您使用什麼裝置或作業系統,PDF 都能確保您的發票格式保持一致。 無論有人在 Windows、Mac 還是手機上打開您的發票,它看起來都和您設計的一樣。 PDF 檔案也很難被誤修改,因此比 Word 或 Excel 等格式更能保護文件的完整性。 您可以添加數位簽名以驗證真實性,並使用加密技術來確保安全。 最重要的是,PDF 已成為所有商業系統都能識別和支援的通用標準。

當然,這是一個挑戰。 PDF檔案的設計目的是為了方便人閱讀,而不是為了方便電腦處理。 PDF 檔案不是將資訊儲存在結構化資料中,而是根據文字、線條、形狀和圖像在頁面上的位置來保存它們。 這就是像 IronPDF 這樣的工具如此有用的原因,因為它們讓將人類友好的文件轉變為軟體可處理的數據成為可能。


如何在 C# 中產生專業的 PDF 發票

以程式設計方式產生發票需要將結構化資料(例如客戶資訊、明細專案和計算結果)轉換為精美的 PDF 文件。 IronPDF 利用 HTML 和 CSS 這兩種大多數開發人員已經很熟悉的技術,讓這一切變得非常簡單。

在本教程中,我們將探討您在現實世界中可能遇到的各種情況。 您還可以在下面的這裡下載該專案。

如何建立發票 HTML 模板

IronPDF 發票產生的基礎是 HTML。 您無需費力處理底層 PDF 繪圖指令,只需使用標準 HTML 和 CSS 設計發票,然後讓 IronPDF 基於 Chrome 的渲染引擎將其轉換為像素級完美的 PDF 即可。

以下是一個示範此方法的基本發票範本:

using IronPdf;

// Define the HTML template for a basic invoice
// Uses inline CSS for styling headers, tables, and totals
string invoiceHtml = @"
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; padding: 40px; }
.header { text-align: right; margin-bottom: 40px; }
.company-name { font-size: 24px; font-weight: bold; color: #333; }
.invoice-title { font-size: 32px; margin: 20px 0; }
.bill-to { margin: 20px 0; }
table { width: 100%; border-collapse: collapse; margin: 20px 0; }
th { background-color: #2A95D5; color: white; padding: 10px; text-align: left; }
td { padding: 10px; border-bottom: 1px solid #ddd; }
.total { text-align: right; font-size: 20px; font-weight: bold; margin-top: 20px; }
</style>
</head>
<body>
<div class='header'>
<div class='company-name'>Your Company Name</div>
<div>123 Business Street</div>
<div>City, State 12345</div>
</div>

<div class='invoice-title'>INVOICE</div>

<div class='bill-to'>
<strong>Bill To:</strong><br>
Customer Name<br>
456 Customer Avenue<br>
City, State 67890
</div>

<table>
<tr>
    <th>Description</th>
    <th>Quantity</th>
    <th>Price</th>
    <th>Total</th>
</tr>
<tr>
    <td>Web Development Services</td>
    <td>10 hours</td>
    <td>$100.00</td>
    <td>$1,000.00</td>
</tr>
<tr>
    <td>Consulting</td>
    <td>5 hours</td>
    <td>$150.00</td>
    <td>$750.00</td>
</tr>
</table>

<div class='total'>Total: $1,750.00</div>
</body>
</html>";

// Initialize the Chrome-based PDF renderer
var renderer = new ChromePdfRenderer();

// Convert the HTML string to a PDF document
var pdf = renderer.RenderHtmlAsPdf(invoiceHtml);

// Save the generated PDF to disk
pdf.SaveAs("basic-invoice.pdf");

範例輸出

這種方法提供了極大的靈活性。 任何在 Chrome 中有效的 CSS 程式碼都可以在 PDF 中生效,包括 flexbox、網格佈局和自訂字體等現代功能。 您甚至可以透過引用 URL 或本機檔案路徑來使用外部樣式表和圖像。

如何新增動態行項目並計算總計

真實的發票很少有靜態內容。 您需要從資料庫中填入行項目,計算小計,套用稅率,並格式化貨幣值。 以下範例展示了一個可用於生產環境的動態發票產生模式:

using IronPdf;
using System;
using System.Collections.Generic;
using System.Linq;

// Represents a single line item on an invoice
public class InvoiceLineItem
{
    public string Description { get; set; }
    public decimal Quantity { get; set; }
    public decimal UnitPrice { get; set; }

    // Auto-calculates line total from quantity and unit price
    public decimal Total => Quantity * UnitPrice;
}

// Represents a complete invoice with customer details and line items
public class Invoice
{
    public string InvoiceNumber { get; set; }
    public DateTime InvoiceDate { get; set; }
    public string CustomerName { get; set; }
    public string CustomerAddress { get; set; }
    public List<InvoiceLineItem> LineItems { get; set; }

    // Computed properties for invoice totals
    public decimal Subtotal => LineItems.Sum(item => item.Total);
    public decimal TaxRate { get; set; } = 0.08m;  // Default 8% tax rate
    public decimal Tax => Subtotal * TaxRate;
    public decimal Total => Subtotal + Tax;
}

// Generates PDF invoices from Invoice objects using HTML templates
public class InvoiceGenerator
{
    public PdfDocument GenerateInvoice(Invoice invoice)
    {
        // Build HTML table rows dynamically from line items
        string lineItemsHtml = string.Join("", invoice.LineItems.Select(item => $@"
            <tr>
                <td>{item.Description}</td>
                <td>{item.Quantity}</td>
                <td>${item.UnitPrice:F2}</td>
                <td>${item.Total:F2}</td>
            </tr>
        "));

        // Build the complete HTML invoice using string interpolation
        // All invoice data is injected into the template dynamically
        string invoiceHtml = $@"
<!DOCTYPE html>
<html>
<head>
    <style>
        body {{ font-family: Arial, sans-serif; padding: 40px; }}
        .header {{ text-align: right; margin-bottom: 40px; }}
        .company-name {{ font-size: 24px; font-weight: bold; color: #333; }}
        .invoice-details {{ margin: 20px 0; }}
        table {{ width: 100%; border-collapse: collapse; margin: 20px 0; }}
        th {{ background-color: #2A95D5; color: white; padding: 10px; text-align: left; }}
        td {{ padding: 10px; border-bottom: 1px solid #ddd; }}
        .totals {{ text-align: right; margin-top: 20px; }}
        .totals div {{ margin: 5px 0; }}
        .grand-total {{ font-size: 20px; font-weight: bold; color: #2A95D5; }}
    </style>
</head>
<body>
    <div class='header'>
        <div class='company-name'>Your Company Name</div>
    </div>

    <h1>INVOICE</h1>

    <div class='invoice-details'>
        <strong>Invoice Number:</strong> {invoice.InvoiceNumber}<br>
        <strong>Date:</strong> {invoice.InvoiceDate:MMM dd, yyyy}<br>
        <strong>Bill To:</strong> {invoice.CustomerName}<br>
        {invoice.CustomerAddress}
    </div>

    <table>
        <tr>
            <th>Description</th>
            <th>Quantity</th>
            <th>Unit Price</th>
            <th>Total</th>
        </tr>
        {lineItemsHtml}
    </table>

    <div class='totals'>
        <div>Subtotal: ${invoice.Subtotal:F2}</div>
        <div>Tax ({invoice.TaxRate:P0}): ${invoice.Tax:F2}</div>
        <div class='grand-total'>Total: ${invoice.Total:F2}</div>
    </div>
</body>
</html>";

        // Render HTML to PDF and return the document
        var renderer = new ChromePdfRenderer();
        return renderer.RenderHtmlAsPdf(invoiceHtml);
    }
}

範例輸出

Invoice 類別封裝了所有發票數據,並具備小計、稅金和總額的計算屬性。 生成器使用字串插值將這些資料轉換為 HTML,然後將其渲染為 PDF。 這種關注點分離使得程式碼易於維護和測試。

如何在發票上新增公司品牌識別和浮水印

專業的發票需要品牌元素,例如徽標,在某些情況下還需要浮水印來表明付款狀態。 IronPDF 支援在 HTML 中嵌入圖像以及在渲染後以程式設計方式新增浮水印。

using IronPdf;

var renderer = new ChromePdfRenderer();

// Invoice HTML template with company logo embedded via URL
// Logo can also be Base64-encoded or a local file path
string htmlWithLogo = @"
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; padding: 40px; }
.logo { width: 200px; margin-bottom: 20px; }
</style>
</head>
<body>
<div style='text-align: center;'>
<img src='https://yourcompany.com/logo.png' alt='Company Logo' class='logo' />
</div>
<h1>INVOICE</h1>
<p><strong>Invoice Number:</strong> INV-2024-001</p>
<p><strong>Total:</strong> $1,250.00</p>
</body>
</html>";

// Render the HTML to PDF
var pdf = renderer.RenderHtmlAsPdf(htmlWithLogo);

// Apply a diagonal "UNPAID" watermark to mark invoice status
// 30% opacity keeps the content readable while the watermark is visible
pdf.ApplyWatermark("<h1 style='color: red;'>UNPAID</h1>",
    opacity: 30,
    rotation: 45,
    verticalAlignment: IronPdf.Editing.VerticalAlignment.Middle);

pdf.SaveAs("invoice-with-watermark.pdf");

範例輸出

ApplyWatermark 方法接受 HTML 內容,讓您完全控制浮水印的外觀。 您可以調整不透明度、旋轉角度和位置,以達到您想要的效果。 這對於將發票標記為「已付款」、「草稿」或「已取消」尤其有用,而無需重新產生整個文件。

如何嵌入支付連結的二維碼

現代發票通常包含二維碼,客戶可以掃描二維碼快速付款。 儘管 IronPDF 專注於 PDF 生成,但它與 IronQR 結合可用於條碼創建:

using IronPdf;
using IronQr;
using IronSoftware.Drawing;

string invoiceNumber = "INV-2026-002";
decimal amount = 1500.00m;

// Create a payment URL with invoice details as query parameters
string paymentUrl = $"https://yourcompany.com/pay?invoice={invoiceNumber}&amount={amount}";

// Generate QR code from the payment URL using IronQR
QrCode qrCode = QrWriter.Write(paymentUrl);
AnyBitmap qrImage = qrCode.Save();
qrImage.SaveAs("payment-qr.png", AnyBitmap.ImageFormat.Png);

// Build invoice HTML with the QR code image embedded
// Customers can scan the QR to pay directly from their phone
string invoiceHtml = $@"
<!DOCTYPE html>
<html>
<head>
<style>
body {{ font-family: Arial, sans-serif; padding: 40px; }}
.payment-section {{ margin-top: 40px; text-align: center;
                   border-top: 2px solid #eee; padding-top: 20px; }}
.qr-code {{ width: 150px; height: 150px; }}
</style>
</head>
<body>
<h1>INVOICE {invoiceNumber}</h1>
<p><strong>Amount Due:</strong> ${amount:F2}</p>

<div class='payment-section'>
<p><strong>Scan to Pay Instantly:</strong></p>
<img src='payment-qr.png' alt='Payment QR Code' class='qr-code' />
<p style='font-size: 12px; color: #666;'>
    Or visit: {paymentUrl}
</p>
</div>
</body>
</html>";

// Convert HTML to PDF and save
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(invoiceHtml);
pdf.SaveAs($"invoice-{invoiceNumber}.pdf");

範例輸出

二維碼直接連結到付款頁面,減少客戶的支付摩擦,加快您的現金流。 此模式適用於任何支援基於 URL 的付款發起的付款提供者。


如何在 C# 中遵循 ZUGFeRD 與 Factur-X 電子發票標準

電子發票在歐洲正迅速成為強制性要求。 德國率先推出了 ZUGFeRD,法國緊跟在後推出了 Factur-X。 這些標準將機器可讀的 XML 資料嵌入到 PDF 發票中,從而實現自動化處理,同時保持文件的可讀性。 對於在歐洲市場經營的企業而言,理解和實施這些標準變得越來越重要。

什麼是 ZUGFeRD?它是如何運作的?

ZUGFeRD(德國電子發票論壇中央使用者指南)是德國電子發票標準,它將發票資料作為 XML 文件附件嵌入到符合 PDF/A-3 標準的文件中。 嵌入式 XML 無需 OCR 或解析即可實現自動資料擷取。

此標準定義了三個符合性級別,每個級別提供的資料結構化程度逐漸提高:

  • 基本: 包含適合簡單自動化處理的核心發票數據
  • 舒適: 添加詳細信息以支援完全自動化的發票處理
  • 擴展: 包含跨行業複雜業務場景的綜合數據

XML 遵循 UN/CEFACT 跨產業發票 (CII) 模式,該模式已成為歐洲電子發票標準化的基礎。

什麼是Factur-X?它與ZUGFeRD有何不同?

Factur-X 是法國對同一底層標準的實現。 ZUGFeRD 2.0 和 Factur-X 在技術上是相同的。 它們共用基於歐洲標準 EN 16931 的相同 XML 模式和一致性設定檔。差異純粹在於區域命名:根據 ZUGFeRD 規範創建的發票在 Factur-X 下有效,反之亦然。

如何在PDF/A-3發票中嵌入XML資料

IronPDF 提供建立符合規範的電子發票所需的配件功能。 此流程包括產生發票 PDF、根據 CII 架構建立 XML 資料,以及將 XML 作為附件嵌入到發票中並遵循正確的命名約定:

using System;
using System.Xml.Linq;

// Generates ZUGFeRD-compliant invoices by embedding structured XML data
// ZUGFeRD allows automated processing while keeping a human-readable PDF
public class ZUGFeRDInvoiceGenerator
{
    public void GenerateZUGFeRDInvoice(Invoice invoice)
    {
        // First, create the visual PDF that humans will read
        var renderer = new ChromePdfRenderer();
        string invoiceHtml = BuildInvoiceHtml(invoice);
        var pdf = renderer.RenderHtmlAsPdf(invoiceHtml);

        // Define the UN/CEFACT namespaces required by the ZUGFeRD standard
        // These are mandatory for compliance with European e-invoicing regulations
        XNamespace rsm = "urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100";
        XNamespace ram = "urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100";
        XNamespace udt = "urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100";

        // Build the ZUGFeRD XML structure following the Cross-Industry Invoice schema
        var zugferdXml = new XDocument(
            new XDeclaration("1.0", "UTF-8", null),
            new XElement(rsm + "CrossIndustryInvoice",
                new XAttribute(XNamespace.Xmlns + "rsm", rsm.NamespaceName),
                new XAttribute(XNamespace.Xmlns + "ram", ram.NamespaceName),
                new XAttribute(XNamespace.Xmlns + "udt", udt.NamespaceName),

                // Document context identifies which e-invoicing guideline is being followed
                new XElement(rsm + "ExchangedDocumentContext",
                    new XElement(ram + "GuidelineSpecifiedDocumentContextParameter",
                        new XElement(ram + "ID", "urn:cen.eu:en16931:2017")
                    )
                ),

                // Core document identification: invoice number, type, and date
                new XElement(rsm + "ExchangedDocument",
                    new XElement(ram + "ID", invoice.InvoiceNumber),
                    new XElement(ram + "TypeCode", "380"), // 380 = Commercial Invoice per UN/CEFACT
                    new XElement(ram + "IssueDateTime",
                        new XElement(udt + "DateTimeString",
                            new XAttribute("format", "102"),
                            invoice.InvoiceDate.ToString("yyyyMMdd")
                        )
                    )
                ),

                // A complete implementation would include additional sections:
                // - Seller information (ram:SellerTradeParty)
                // - Buyer information (ram:BuyerTradeParty)
                // - Line items (ram:IncludedSupplyChainTradeLineItem)
                // - Payment terms (ram:SpecifiedTradePaymentTerms)
                // - Tax summaries (ram:ApplicableTradeTax)

                // Financial summary with all monetary totals
                new XElement(rsm + "SupplyChainTradeTransaction",
                    new XElement(ram + "ApplicableHeaderTradeSettlement",
                        new XElement(ram + "InvoiceCurrencyCode", "EUR"),
                        new XElement(ram + "SpecifiedTradeSettlementHeaderMonetarySummation",
                            new XElement(ram + "TaxBasisTotalAmount", invoice.Subtotal),
                            new XElement(ram + "TaxTotalAmount",
                                new XAttribute("currencyID", "EUR"),
                                invoice.Tax),
                            new XElement(ram + "GrandTotalAmount", invoice.Total),
                            new XElement(ram + "DuePayableAmount", invoice.Total)
                        )
                    )
                )
            )
        );

        // Save the XML to a temp file for embedding
        string xmlPath = $"zugferd-{invoice.InvoiceNumber}.xml";
        zugferdXml.Save(xmlPath);

        // Attach the XML to the PDF - filename must follow ZUGFeRD conventions
        byte[] xmlData = System.IO.File.ReadAllBytes(xmlPath);
        pdf.Attachments.AddAttachment("zugferd-invoice.xml", xmlData);

        // Final PDF contains both visual invoice and machine-readable XML
        pdf.SaveAs($"invoice-{invoice.InvoiceNumber}-zugferd.pdf");
    }

    // Generates simple HTML for the visual portion of the invoice
    private string BuildInvoiceHtml(Invoice invoice)
    {
        return $@"
<!DOCTYPE html>
<html>
<head>
    <style>
        body {{ font-family: Arial, sans-serif; padding: 40px; }}
        h1 {{ color: #333; }}
        .zugferd-notice {{ 
            margin-top: 30px; padding: 10px; 
            background: #f0f0f0; font-size: 11px; 
        }}
    </style>
</head>
<body>
    <h1>RECHNUNG / INVOICE</h1>
    <p><strong>Rechnungsnummer:</strong> {invoice.InvoiceNumber}</p>
    <p><strong>Datum:</strong> {invoice.InvoiceDate:dd.MM.yyyy}</p>
    <p><strong>Betrag:</strong> €{invoice.Total:F2}</p>
    
    <div class='zugferd-notice'>
        This invoice contains embedded ZUGFeRD data for automated processing.
    </div>
</body>
</html>";
    }
}

範例輸出

合規的關鍵在於使用正確的 XML 命名空間,遵循 CII 模式結構,並將 XML 嵌入到適當的檔案名稱中。 TypeCode「380」明確表明該文件是聯合國/CEFACT標準中的商業發票。

如何使發票符合歐盟法規要求

歐盟正在逐步強製成員國使用電子發票。 義大利已經要求企業對企業交易使用電子發票,法國正在分階段實施,將於 2026 年前完成,德國已宣布從 2025 年開始強制執行企業對企業電子發票。現在建立 ZUGFeRD/Factur-X 支援,即可使您的系統為這些監管要求做好準備。

以下是一個符合不同標準的合規性發票產生器的範本:

using IronPdf;
using System;

// Enum representing supported European e-invoicing standards
public enum InvoiceStandard
{
    None,
    ZUGFeRD,    // German standard - uses CII XML format
    FacturX,    // French standard - technically identical to ZUGFeRD 2.0
    Peppol      // Pan-European standard - uses UBL XML format
}

// Factory class that generates invoices compliant with different e-invoicing standards
// Allows switching between standards without changing core invoice generation logic
public class CompliantInvoiceGenerator
{
    public PdfDocument GenerateCompliantInvoice(Invoice invoice, InvoiceStandard standard)
    {
        // Generate the base PDF from HTML
        var renderer = new ChromePdfRenderer();
        string html = BuildInvoiceHtml(invoice);
        var pdf = renderer.RenderHtmlAsPdf(html);

        // Attach the appropriate XML format based on target market/regulation
        switch (standard)
        {
            case InvoiceStandard.ZUGFeRD:
            case InvoiceStandard.FacturX:
                // Both use Cross-Industry Invoice format, just different filenames
                EmbedCIIXmlData(pdf, invoice, standard);
                break;
            case InvoiceStandard.Peppol:
                // Peppol uses Universal Business Language format
                EmbedUBLXmlData(pdf, invoice);
                break;
        }

        return pdf;
    }

    // Creates and embeds CII-format XML (used by ZUGFeRD and Factur-X)
    private void EmbedCIIXmlData(PdfDocument pdf, Invoice invoice, InvoiceStandard standard)
    {
        string xml = GenerateCIIXml(invoice);

        // Filename convention differs between German and French standards
        string filename = standard == InvoiceStandard.ZUGFeRD
            ? "zugferd-invoice.xml"
            : "factur-x.xml";

        System.IO.File.WriteAllText("temp-invoice.xml", xml);
        byte[] xmlData = System.IO.File.ReadAllBytes("temp-invoice.xml");
        pdf.Attachments.AddAttachment(filename, xmlData);
    }

    // Creates and embeds UBL-format XML for Peppol network compliance
    private void EmbedUBLXmlData(PdfDocument pdf, Invoice invoice)
    {
        // UBL (Universal Business Language) is the Peppol standard format
        string xml = $@"<?xml version='1.0' encoding='UTF-8'?>
<Invoice xmlns='urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'>
    <ID>{invoice.InvoiceNumber}</ID>
    <IssueDate>{invoice.InvoiceDate:yyyy-MM-dd}</IssueDate>
    <DocumentCurrencyCode>EUR</DocumentCurrencyCode>
    <LegalMonetaryTotal>
        <PayableAmount currencyID='EUR'>{invoice.Total}</PayableAmount>
    </LegalMonetaryTotal>
</Invoice>";

        System.IO.File.WriteAllText("peppol-invoice.xml", xml);
        pdf.Attachments.AddFile("peppol-invoice.xml", "invoice.xml", "Peppol UBL Invoice");
    }

    // Generates minimal CII XML structure for demonstration
    private string GenerateCIIXml(Invoice invoice)
    {
        return $@"<?xml version='1.0' encoding='UTF-8'?>
<rsm:CrossIndustryInvoice
    xmlns:rsm='urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100'
    xmlns:ram='urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100'>
    <rsm:ExchangedDocument>
        <ram:ID>{invoice.InvoiceNumber}</ram:ID>
        <ram:TypeCode>380</ram:TypeCode>
    </rsm:ExchangedDocument>
</rsm:CrossIndustryInvoice>";
    }

    private string BuildInvoiceHtml(Invoice invoice)
    {
        return $"<html><body><h1>Invoice {invoice.InvoiceNumber}</h1></body></html>";
    }
}

這種架構允許您在新標準出現時新增這些標準,而無需重構核心發票產生邏輯。 基於枚舉的方法使得使用者或配置能夠輕鬆地確定要使用的合規模式。


我最喜歡的程式庫是IronPDF。它允許快速高效地操作PDF文件。它還有許多有價值的功能,例如導出到PDF/A格式和數位簽署PDF文件。

Milan Jovanovic

Microsoft MVP

查看案例研究

IronOCR意味著我們每年可以從手動處理中節省$40,000,同時提高生產力,釋放資源以進行高影響的任務。我會強烈推薦它。

Brent Matzelle

首席技術官,OPYN

查看案例研究

如何在 C# 中從 PDF 發票擷取資料

開立發票只是成功的一半。 大多數企業也會收到供應商的發票,需要提取資料進行處理。 IronPDF 提供強大的文字擷取功能,這是發票資料收集的基礎。

如何從PDF發票中提取文字

最基本的提取操作是從 PDF 中提取所有文字內容。 IronPDF 的 ExtractAllText 方法處理 PDF 文本編碼和定位的複雜性:

using IronPdf;
using System;

// Extracts raw text content from PDF invoices for further processing
public class InvoiceTextExtractor
{
    // Extracts all text from a PDF in one operation
    // Best for single-page invoices or when you need the complete content
    public string ExtractInvoiceText(string pdfPath)
    {
        var pdf = PdfDocument.FromFile(pdfPath);

        // IronPDF handles the complexity of PDF text encoding and positioning
        string allText = pdf.ExtractAllText();
        Console.WriteLine("Full invoice text:");
        Console.WriteLine(allText);

        return allText;
    }

    // Extracts text page by page - useful for multi-page invoices
    // Allows you to process header info separately from line items
    public void ExtractTextByPage(string pdfPath)
    {
        var pdf = PdfDocument.FromFile(pdfPath);

        // Iterate through each page (0-indexed)
        for (int i = 0; i < pdf.PageCount; i++)
        {
            string pageText = pdf.ExtractTextFromPage(i);
            Console.WriteLine($"\n--- Page {i + 1} ---");
            Console.WriteLine(pageText);
        }
    }
}

逐頁提取對於多頁發票特別有用,因為您需要查找特定部分,例如查找跨越多頁的行項目,而標題資訊僅出現在第一頁上。

如何提取表格中的行項目資料

發票明細通常以表格呈現。 PDF 檔案本身沒有表格結構,但您可以提取文字並進行解析,從而重建表格資料:

using IronPdf;
using System;
using System.Collections.Generic;

// Data model for a single invoice line item
public class InvoiceLineItem
{
    public string Description { get; set; }
    public decimal Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal Total { get; set; }
}

// Extracts tabular line item data from PDF invoices
// Note: PDFs don't have native table structure, so this uses text parsing
public class InvoiceTableExtractor
{
    public List<InvoiceLineItem> ExtractLineItems(string pdfPath)
    {
        var pdf = PdfDocument.FromFile(pdfPath);
        string text = pdf.ExtractAllText();

        var lineItems = new List<InvoiceLineItem>();
        string[] lines = text.Split('\n');

        foreach (string line in lines)
        {
            // Currency symbols indicate potential line items with amounts
            if (line.Contains("$") || line.Contains("€"))
            {
                Console.WriteLine($"Potential line item: {line.Trim()}");

                // Split on whitespace to separate columns
                // Actual parsing logic depends on your invoice format
                string[] parts = line.Split(new[] { '\t', ' ' },
                    StringSplitOptions.RemoveEmptyEntries);

                // Try to find numeric values that could be amounts
                foreach (string part in parts)
                {
                    string cleaned = part.Replace("$", "").Replace("€", "").Replace(",", "");
                    if (decimal.TryParse(cleaned, out decimal amount))
                    {
                        Console.WriteLine($"  Found amount: {amount:C}");
                    }
                }
            }
        }

        return lineItems;
    }
}

解析邏輯會根據您的發票格式而有所不同。 對於來自已知供應商且格式一致的發票,您可以建立特定格式的解析器。 對於各種格式,請考慮本文後面將介紹的 AI 驅動擷取。

如何使用模式匹配來匹配發票編號、日期和總金額

正規表示式對於從發票文字中提取特定資料點非常有價值。 發票編號、日期和總額等關鍵欄位通常遵循可識別的模式:

using IronPdf;
using System;
using System.Text.RegularExpressions;

// Data model for extracted invoice information
public class InvoiceData
{
    public string InvoiceNumber { get; set; }
    public string InvoiceDate { get; set; }
    public decimal TotalAmount { get; set; }
    public string VendorName { get; set; }
}

// Extracts key invoice fields using regex pattern matching
// Multiple patterns handle variations across different vendors
public class InvoiceParser
{
    public InvoiceData ParseInvoice(string pdfPath)
    {
        var pdf = PdfDocument.FromFile(pdfPath);
        string text = pdf.ExtractAllText();

        var invoiceData = new InvoiceData();

        // Try multiple patterns to find invoice number
        // Handles: "Invoice #123", "INV-123", "Invoice Number: 123", German "Rechnungsnummer"
        string[] invoiceNumberPatterns = new[]
        {
            @"Invoice\s*#?\s*:?\s*([A-Z0-9-]+)",
            @"INV[-\s]?(\d+)",
            @"Invoice\s+Number\s*:?\s*([A-Z0-9-]+)",
            @"Rechnungsnummer\s*:?\s*([A-Z0-9-]+)"
        };

        foreach (string pattern in invoiceNumberPatterns)
        {
            var match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
            if (match.Success)
            {
                invoiceData.InvoiceNumber = match.Groups[1].Value;
                Console.WriteLine($"Found Invoice Number: {invoiceData.InvoiceNumber}");
                break;
            }
        }

        // Date patterns for US, European, and written formats
        string[] datePatterns = new[]
        {
            @"Date\s*:?\s*(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})",
            @"Invoice\s+Date\s*:?\s*(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})",
            @"(\d{1,2}\.\d{1,2}\.\d{4})",  // European: DD.MM.YYYY
            @"(\w+\s+\d{1,2},?\s+\d{4})"   // Written: January 15, 2024
        };

        foreach (string pattern in datePatterns)
        {
            var match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
            if (match.Success)
            {
                invoiceData.InvoiceDate = match.Groups[1].Value;
                Console.WriteLine($"Found Date: {invoiceData.InvoiceDate}");
                break;
            }
        }

        // Look for total amount with various labels
        string[] totalPatterns = new[]
        {
            @"Total\s*:?\s*[\$€]?\s*([\d,]+\.\d{2})",
            @"Amount\s+Due\s*:?\s*[\$€]?\s*([\d,]+\.\d{2})",
            @"Grand\s+Total\s*:?\s*[\$€]?\s*([\d,]+\.\d{2})",
            @"Balance\s+Due\s*:?\s*[\$€]?\s*([\d,]+\.\d{2})"
        };

        foreach (string pattern in totalPatterns)
        {
            var match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
            if (match.Success)
            {
                // Remove commas before parsing
                string amountStr = match.Groups[1].Value.Replace(",", "");
                if (decimal.TryParse(amountStr, out decimal amount))
                {
                    invoiceData.TotalAmount = amount;
                    Console.WriteLine($"Found Total: ${invoiceData.TotalAmount:F2}");
                    break;
                }
            }
        }

        return invoiceData;
    }
}

這種基於模式的方法對於格式可預測的發票非常有效。 多種模式變化處理不同供應商之間常見的格式差異,例如"Invoice #" 和 "Invoice Number:"

掃描件或影像發票呢?

上述文字擷取方法適用於包含嵌入式文字的 PDF 檔案。 但是,掃描文件和基於圖像的 PDF 文件不包含可提取的文字。 它們本質上是發票的照片。

請注意: 處理掃描的發票時,您需要 OCR(光學字符識別)功能。 IronOCR 是 Iron Suite 的一部分,與 IronPDF 在這些場景中整合。 訪問 https://ironsoftware.com/csharp/ocr/ 以了解從掃描文檔和圖像中提取文字的更多信息。

如何在 .NET 中使用 AI 處理發票

傳統模式匹配對標準化發票效果很好,但現實世界中的應付帳款部門會收到無數種格式的文件。 這就是人工智慧資料擷取的優勢所在。 大型語言模型能夠理解發票語義,即使面對不熟悉的佈局也能提取結構化資料。

如何將人工智慧整合到發票解析中

AI驅動的發票處理模式結合了IronPDF的文字擷取和LLM API呼叫。 以下是一個適用於任何 OpenAI 相容 API 的通用實作:

using IronPdf;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

// Data model for extracted invoice information
public class InvoiceData
{
    public string InvoiceNumber { get; set; }
    public string InvoiceDate { get; set; }
    public string VendorName { get; set; }
    public decimal TotalAmount { get; set; }
}

// Leverages AI/LLM APIs to extract structured data from any invoice format
// Works with OpenAI or any compatible API endpoint
public class AIInvoiceParser
{
    private readonly HttpClient _httpClient;
    private readonly string _apiKey;
    private readonly string _apiUrl;

    public AIInvoiceParser(string apiKey, string apiUrl = "https://api.openai.com/v1/chat/completions")
    {
        _apiKey = apiKey;
        _apiUrl = apiUrl;
        _httpClient = new HttpClient();
        _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}");
    }

    public async Task<InvoiceData> ParseInvoiceWithAI(string pdfPath)
    {
        // First extract raw text from the PDF using IronPDF
        var pdf = PdfDocument.FromFile(pdfPath);
        string invoiceText = pdf.ExtractAllText();

        // Construct a prompt that instructs the AI to return structured JSON
        // Being explicit about the format reduces parsing errors
        string prompt = $@"Extract the following information from this invoice text.
Return ONLY valid JSON with no additional text or markdown formatting.

Required fields:
- InvoiceNumber: The invoice or document number
- InvoiceDate: The invoice date in YYYY-MM-DD format
- VendorName: The company or person who sent the invoice
- TotalAmount: The total amount due as a number (no currency symbols)

Invoice text:
{invoiceText}

JSON response:";

        // Build the API request with a system prompt for context
        var requestBody = new
        {
            model = "gpt-4",
            messages = new[]
            {
                new {
                    role = "system",
                    content = "You are an invoice data extraction assistant. Extract structured data from invoices and return valid JSON only."
                },
                new { role = "user", content = prompt }
            },
            temperature = 0.1  // Low temperature ensures consistent, deterministic results
        };

        var json = JsonSerializer.Serialize(requestBody);
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        var response = await _httpClient.PostAsync(_apiUrl, content);
        var responseJson = await response.Content.ReadAsStringAsync();

        // Navigate the API response structure to get the extracted content
        using var doc = JsonDocument.Parse(responseJson);
        var messageContent = doc.RootElement
            .GetProperty("choices")[0]
            .GetProperty("message")
            .GetProperty("content")
            .GetString();

        Console.WriteLine("AI Extracted Data:");
        Console.WriteLine(messageContent);

        // Deserialize the AI's JSON response into our data class
        var invoiceData = JsonSerializer.Deserialize<InvoiceData>(messageContent,
            new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

        return invoiceData;
    }
}

低溫設定 (0.1) 有利於獲得確定性輸出,這對於希望對相同輸入獲得一致結果的資料擷取任務來說非常重要。

如何從發票中提取結構化 JSON 資料

對於包含明細、供應商詳細資訊和客戶資訊的更複雜的發票,您可以要求更豐富的 JSON 結構:

using IronPdf;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;

// Comprehensive invoice data model with all details
public class DetailedInvoiceData
{
    public string InvoiceNumber { get; set; }
    public DateTime InvoiceDate { get; set; }
    public DateTime DueDate { get; set; }
    public VendorInfo Vendor { get; set; }
    public CustomerInfo Customer { get; set; }
    public List<LineItem> LineItems { get; set; }
    public decimal Subtotal { get; set; }
    public decimal Tax { get; set; }
    public decimal Total { get; set; }
}

public class VendorInfo
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string TaxId { get; set; }
}

public class CustomerInfo
{
    public string Name { get; set; }
    public string Address { get; set; }
}

public class LineItem
{
    public string Description { get; set; }
    public decimal Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal Total { get; set; }
}

// Extracts comprehensive invoice data including line items and party details
public class StructuredInvoiceExtractor
{
    private readonly AIInvoiceParser _aiParser;

    public StructuredInvoiceExtractor(string apiKey)
    {
        _aiParser = new AIInvoiceParser(apiKey);
    }

    public async Task<DetailedInvoiceData> ExtractDetailedData(string pdfPath)
    {
        var pdf = PdfDocument.FromFile(pdfPath);
        string text = pdf.ExtractAllText();

        // Define the exact JSON structure we want the AI to return
        // This schema guides the AI to extract all relevant fields
        string jsonSchema = @"{
  ""InvoiceNumber"": ""string"",
  ""InvoiceDate"": ""YYYY-MM-DD"",
  ""DueDate"": ""YYYY-MM-DD"",
  ""Vendor"": {
    ""Name"": ""string"",
    ""Address"": ""string"",
    ""TaxId"": ""string or null""
  },
  ""Customer"": {
    ""Name"": ""string"",
    ""Address"": ""string""
  },
  ""LineItems"": [
    {
      ""Description"": ""string"",
      ""Quantity"": 0.0,
      ""UnitPrice"": 0.00,
      ""Total"": 0.00
    }
  ],
  ""Subtotal"": 0.00,
  ""Tax"": 0.00,
  ""Total"": 0.00
}";

        // Prompt includes both the schema and the extracted text
        string prompt = $@"Extract all invoice data and return it in this exact JSON structure:
{jsonSchema}

Invoice text:
{text}

Return only valid JSON, no markdown formatting or additional text.";

        // Call AI API and parse response (implementation as shown above)
        // Return deserialized DetailedInvoiceData

        return new DetailedInvoiceData(); // Placeholder
    }
}

如何處理格式不一致的發票

人工智慧資料擷取的真正威力體現在處理來自多個供應商、格式各異的發票時。 智慧處理器可以先嘗試基於模式的提取(速度更快、成本更低),僅在必要時才回退到人工智慧:

using IronPdf;
using System.Threading.Tasks;

// Hybrid processor that optimizes for cost and capability
// Tries fast regex patterns first, uses AI only when patterns fail
public class SmartInvoiceProcessor
{
    private readonly AIInvoiceParser _aiParser;

    public SmartInvoiceProcessor(string aiApiKey)
    {
        _aiParser = new AIInvoiceParser(aiApiKey);
    }

    public async Task<InvoiceData> ProcessAnyInvoice(string pdfPath)
    {
        var pdf = PdfDocument.FromFile(pdfPath);
        string text = pdf.ExtractAllText();

        // First attempt: regex patterns (fast and free)
        var patternParser = new InvoiceParser();
        var standardResult = patternParser.ParseInvoiceFromText(text);

        // If pattern matching found all required fields, use that result
        if (IsComplete(standardResult))
        {
            Console.WriteLine("Pattern extraction successful");
            return standardResult;
        }

        // Fallback: use AI for complex or unusual invoice formats
        // This costs money but handles any layout
        Console.WriteLine("Using AI extraction for complex invoice format");
        var aiResult = await _aiParser.ParseInvoiceWithAI(pdfPath);

        return aiResult;
    }

    // Validates that we have the minimum required fields
    private bool IsComplete(InvoiceData data)
    {
        return !string.IsNullOrEmpty(data.InvoiceNumber) &&
               !string.IsNullOrEmpty(data.InvoiceDate) &&
               data.TotalAmount > 0;
    }
}

如何建立應付帳款自動化流程

將所有這些部分整合在一起,就形成了一個完整的自動化流程,它可以處理收到的發票、提取資料、驗證資料並將其準備好供您的會計系統使用:

using IronPdf;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;

// Tracks the outcome of processing each invoice
public class ProcessingResult
{
    public string FileName { get; set; }
    public bool Success { get; set; }
    public string InvoiceNumber { get; set; }
    public string ErrorMessage { get; set; }
}

// Complete automation pipeline for accounts payable
// Watches a folder, extracts data, validates, and routes to accounting system
public class InvoiceAutomationPipeline
{
    private readonly SmartInvoiceProcessor _processor;
    private readonly string _inputFolder;
    private readonly string _processedFolder;
    private readonly string _errorFolder;

    public InvoiceAutomationPipeline(string apiKey, string inputFolder)
    {
        _processor = new SmartInvoiceProcessor(apiKey);
        _inputFolder = inputFolder;
        _processedFolder = Path.Combine(inputFolder, "processed");
        _errorFolder = Path.Combine(inputFolder, "errors");

        // Create output directories if they don't exist
        Directory.CreateDirectory(_processedFolder);
        Directory.CreateDirectory(_errorFolder);
    }

    // Main entry point - processes all PDFs in the input folder
    public async Task<List<ProcessingResult>> ProcessInvoiceBatch()
    {
        string[] invoiceFiles = Directory.GetFiles(_inputFolder, "*.pdf");
        Console.WriteLine($"Found {invoiceFiles.Length} invoices to process");

        var results = new List<ProcessingResult>();

        foreach (string invoicePath in invoiceFiles)
        {
            string fileName = Path.GetFileName(invoicePath);

            try
            {
                Console.WriteLine($"Processing: {fileName}");

                // Extract data using smart processor (patterns first, then AI)
                var invoiceData = await _processor.ProcessAnyInvoice(invoicePath);

                // Ensure we have minimum required fields before proceeding
                if (ValidateInvoiceData(invoiceData))
                {
                    // Send to accounting system (QuickBooks, Xero, etc.)
                    await SaveToAccountingSystem(invoiceData);

                    // Archive successful invoices
                    string destPath = Path.Combine(_processedFolder, fileName);
                    File.Move(invoicePath, destPath, overwrite: true);

                    results.Add(new ProcessingResult
                    {
                        FileName = fileName,
                        Success = true,
                        InvoiceNumber = invoiceData.InvoiceNumber
                    });

                    Console.WriteLine($"✓ Processed: {invoiceData.InvoiceNumber}");
                }
                else
                {
                    throw new Exception("Validation failed - missing required fields");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"✗ Failed: {fileName} - {ex.Message}");

                // Quarantine failed invoices for manual review
                string destPath = Path.Combine(_errorFolder, fileName);
                File.Move(invoicePath, destPath, overwrite: true);

                results.Add(new ProcessingResult
                {
                    FileName = fileName,
                    Success = false,
                    ErrorMessage = ex.Message
                });
            }
        }

        GenerateReport(results);
        return results;
    }

    // Checks for minimum required fields
    private bool ValidateInvoiceData(InvoiceData data)
    {
        return !string.IsNullOrEmpty(data.InvoiceNumber) &&
               !string.IsNullOrEmpty(data.VendorName) &&
               data.TotalAmount > 0;
    }

    // Placeholder for accounting system integration
    private async Task SaveToAccountingSystem(InvoiceData data)
    {
        // Integrate with your accounting system here
        // Examples: QuickBooks API, Xero API, SAP, or database storage
        Console.WriteLine($"  Saved invoice {data.InvoiceNumber} to accounting system");
        await Task.CompletedTask;
    }

    // Outputs a summary of the batch processing results
    private void GenerateReport(List<ProcessingResult> results)
    {
        int successful = results.Count(r => r.Success);
        int failed = results.Count(r => !r.Success);

        Console.WriteLine($"\n========== Processing Complete ==========");
        Console.WriteLine($"Total Processed: {results.Count}");
        Console.WriteLine($"Successful: {successful}");
        Console.WriteLine($"Failed: {failed}");

        if (failed > 0)
        {
            Console.WriteLine("\nFailed invoices requiring review:");
            foreach (var failure in results.Where(r => !r.Success))
            {
                Console.WriteLine($"  • {failure.FileName}: {failure.ErrorMessage}");
            }
        }
    }
}

該管道實現了一個完整的工作流程:它掃描資料夾中的傳入 PDF 文件,處理每個文件,驗證提取的資料,將成功提取的文件路由到您的會計系統,並將失敗的文件隔離以供人工審核。 總結報告可以清楚地顯示處理結果。


如何將 C# 發票處理與會計系統整合

提取的發票資料最終需要匯入會計系統進行支付和記錄保存。 具體細節因平台而異,但整合模式是一致的。

QuickBooks、Xero 和 SAP 的常見整合模式有哪些?

大多數會計平台都提供 REST API,用於以程式設計方式建立帳單或發票。 以下是一個通用模式,您可以根據自己的平台進行調整:

using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

// Generic integration layer for pushing invoice data to accounting systems
// Adapt the API calls based on your specific platform
public class AccountingSystemIntegration
{
    private readonly HttpClient _httpClient;
    private readonly string _apiKey;
    private readonly string _baseUrl;

    public AccountingSystemIntegration(string apiKey, string baseUrl)
    {
        _apiKey = apiKey;
        _baseUrl = baseUrl;
        _httpClient = new HttpClient();
        _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}");
    }

    // Creates a Bill in QuickBooks (vendor invoices are called "Bills")
    public async Task SendToQuickBooks(InvoiceData invoice)
    {
        // QuickBooks Bill structure - see their API docs for full schema
        var bill = new
        {
            VendorRef = new { name = invoice.VendorName },
            TxnDate = invoice.InvoiceDate,
            DocNumber = invoice.InvoiceNumber,
            TotalAmt = invoice.TotalAmount,
            Line = new[]
            {
                new
                {
                    Amount = invoice.TotalAmount,
                    DetailType = "AccountBasedExpenseLineDetail",
                    AccountBasedExpenseLineDetail = new
                    {
                        AccountRef = new { name = "Accounts Payable" }
                    }
                }
            }
        };

        await PostToApi("/v3/company/{companyId}/bill", bill);
    }

    // Creates an accounts payable invoice in Xero
    public async Task SendToXero(InvoiceData invoice)
    {
        // ACCPAY type indicates this is a bill to pay (not a sales invoice)
        var bill = new
        {
            Type = "ACCPAY",
            Contact = new { Name = invoice.VendorName },
            Date = invoice.InvoiceDate,
            InvoiceNumber = invoice.InvoiceNumber,
            Total = invoice.TotalAmount
        };

        await PostToApi("/api.xro/2.0/Invoices", bill);
    }

    // Generic POST helper with error handling
    private async Task PostToApi(string endpoint, object payload)
    {
        string json = JsonSerializer.Serialize(payload);
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        var response = await _httpClient.PostAsync($"{_baseUrl}{endpoint}", content);

        if (!response.IsSuccessStatusCode)
        {
            string error = await response.Content.ReadAsStringAsync();
            throw new Exception($"API Error: {response.StatusCode} - {error}");
        }

        Console.WriteLine($"Successfully posted to {endpoint}");
    }
}

每個平台都有自己的驗證機制(QuickBooks 和 Xero 使用 OAuth,SAP 使用各種方法)、必填欄位和 API 約定。 有關具體細節,請參閱目標平台的文檔,但將提取的發票資料轉換為 API 有效負載的模式保持不變。

如何大量處理數百張發票

大批量發票處理需要格外注意並發性和資源管理。 以下是使用可控並發的平行處理模式:

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

// Tracks the result of processing a single invoice in a batch
public class BatchResult
{
    public string FilePath { get; set; }
    public bool Success { get; set; }
    public string InvoiceNumber { get; set; }
    public string Error { get; set; }
}

// High-volume invoice processor with controlled parallelism
// Prevents overwhelming APIs while maximizing throughput
public class BatchInvoiceProcessor
{
    private readonly SmartInvoiceProcessor _invoiceProcessor;
    private readonly AccountingSystemIntegration _accountingIntegration;
    private readonly int _maxConcurrency;

    public BatchInvoiceProcessor(string aiApiKey, string accountingApiKey,
        string accountingUrl, int maxConcurrency = 5)
    {
        _invoiceProcessor = new SmartInvoiceProcessor(aiApiKey);
        _accountingIntegration = new AccountingSystemIntegration(accountingApiKey, accountingUrl);
        _maxConcurrency = maxConcurrency;  // Adjust based on API rate limits
    }

    // Processes multiple invoices in parallel with controlled concurrency
    public async Task<List<BatchResult>> ProcessInvoiceBatch(List<string> invoicePaths)
    {
        // Thread-safe collection for gathering results from parallel tasks
        var results = new ConcurrentBag<BatchResult>();

        // Semaphore limits how many invoices process simultaneously
        var semaphore = new SemaphoreSlim(_maxConcurrency);

        // Create a task for each invoice
        var tasks = invoicePaths.Select(async path =>
        {
            // Wait for a slot to become available
            await semaphore.WaitAsync();
            try
            {
                var result = await ProcessSingleInvoice(path);
                results.Add(result);
            }
            finally
            {
                // Release slot for next invoice
                semaphore.Release();
            }
        });

        // Wait for all invoices to complete
        await Task.WhenAll(tasks);

        // Output summary statistics
        var resultList = results.ToList();
        int successful = resultList.Count(r => r.Success);
        int failed = resultList.Count(r => !r.Success);

        Console.WriteLine($"\nBatch Processing Complete:");
        Console.WriteLine($"  Total: {resultList.Count}");
        Console.WriteLine($"  Successful: {successful}");
        Console.WriteLine($"  Failed: {failed}");

        return resultList;
    }

    // Processes one invoice: extract data and send to accounting system
    private async Task<BatchResult> ProcessSingleInvoice(string pdfPath)
    {
        try
        {
            Console.WriteLine($"Processing: {pdfPath}");

            var invoiceData = await _invoiceProcessor.ProcessAnyInvoice(pdfPath);
            await _accountingIntegration.SendToQuickBooks(invoiceData);

            Console.WriteLine($"✓ Completed: {invoiceData.InvoiceNumber}");

            return new BatchResult
            {
                FilePath = pdfPath,
                Success = true,
                InvoiceNumber = invoiceData.InvoiceNumber
            };
        }
        catch (Exception ex)
        {
            Console.WriteLine($"✗ Failed: {pdfPath}");

            return new BatchResult
            {
                FilePath = pdfPath,
                Success = false,
                Error = ex.Message
            };
        }
    }
}

SemaphoreSlim 確保您不會壓垮外部 API 或耗盡系統資源。 根據您的 API 速率限制和伺服器容量調整 _maxConcurrency。 ConcurrentBag 安全地從並行操作中收集結果。


後續步驟

發票自動化為減少人工操作、最大限度地減少錯誤和加快業務流程提供了重要機會。 本指南已帶您走過完整的生命周期:從HTML 模板生成專業發票,符合ZUGFeRD 和 Factur-X 電子發票標準,使用模式匹配和AI 驅動的處理從收到的發票中提取數據,以及建立可擴展的自動化管道。

IronPDF 作為這些功能的基礎,提供可靠的 HTML 到 PDF 渲染、可靠的文字提取,以及 PDF/A-3 電子發票合規所需的附件功能。 其基於 Chrome 的渲染引擎確保您的發票看起來與設計完全一致,而其提取方法可自動處理 PDF 文字編碼的複雜性。

這裡展示的模式只是起點。 實際應用程式中需要根據您特定的發票格式、會計系統和業務規則進行調整。 對於高容量的情境,批次處理教學涵蓋了具有受控並發性和錯誤恢復的平行執行。

準備開始建造了嗎? 下載 IronPDF 並試用免費試用版。 該程式庫包含免費開發授權,讓您可以在承諾生產授權之前充分評估發票生成、數據提取和PDF 報告功能。 如果您對發票自動化或會計系統整合有疑問,請聯繫我們的工程支援團隊。

常見問題

什麼是使用IronPDF在C# .NET中的發票處理?

在C# .NET中使用IronPDF進行發票處理涉及從HTML模板生成專業PDF發票,確保符合ZUGFeRD和Factur-X電子發票標準,使用AI驅動的處理提取結構化數據,以及創建與QuickBooks等會計系統整合的自動化管道。

IronPDF如何幫助生成PDF發票?

IronPDF通過將HTML模板轉換為高品質PDF文件來幫助生成PDF發票。這使得可以使用標準的Web技術,如HTML和CSS來設計發票,而IronPDF會精確呈現這些設計。

IronPDF能否在PDF發票中嵌入XML數據以符合電子發票規範?

是的,IronPDF可以在PDF發票中嵌入XML數據,以符合如ZUGFeRD和Factur-X的電子發票標準。這涉及在PDF/A-3格式中附加結構化XML數據,以便符合歐洲電子發票的要求。

如何使用IronPDF從PDF發票中提取文本?

您可以使用IronPDF的ExtractAllText方法從PDF發票中提取文本,該方法處理PDF文本提取的複雜性,並提供處理發票所需的文本數據。

IronPDF是否支持AI驅動的發票數據提取?

是的,IronPDF通過與AI和LLM API接口來支持AI驅動的發票數據提取。這種方法用於從不同的發票格式中提取結構化數據,使用IronPDF進行初步文本提取,並使用AI進行數據解析。

使用IronPDF的會計系統整合模式有哪些?

使用IronPDF的會計系統常見整合模式包括使用REST API在QuickBooks、Xero和SAP等平台上以程式方式創建帳單或發票,將提取的發票數據轉換為這些平台的JSON負載。

IronPDF能否處理批量發票處理?

是的,IronPDF能有效地處理批量發票處理。這涉及使用平行處理模式和控制並發性來管理資源,同時批量轉換多個發票。

使用PDF格式作為發票的好處是什麼?

PDF格式在不同設備上保留了發票格式,防止未經授權的更改,支持電子簽名和加密,並被商業系統廣泛認可,這使其成為專業發票的理想選擇。

IronPDF如何協助歐洲的電子發票合規?

IronPDF通過在PDF中嵌入ZUGFeRD和Factur-X等標準的XML數據來協助歐洲的電子發票合規,這在滿足歐盟電子發票要求中是必要的。

在.NET中發票處理的推薦技術堆疊是什麼?

在.NET中發票處理的推薦技術堆疊包括IronPDF用於PDF操作,IronQR用於嵌入QR碼,以及與AI模型整合進行高級數據解析。

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 22,321,142版本:2026.11剛剛發布