我目前正在構建一個.NET應用程序,其中一個要求是它必須將PDF文件轉換爲XML文件。有沒有人有過這樣的成功?如果是這樣,你用了什麼?pdf轉換爲使用.NET的xml轉換
回答
我結束了使用Byte Scout's PDF Extractor SDK。它工作得很好。
您可以使用pdf庫,如iTextSharp查詢您的PDF文件。一旦你訪問了你需要的數據,你就可以輕鬆創建一個xml文件。網上有關於如何用c#和其他.net語言創建xml文件的TON信息。如果你有一個具體的問題,請問;-)
我已經做了很多次這種項目之前。
事情你需要做的:
1)看看這個項目Extract Text from PDF in C#。該項目使用ITextSharp。
- 如果您下載示例項目並查看其工作原理,將會更好。在這個項目中,它展示瞭如何從PDF中提取數據。查看PDFParser類,它具有名爲ExtractTextFromPDFBytes(byte [] input)函數,您可以看到文本是如何從未壓縮的pdf文件中提取出來的。 不要忘了包含ITextSharp dll。
PDFParser類
1 using System; 2 using System.IO; 3 using iTextSharp.text.pdf; 4 5 namespace PdfToText 6 { 7 /// 8 /// Parses a PDF file and extracts the text from it. 9 /// 10 public class PDFParser 11 { 12 /// BT = Beginning of a text object operator 13 /// ET = End of a text object operator 14 /// Td move to the start of next line 15 /// 5 Ts = superscript 16 /// -5 Ts = subscript 17 18 #region Fields 19 20 #region _numberOfCharsToKeep 21 /// 22 /// The number of characters to keep, when extracting text. 23 /// 24 private static int _numberOfCharsToKeep = 15; 25 #endregion 26 27 #endregion 28 29 #region ExtractText 30 /// 31 /// Extracts a text from a PDF file. 32 /// 33 /// the full path to the pdf file. 34 /// the output file name. 35 /// the extracted text 36 public bool ExtractText(string inFileName, string outFileName) 37 { 38 StreamWriter outFile = null; 39 try 40 { 41 // Create a reader for the given PDF file 42 PdfReader reader = new PdfReader(inFileName); 43 //outFile = File.CreateText(outFileName); 44 outFile = new StreamWriter(outFileName, false, System.Text.Encoding.UTF8); 45 46 Console.Write("Processing: "); 47 48 int totalLen = 68; 49 float charUnit = ((float)totalLen)/(float)reader.NumberOfPages; 50 int totalWritten= 0; 51 float curUnit = 0; 52 53 for (int page = 1; page = 1.0f) 59 { 60 for (int i = 0; i = 1.0f) 70 { 71 for (int i = 0; i 104 /// This method processes an uncompressed Adobe (text) object 105 /// and extracts text. 106 /// 107 /// uncompressed 108 /// 109 private string ExtractTextFromPDFBytes(byte[] input) 110 { 111 if (input == null || input.Length == 0) return ""; 112 113 try 114 { 115 string resultString = ""; 116 117 // Flag showing if we are we currently inside a text object 118 bool inTextObject = false; 119 120 // Flag showing if the next character is literal 121 // e.g. '\\' to get a '\' character or '\(' to get '(' 122 bool nextLiteral = false; 123 124 //() Bracket nesting level. Text appears inside() 125 int bracketDepth = 0; 126 127 // Keep previous chars to get extract numbers etc.: 128 char[] previousCharacters = new char[_numberOfCharsToKeep]; 129 for (int j = 0; j = ' ') && (c = 128) && (c 235 /// Check if a certain 2 character token just came along (e.g. BT) 236 /// 237 /// the searched token 238 /// the recent character array 239 /// 240 private bool CheckToken(string[] tokens, char[] recent) 241 { 242 foreach(string token in tokens) 243 { 244 if ((recent[_numberOfCharsToKeep - 3] == token[0]) && 245 (recent[_numberOfCharsToKeep - 2] == token[1]) && 246 ((recent[_numberOfCharsToKeep - 1] == ' ') || 247 (recent[_numberOfCharsToKeep - 1] == 0x0d) || 248 (recent[_numberOfCharsToKeep - 1] == 0x0a)) && 249 ((recent[_numberOfCharsToKeep - 4] == ' ') || 250 (recent[_numberOfCharsToKeep - 4] == 0x0d) || 251 (recent[_numberOfCharsToKeep - 4] == 0x0a)) 252 ) 253 { 254 return true; 255 } 256 } 257 return false; 258 } 259 #endregion 260 } 261 }
2.)解析所提取的文本,並創建和XML文件。
我以前的一些擔憂是pdf中包含頁面內部的鏈接或URL。現在,如果您也擔心這個問題,正則表達式可以輕鬆解決您的問題,但我建議您稍後再處理它。
現在這裏是如何創建一個XML的示例代碼。瞭解代碼是如何工作的,以便稍後您將知道如何處理自己的代碼。
try { //XmlDataDocument sourceXML = new XmlDataDocument(); string xmlFile = Server.MapPath(「DVDlist.xml」); //create a XML file is not exist System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(xmlFile, null); //starts a new document writer.WriteStartDocument(); //write comments writer.WriteComment(「Commentss: XmlWriter Test Program」); writer.Formatting = Formatting.Indented; writer.WriteStartElement(「DVDlist」); writer.WriteStartElement(「DVD」); writer.WriteAttributeString(「ID」, 「1″); //write some simple elements writer.WriteElementString(「Title」, 「Tere Naam」); writer.WriteStartElement(「Starring」); writer.WriteElementString(「Actor」, 「Salman Khan」); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.Close(); } catch (Exception e1) { Page.Response.Write(e1); }
希望它能幫助:)
看看pdf2Data。
http://itextpdf.com/blog/pdf2data-extract-information-invoices-and-templates
它將pdf文件轉換爲基於模板的XML文件。模板使用選擇器定義,允許最終用戶指定「第二頁上的表格選擇」或「選擇以該特定字體書寫的文本」等內容。
請記住,我隸屬於iText,所以即使我對PDF的知識很廣泛,但我可能會認爲它對iText產品有偏見(看我幫助開發它們)。
你必須說你和itext有關係。 – juFo 2018-02-15 08:26:15
我已經提到了我的關係。 – 2018-02-15 09:32:37
- 1. 使用氧氣將XML轉換爲PDF
- 2. 使用PHP將PDF轉換爲XML
- 3. 使用matlab將PDF轉換爲XML
- 4. 使用Java將PDF轉換爲XML
- 5. Html轉換爲PDF的PDF轉換
- 6. WordML轉換爲PDF轉換
- 7. 用於轉換爲PDF的.NET組件
- 8. PDF轉換爲PDF轉換器
- 9. 轉換爲PDF
- 10. Html轉換爲PDF與webkit轉換
- 11. 如何將pdf文件轉換爲C#.net中的xml文件?
- 12. 轉換爲pdf(yii2)
- 13. Java轉換爲PDF
- 14. Bash轉換爲pdf
- 15. 使用XSLT(編號轉換)將XML轉換爲另一個XML
- 16. 將xml文件轉換爲pdf c#
- 17. 如何將PDF轉換爲XML
- 18. ITextSharp問題 - 將XML轉換爲PDF
- 19. 將XML轉換爲HTML中的.net
- 20. .xlsx轉換爲Perl中的xml轉換
- 21. 使用iTextSharp的轉換HTML爲PDF
- 22. 將html轉換爲pdf轉換爲空白pdf
- 23. 使用Asp.Net使用itexthandler將xml轉換爲pdf?
- 24. 轉換vb.net代碼轉換爲C#.NET
- 25. 使用XSL將XML轉換爲XML
- 26. 使用exec將pdf轉換爲JPG
- 27. 使用Java將EPUB轉換爲PDF
- 28. 使用PDFSharp將HTML轉換爲PDF
- 29. 使用JavaScript將Excel轉換爲PDF
- 30. 使用Python將PDF轉換爲HTML
你想要「轉換」什麼樣的pdf? PDF文件不是「結構化」的,所以通常從他們那裏提取信息是一項艱鉅的任務。我認爲你應該提供更多關於你想要實現的細節。 – yms 2011-06-09 03:55:20