[英]Open PDF in Hololens (Unity)
Good afternoon!下午好!
I'm trying to open a pdf located into knownfolders in an UWP Hololens app without results... Does anyone know how to proceed?我正在尝试打开位于 UWP Hololens 应用程序中已知文件夹中的 pdf,但没有结果...有谁知道如何继续? I managed to open it and retrieve its page number by using:我设法打开它并使用以下方法检索其页码:
StorageFolder storageFolder = KnownFolders.CameraRoll;
StorageFile sampleFile = await storageFolder.GetFileAsync("prova.pdf");
PdfDocument pdfDocument = await PdfDocument.LoadFromFileAsync(sampleFile);
test.text = pdfDocument.PageCount.ToString();
I then added the selection of a page by using:然后,我使用以下方法添加了页面选择:
using (PdfPage firstPage = pdfDocument.GetPage(0))
{
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
await firstPage.RenderToStreamAsync(stream);
//Missing something here...
}
But as my comment state i have no idea how to convert the stream i create into an actual texture to apply to something... Any suggestion would be highly appreciated :)但正如我的评论所说,我不知道如何将我创建的流转换为实际的纹理以应用于某些东西......任何建议将不胜感激:)
Edit: I managed to make it work, to every poor soul trying to achieve the same results this is my code!编辑:我设法使它工作,对于每个试图达到相同结果的可怜的灵魂,这是我的代码!
public async void LoadAsync()
{
StorageFolder storageFolder = KnownFolders.CameraRoll;
StorageFile sampleFile = await storageFolder.GetFileAsync("test.pdf");
//pdfDocument is an instanced object of type PdfDocument
pdfDocument = await PdfDocument.LoadFromFileAsync(sampleFile);
}
public async void ChangePageAsync()
{
//pg is the page to load
using (PdfPage firstPage = pdfDocument.GetPage((uint)pg))
{
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
await firstPage.RenderToStreamAsync(stream);
MemoryStream tempStream = new MemoryStream();
await stream.AsStream().CopyToAsync(tempStream);
byte[] imgBytes = new byte[16 * 1024];
imgBytes = tempStream.ToArray();
Texture2D imgTex = new Texture2D(2048, 2048, TextureFormat.BC7, false);
imgTex.LoadImage(imgBytes);
imgTex.filterMode = FilterMode.Bilinear;
imgTex.wrapMode = TextureWrapMode.Clamp;
MeshRenderer meshRenderer = PDFPlane.GetComponent<MeshRenderer>();
Material mat = meshRenderer.material;
mat.SetTexture("_MainTex", imgTex);
mat.mainTextureScale = new Vector2(1, 1);
}
}
Big thanks to @AlexAR非常感谢@AlexAR
Basically you can turn your stream into a byte array and load these bytes into a texture.基本上,您可以将流转换为字节数组并将这些字节加载到纹理中。 Then you can easily set this texture to a quad material to show the image of the pdf page.然后您可以轻松地将此纹理设置为四边形材质以显示 pdf 页面的图像。 Here's a exemple of what I've done on a project :这是我在一个项目中所做的一个例子:
using (PdfPage firstPage = pdfDocument.GetPage(0))
{
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
await firstPage.RenderToStreamAsync(stream);
MemoryStream tempStream = (MemoryStream)stream.AsStream();
//initialize an empty byte array
byte[] imgBytes = new byte[16 * 1024];
imgBytes = tempStream.ToArray();
//best parameters I found to get a good quality
Texture2D imgTex = new Texture2D(2048, 2048, TextureFormat.BC7, false);
imgTex.LoadImage(imgBytes);
imgTex.filterMode = FilterMode.Bilinear;
imgTex.wrapMode = TextureWrapMode.Clamp;
MeshRenderer meshRenderer = YourGameObject.GetComponent<MeshRenderer>();
Material mat = meshRenderer.material;
mat.SetTexture("_MainTex", imgTex);
mat.mainTextureScale = new Vector2(1, 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.