最近在研究Unity3D中读取PDF的内容,预想了三种方案,一是用Java来实现,二是调用C#的iTextSharp库或者PDFBox库来实现,三是下载PDF Renderer插件(土豪可以买这个插件,支持的系统也比较全面),java不是很精通,我就研究了一下C#调用iTextSharp库来实现,但是遗憾的是不支持UWP系统。
再写代码之前呢,得在Assets下的Plugins文件夹中导入itextsharp.dll、Spire.Pdf.dll、Spire.License.dll、System.Drawing.dll库,才能好使。文章源自大腿Plus-https://www.zhaoshijun.com/archives/908
直接上传代码:文章源自大腿Plus-https://www.zhaoshijun.com/archives/908
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
//读取PDF文字内容 private void ReadPDF_Click() { string path = Application.streamingAssetsPath + "/SimplePDF.pdf"; msg.text = OnCreated(path); } private string OnCreated(string filepath) { try { string pdffilename = filepath; PdfReader pdfReader = new PdfReader(pdffilename); int numberOfPages = pdfReader.NumberOfPages; string text = string.Empty; for (int i = 1; i <= numberOfPages; ++i) { iTextSharp.text.pdf.parser.ITextExtractionStrategy strategy = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy(); text += iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfReader, i, strategy); } pdfReader.Close(); return text; } catch (Exception ex) { StreamWriter wlog = File.AppendText(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\mylog.log"); wlog.WriteLine("出错文件:" + "原因:" + ex.ToString()); wlog.Flush(); wlog.Close(); return null; } } //读取PDF中的图片 private void ReadPDFImage() { string path = Application.streamingAssetsPath + "/aa.pdf"; ExtractImageEvent(path); } private void ExtractImageEvent(string padPath) { try { int index = 0; PdfReader pdfReader = new PdfReader(padPath); Debug.Log(pdfReader.NumberOfPages); for (int pageNumber = 1; pageNumber <= pdfReader.NumberOfPages; pageNumber++) { PdfReader pdf = new PdfReader(padPath); PdfDictionary pg = pdf.GetPageN(pageNumber); PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES)); PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT)); try { foreach (PdfName name in xobj.Keys) { PdfObject obj = xobj.Get(name); if (obj.IsIndirect()) { PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj); string width = tg.Get(PdfName.WIDTH).ToString(); string height = tg.Get(PdfName.HEIGHT).ToString(); //ImageRenderInfo imgRI = ImageRenderInfo.CreateForXObject((GraphicsState)new Matrix(float.Parse(width), float.Parse(height)), (PRIndirectReference)obj, tg); ImageRenderInfo imgRI = ImageRenderInfo.CreateForXObject(new GraphicsState(), (PRIndirectReference)obj, tg); RenderImageByte(imgRI,index); } } } catch { continue; } } } catch (Exception) { throw; } } private void RenderImageByte(ImageRenderInfo renderInfo,int index) { PdfImageObject imageObj = renderInfo.GetImage(); pimage = imageObj.GetDrawingImage(); MemoryStream ms = new MemoryStream(); pimage.Save(ms, ImageFormat.Png); byte[] byteData = new Byte[ms.Length]; ms.Position = 0; ms.Read(byteData, 0, byteData.Length); ms.Close(); Texture2D tex2d = new Texture2D(500, 1000); if (tex2d.LoadImage(byteData)) { UIimage.texture = tex2d; } ///保存到本地 //Bitmap dd = new Bitmap(pimage); //dd.Save(Application.dataPath + "/Resources/wode.Jpeg"); } |
我只测试了windows下好使,其他的没有测试,希望大家能够用得着,也可以测试一下其他的系统。文章源自大腿Plus-https://www.zhaoshijun.com/archives/908 文章源自大腿Plus-https://www.zhaoshijun.com/archives/908
我的微信
微信扫一扫
shijun_z
我的QQ
QQ扫一扫
846207670
评论