码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D读取PDF文件内容

时间:2018-07-05 14:50:50      阅读:476      评论:0      收藏:0      [点我收藏+]

标签:for   rect   state   writer   原因   box   new t   source   ima   

最近在研究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库,才能好使。

直接上传代码:

//读取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下好使,其他的没有测试,希望大家能够用得着,也可以测试一下其他的系统。

Unity3D读取PDF文件内容

标签:for   rect   state   writer   原因   box   new t   source   ima   

原文地址:http://blog.51cto.com/myselfdream/2136560

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!