摘要:[習題]ASP.NET 讀取 PDF檔案、轉成 TXT文字檔
[習題]ASP.NET 讀取 PDF檔案、轉成 TXT文字檔
找了一下 Google,發現滿多人推薦 PDFBox(請由此下載 http://sourceforge.net/projects/pdfbox/files/)
它原本是 Java PDF Library,但也提供了 .NET可參考的 DLL檔,對於中文的支援也加入了。
======================================================
本範例 資料來源: http://blog.csdn.net/yezi2413/archive/2008/10/23/3132074.aspx
特此感謝。
======================================================
首先,下載 PDFBox。
解壓縮之後,裡面有一個 \bin子目錄,就有 .NET可用的 DLL檔。
並且在 VS 2008裡面「加入參考」
只要加入這兩個,其他的都會自動添加。
IKVM.GNU.Classpath.dll
PDFBox-0.7.3.dll
別忘了,在程式裡面,自己加入這兩個 NameSpace喔!
using org.pdfbox.pdmodel;
using org.pdfbox.util;
using System;02

03
using System.Configuration;04

05
using System.Data;06

07
using System.Linq;08

09
using System.Web;10

11
using System.Web.Security;12

13
using System.Web.UI;14

15
using System.Web.UI.HtmlControls;16

17
using System.Web.UI.WebControls;18

19
using System.Web.UI.WebControls.WebParts;20

21
using System.Xml.Linq;22

23
24

25
//======================= 26

27
//== 本範例 資料來源: http://blog.csdn.net/yezi2413/archive/2008/10/23/3132074.aspx 28

29
using System.IO;30

31
using System.Text;32

33
34

35
using org.pdfbox.pdmodel; //-- 由此下載 http://sourceforge.net/projects/pdfbox/files/ 36

37
using org.pdfbox.util; 38

39
//======================= 40

41
42

43
public partial class _Default : System.Web.UI.Page 44

45
{46

47
protected void Page_Load(object sender, EventArgs e)48
{49

50
FileInfo pdffile = new FileInfo("c:\\mis2000lab_example.pdf");51

52
//-- http://msdn.microsoft.com/zh-tw/library/system.io.fileinfo.aspx 53
// 以下所列都是可以接受的路徑: 54
// C# 中的 "c:\\MyDir\\MyFile.txt",或 Visual Basic 中的 "c:\MyDir\MyFile.txt"。 55
// C# 中的 "c:\\MyDir",或 Visual Basic 中的 "c:\MyDir"。 56
// C# 中的 "MyDir\\MySubdir",或 Visual Basic 中的 "MyDir\MySubDir"。 57
// C# 中的 "\\\\MyServer\\MyShare",或 Visual Basic 中的 "\\MyServer\MyShare"。 58
59

60
if (pdffile.Exists)61
{62
FileInfo file = new FileInfo("c:\\mis2000lab_example.txt");63
pdf2txt(pdffile, file);64
}65
else66
{67
Response.Write("The File is NOT Exist.");68
}69

70
}71

72

73
public void pdf2txt(FileInfo file, FileInfo txtfile)74
{75
PDDocument doc = PDDocument.load(file.FullName);76

77
PDFTextStripper pdfStripper = new PDFTextStripper();78

79
string text = pdfStripper.getText(doc);80

81
StreamWriter swPdfChange = new StreamWriter(txtfile.FullName, false, Encoding.GetEncoding(65001));82

83
swPdfChange.Write(text);84
swPdfChange.Close();85
}86

87
}
Line 63 ---- 原作的範例,在pdf2txt(file, pdffile); 這個地方稍有問題,我修改為 pdf2txt(pdffile, file);
Line 81 ---- 程式中的編碼65001就是 utf-8,您也可以寫成
StreamWriter swPdfChange = new StreamWriter(txtfile.FullName, false, Encoding.GetEncoding("utf-8"));
============================================================================
原本第63行 pdf2txt(pdffile, file);,若改成以下的程式碼,
讀取出來會變成亂碼。
有人建議「還是乖乖轉成 txt文字檔,可以剃除掉 PDF檔案裡面包含的圖片」
上面的程式 第63行 pdf2txt(pdffile, file);,就是將 PDF轉成 txt文字檔
就一切ok了。
//-- StreamReader 02
//-- http://msdn.microsoft.com/zh-tw/library/system.io.streamreader.aspx 03

04
//-- StreamReader 05
//-- http://msdn.microsoft.com/zh-tw/library/system.io.streamreader.aspx 06

07
using (StreamReader sr = new StreamReader(pdffile.FullName, Encoding.GetEncoding(65000))) 08

{09
string line = null;10

11
while ((line = sr.ReadLine()) != null)12
{13
Label1.Text = Label1.Text + line;14
}15

16
}
======================================================
本範例 資料來源: http://blog.csdn.net/yezi2413/archive/2008/10/23/3132074.aspx
特此感謝。
======================================================
另外一個範例,請參閱
[微軟範例] iTextSharp.dll 將 GridView匯出 doc/access/csv/Excel/pdf/xml/html/text/print
我將思想傳授他人, 他人之所得,亦無損於我之所有;
猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson
線上課程教學,遠距教學 (Web Form 約 51hr) https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015
線上課程教學,遠距教學 (ASP.NET MVC 約 140hr) https://dotblogs.com.tw/mis2000lab/2018/08/14/ASPnet_MVC_Online_Learning_MIS2000Lab
寫信給我,不要私訊 -- mis2000lab (at) yahoo.com.tw 或 school (at) mis2000lab.net
(1) 第一天 ASP.NET MVC5 完整影片(5.5小時 / .NET 4.x版)免費試聽。影片 https://youtu.be/9spaHik87-A
(2) 第一天 ASP.NET Core MVC 完整影片(3小時 / .NET Core 6.0~8.0)免費試聽。影片 https://youtu.be/TSmwpT-Bx4I
[學員感言] mis2000lab課程評價 - ASP.NET MVC , WebForm 。 https://mis2000lab.medium.com/%E5%AD%B8%E5%93%A1%E6%84%9F%E8%A8%80-mis2000lab%E8%AA%B2%E7%A8%8B%E8%A9%95%E5%83%B9-asp-net-mvc-webform-77903ce9680b
ASP.NET遠距教學、線上課程(Web Form + MVC)。 第一天課程, "完整" 試聽。
......... facebook社團 https://www.facebook.com/mis2000lab ......................
......... YouTube (ASP.NET) 線上教學影片 https://www.youtube.com/channel/UC6IPPf6tvsNG8zX3u1LddvA/
Blog文章 "附的範例" 無法下載,請看 https://dotblogs.com.tw/mis2000lab/2016/03/14/2008_2015_mis2000lab_sample_download
請看我們的「售後服務」範圍(嚴格認定)。
......................................................................................................................................................
ASP.NET MVC => .NET Core MVC 線上教學 ...... 第一天課程 完整內容 "免費"讓您評估 / 試聽

[遠距教學、教學影片] ASP.NET (Web Form) 課程 上線了!MIS2000Lab.主講 事先錄好的影片,並非上課側錄! 觀看時,有如「一對一」面對面講課。

using