如何将PDF字节数组转换为可读取文档?可使用DevExpress框架
使用DevExpress处理PDF byte[]数组的方案
场景1:可视化查看PDF内容
如果需要在WinForms/WPF界面中直接查看PDF,可使用DevExpress的PDF Viewer控件加载byte[]:
WinForms示例
// 假设已在窗体中添加PdfViewer控件实例pdfViewer1 byte[] pdfBytes = /* 你的PDF字节数组 */; using (MemoryStream ms = new MemoryStream(pdfBytes)) { pdfViewer1.LoadDocument(ms); }
WPF示例
// 假设已添加PdfViewerControl实例pdfViewerControl byte[] pdfBytes = /* 你的PDF字节数组 */; using (MemoryStream ms = new MemoryStream(pdfBytes)) { pdfViewerControl.DocumentSource = ms; }
场景2:提取PDF文本内容
如果仅需读取PDF中的文本,无需可视化,可使用PdfDocumentProcessor类:
byte[] pdfBytes = /* 你的PDF字节数组 */; string extractedText = string.Empty; using (MemoryStream ms = new MemoryStream(pdfBytes)) { using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) { processor.LoadDocument(ms); extractedText = processor.TextContent.ToString(); } } // extractedText即为PDF中的文本内容
注意事项
- 确保项目已引用
DevExpress.Docs、DevExpress.Pdf.Core、DevExpress.Pdf.Viewer相关程序集; - ASP.NET场景下,可将byte[]直接输出到响应流,让浏览器打开:
byte[] pdfBytes = /* 你的PDF字节数组 */; Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "inline; filename=document.pdf"); Response.BinaryWrite(pdfBytes); Response.End();
内容的提问来源于stack exchange,提问作者Gonçalo Bastos
相关产品推荐
相关产品推荐

