在 Hololens (Unity) 中打开 PDF
Open PDF in Hololens (Unity)
下午好!
我试图在 UWP Hololens 应用程序中打开位于已知文件夹中的 pdf,但没有结果...
有谁知道如何进行?
我设法打开它并使用以下方法检索了它的页码:
StorageFolder storageFolder = KnownFolders.CameraRoll;
StorageFile sampleFile = await storageFolder.GetFileAsync("prova.pdf");
PdfDocument pdfDocument = await PdfDocument.LoadFromFileAsync(sampleFile);
test.text = pdfDocument.PageCount.ToString();
然后我使用以下方法添加了页面选择:
using (PdfPage firstPage = pdfDocument.GetPage(0))
{
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
await firstPage.RenderToStreamAsync(stream);
//Missing something here...
}
但是正如我的评论所述,我不知道如何将我创建的流转换为实际纹理以应用于某些东西......
任何建议将不胜感激:)
编辑:
我设法让它发挥作用,对于每一个试图获得相同结果的可怜人来说,这就是我的代码!
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);
}
}
非常感谢@AlexAR
基本上,您可以将流转换为字节数组并将这些字节加载到纹理中。然后,您可以轻松地将此纹理设置为四边形 material 以显示 pdf 页面的图像。
这是我在项目中所做的一个例子:
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);
}
下午好!
我试图在 UWP Hololens 应用程序中打开位于已知文件夹中的 pdf,但没有结果... 有谁知道如何进行? 我设法打开它并使用以下方法检索了它的页码:
StorageFolder storageFolder = KnownFolders.CameraRoll;
StorageFile sampleFile = await storageFolder.GetFileAsync("prova.pdf");
PdfDocument pdfDocument = await PdfDocument.LoadFromFileAsync(sampleFile);
test.text = pdfDocument.PageCount.ToString();
然后我使用以下方法添加了页面选择:
using (PdfPage firstPage = pdfDocument.GetPage(0))
{
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
await firstPage.RenderToStreamAsync(stream);
//Missing something here...
}
但是正如我的评论所述,我不知道如何将我创建的流转换为实际纹理以应用于某些东西...... 任何建议将不胜感激:)
编辑: 我设法让它发挥作用,对于每一个试图获得相同结果的可怜人来说,这就是我的代码!
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);
}
}
非常感谢@AlexAR
基本上,您可以将流转换为字节数组并将这些字节加载到纹理中。然后,您可以轻松地将此纹理设置为四边形 material 以显示 pdf 页面的图像。 这是我在项目中所做的一个例子:
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);
}