Unity 基于Paroxe PDFRenderer 实现PC/WebGL 平台PDF 读取显示及下载

阅读: 评论:0

Unity 基于Paroxe PDFRenderer 实现PC/WebGL 平台PDF 读取显示及下载

Unity 基于Paroxe PDFRenderer 实现PC/WebGL 平台PDF 读取显示及下载

导入Paroxe PDFRenderer 插件

首先导入Paroxe PDFRenderer 插件

显示PDF

PC

参考

Unity3D 加载PDF文件以及简单的切换页面_unity 打开word pdf_菜菜ANY的博客-CSDN博客先导入插件 PDFRenderer链接: 提取码: z78q然后使用以下代码就可以using Paroxe.PdfRenderer;using System.Collections;using System.Collections.Generic;using System.IO;using UnityEngine;using UnityEngine.Networking;using U._unity 打开word pdf

实现 

UnityWebRequest 读取PDF

UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();if (!request.isNetworkError && request.isDone)
{bytesDocument = request.downloadHandler.data;
}

使用获取到的字节数组打开PDF

documentPDF = new PDFDocument(bytesDocument);

RawImage 显示PDF

currentPage = 0;
Texture2D pdf = documentPDF.Renderer.RenderPageToTexture(documentPDF.GetPage(currentPage));ure = pdf;

显示效果

WebGL

实现

UnityWebRequest 读取PDF

UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();if (!request.isNetworkError && request.isDone)
{bytesDocument = request.downloadHandler.data;
}

使用获取到的字节数组打开PDF 

PDFJS_Promise<PDFDocument> documentPromise = PDFDocument.LoadDocumentFromBytesAsync(bytesDocument);while (!documentPromise.HasFinished)
{yield return null;
}if (!documentPromise.HasSucceeded)
{yield break;
}documentPDF = documentPromise.Result;

RawImage 显示PDF 

currentPage = 0;
PDFJS_Promise<PDFPage> pagePromise = documentPDF.GetPageAsync(currentPage);while (!pagePromise.HasFinished)
{yield return null;
}if (!pagePromise.HasSucceeded)
{yield break;
}PDFPage pagePDF = pagePromise.Result;PDFJS_Promise<Texture2D> renderPromise = PDFRenderer.RenderPageToTextureAsync(pagePDF, (int)pagePDF.GetPageSize().x, (int)pagePDF.GetPageSize().y);while (!renderPromise.HasFinished)
{yield return null;
}if (!renderPromise.HasSucceeded)
{yield break;
}Texture2D pdf = renderPromise.ure = pdf;

显示效果

翻页

PC 平台通过PDFDocument.GetPage(int) 获取指定页码的PDF,WebGL 平台通过PDFDocument.GetPageAsync(int) 获取指定页码的PDF,修改参数即可实现翻页

sumPages = documentPDF.GetPageCount(); //PDF总页码//下一页
currentPage += 1;
if (currentPage >= sumPages)
{currentPage = 0;
}/* todo
* 切换显示PDF
*///上一页
currentPage -= 1;
if (currentPage < 0)
{currentPage = sumPages - 1;
}/* todo
* 切换显示PDF
*/

下载PDF

PC

参考 

Unity3D 加载PDF文件以及简单的切换页面_unity 打开word pdf_菜菜ANY的博客-CSDN博客先导入插件 PDFRenderer链接: 提取码: z78q然后使用以下代码就可以using Paroxe.PdfRenderer;using System.Collections;using System.Collections.Generic;using System.IO;using UnityEngine;using UnityEngine.Networking;using U._unity 打开word pdf

实现 

if (!Directory.Exists(downloadPath))
{Directory.CreateDirectory(downloadPath);
}string downloadFileName = url.Substring(url.LastIndexOf('/') + 1);
string localPath = downloadPath + "/" + downloadFileName;UnityWebRequest webRequest = UnityWebRequest.Get(url);
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError)
{if (File.Exists(localPath)){File.Delete(localPath);}
}
else
{var file = webRequest.downloadHandler.data;FileStream fileStream = new FileStream(localPath, FileMode.Create);fileStream.Write(file, 0, file.Length);fileStream.Close();
}

WebGL

参考

UnityWebGL截图/图片/文件调用浏览器下载 | 梓喵出没 (azimiao).html

实现

根据参考编写jslib,并放入Plugins 文件夹

引入dll

[DllImport("__Internal")]
private static extern void PDFDownloader(string strData, string fileName);

实现下载 

string downloadFileName = url.Substring(url.LastIndexOf('/') + 1);UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();if (!request.isNetworkError && request.isDone)
{byte[] downloadBytes = request.downloadHandler.data;if (downloadBytes != null){PDFDownloader(System.Convert.ToBase64String(downloadBytes), downloadFileName);}
}

效果

拓展 

Android 平台读取显示PDF

Android用PdfRenderer类开发打开pdf文件的功能_android pdfrenderer-CSDN博客PdfRenderer是Android官方用于开发打开pdf文件功能的类,今天介绍一下它的最基本的使用。 使用PdfRenderer有这么几步: 1、用文件的路径生成一个File对象: File file = new File("/mnt/sdcard/printTemplate.pdf"); 2、然后需要用ParcelFileDescriptor类对File对象打一下包: _android pdfrenderer

各种文件对应的ContentType 

各种文件对应的ContentType,拿来即用_pdf的contenttype_way_more的博客-CSDN博客Content-TypeContent-Type即内容类型,Content-Type用于定义网络文件的类型和网页的编码,决定文件接收方将以什么形式、什么编码读取这个文件,这就是经常看到一些网页点击的结果却是下载到的一个文件或一张图片的原因。ContentType属性指定响应的 HTTP内容类型。如果未指定 ContentType,默认为TEXT/HTML。我们在代码也经常需要定义ContentType,用于指定响应的类型例:response.setCharacterEncoding("utf-8"_pdf的contenttype

 Unity 常用路径

【100个 Unity实用技能】| Unity中常用的几种路径 分析,不同平台路径总结_unity 路径_呆呆敲代码的小Y的博客-CSDN博客在Unity中有很多种路径,尤其是在不同的平台上,同一种路径的写法可能最终是不一样的。本文就来总结一下Unity中的几种路径,以及简单的使用方法。_unity 路径

本文发布于:2024-01-28 16:33:35,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17064308168757.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:electron
标签:平台   PDFRenderer   Paroxe   Unity   PDF
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23