亚洲乱色熟女一区二区三区丝袜,天堂√中文最新版在线,亚洲精品乱码久久久久久蜜桃图片,香蕉久久久久久av成人,欧美丰满熟妇bbb久久久

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

用C#提取PDF文件中的表格并導(dǎo)出為HTML格式頁(yè)面

admin
2025年8月4日 13:18 本文熱度 1399

在C#中提取PDF表格并轉(zhuǎn)換為HTML,推薦使用開源庫(kù)iTextSharp(針對(duì)結(jié)構(gòu)化PDF)和PdfPig(針對(duì)復(fù)雜布局)。以下是兩種實(shí)現(xiàn)方案:

方案1:使用iTextSharp(推薦格式良好的PDF)

// 安裝NuGet包:Install-Package iTextSharp

using iTextSharp.text.pdf;

using iTextSharp.text.pdf.parser;

using System.Text;

using System.IO;


public class PdfTableExtractor

{

    public static string ExtractTableToHtml(string pdfPath)

    {

        var html = new StringBuilder();

        html.AppendLine("<table border='1'>");


        using (var reader = new PdfReader(pdfPath))

        {

            for (int page = 1; page <= reader.NumberOfPages; page++)

            {

                var strategy = new SimpleTextExtractionStrategy();

                string text = PdfTextExtractor.GetTextFromPage(reader, page, strategy);


                // 簡(jiǎn)單示例:按行分割(實(shí)際需更復(fù)雜解析)

                foreach (var line in text.Split('\n'))

                {

                    if (line.Contains("|")) // 假設(shè)表格使用豎線分隔

                    {

                        html.AppendLine("<tr>");

                        foreach (var cell in line.Split('|'))

                        {

                            html.AppendLine($"<td>{cell.Trim()}</td>");

                        }

                        html.AppendLine("</tr>");

                    }

                }

            }

        }


        html.AppendLine("</table>");

        return html.ToString();

    }

}


// 使用示例

string htmlTable = PdfTableExtractor.ExtractTableToHtml("input.pdf");

File.WriteAllText("output.html", htmlTable);

注意:此方案僅適用于結(jié)構(gòu)簡(jiǎn)單的PDF表格。復(fù)雜表格需使用LocationTextExtractionStrategy解析坐標(biāo)。

方案2:使用PdfPig(處理復(fù)雜布局)

// 安裝NuGet包:Install-Package PdfPig

using UglyToad.PdfPig;

using System.Text;

using System.Collections.Generic;


public class PdfPigTableExtractor

{

    public static string ExtractTables(string pdfPath)

    {

        var html = new StringBuilder();

        html.AppendLine("<table border='1' style='border-collapse:collapse'>");


        using (var document = PdfDocument.Open(pdfPath))

        {

            foreach (var page in document.GetPages())

            {

                var words = page.GetWords();

                var lines = GroupWordsIntoLines(words);


                foreach (var line in lines)

                {

                    html.AppendLine("<tr>");

                    foreach (var word in line)

                    {

                        html.Append($"<td>{word.Text}</td>");

                    }

                    html.AppendLine("</tr>");

                }

            }

        }


        html.AppendLine("</table>");

        return html.ToString();

    }


    private static List<List<Word>> GroupWordsIntoLines(IEnumerable<Word> words)

    {

        // 按Y坐標(biāo)分組(同一行)

        var grouped = new Dictionary<double, List<Word>>();

        foreach (var word in words)

        {

            var y = Math.Round(word.BoundingBox.Bottom, 1);

            if (!grouped.ContainsKey(y)) grouped[y] = new List<Word>();

            grouped[y].Add(word);

        }


        // 按行排序(從上到下)

        return grouped.OrderByDescending(g => g.Key)

                      .Select(g => g.Value.OrderBy(w => w.BoundingBox.Left).ToList())

                      .ToList();

    }

}


// 使用示例

string html = PdfPigTableExtractor.ExtractTables("input.pdf");

File.WriteAllText("table.html", html);

處理復(fù)雜表格的改進(jìn)建議

  1. 列檢測(cè):統(tǒng)計(jì)單詞X坐標(biāo)聚類,自動(dòng)識(shí)別列邊界

  2. 單元格合并:通過空白區(qū)域檢測(cè)跨行/跨列單元格

  3. 使用專用庫(kù)

  • Tabula(通過C#調(diào)用Java)

  • Pdf2Data(高級(jí)表格提?。?/p>

示例HTML輸出結(jié)構(gòu)

<table border='1'>

  <tr>

    <td>Header 1</td>

    <td>Header 2</td>

  </tr>

  <tr>

    <td>Row 1 Cell 1</td>

    <td>Row 1 Cell 2</td>

  </tr>

</table>

提示:對(duì)于生產(chǎn)環(huán)境,建議結(jié)合PDF解析庫(kù)(提取原始數(shù)據(jù))+ HTML生成庫(kù)(如HtmlAgilityPack)構(gòu)建更健壯的解決方案。復(fù)雜PDF可能需要OCR預(yù)處理。


該文章在 2025/8/4 14:42:03 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved