您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
最近在研究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下好使,其他的没有测试,希望大家能够用得着,也可以测试一下其他的系统。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。