Home > Enterprise >  Open PDF in Hololens (Unity)
Open PDF in Hololens (Unity)

Time:03-10

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? 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

CodePudding user response:

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. 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);
}             
  • Related