Unity3d Reading PDF file contents

Source: Internet
Author: User

Tag: For rect State writer reason box new T source ima

Recently in the study Unity3d read the contents of the PDF, envisioned three scenarios, one is implemented in Java, the second is to call C # Itextsharp library or PDFBox library to achieve, three is to download PDF renderer Plugin (Local tyrants can buy this plugin, Supported systems are also more comprehensive), Java is not very proficient, I have studied the C # call Itextsharp Library to implement, but unfortunately does not support the UWP system.

Before you write the code, you have to import Itextsharp.dll, Spire.Pdf.dll, Spire.License.dll, and System.Drawing.dll libraries in the Plugins folder under assets.

Upload code directly:

Read PDF text content 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 Strateg            y = 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 ("Error file:" + "Cause:" + ex.)        ToString ()); Wlog.        Flush (); Wlog.        Close (); RetUrn null; }}

//read a picture in a 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 P            Dfreader (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;    }///save to local//bitmap DD = new Bitmap (pimage); Dd. Save (Application.datapath + "/resources/wode. Jpeg ");}

I only tested windows so that others did not test, I hope you can help, but also to test other systems.

Unity3d Reading PDF file contents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.