0

现在我在 Unity 中使用 ghostscript 将 pdf 转换为 jpg 并在我的项目中查看它们。

目前它的流程如下:
-Pdfs 被转换成多个 jpeg(每页一个)
-转换后的 jpeg 被写入磁盘
-然后它们按字节读入 2D 纹理
-并且这个 2D 纹理被分配给 GameObjects RawImage零件

这在 Unity 中完美运行,但是......(现在出现了问题)我的项目打算在 Microsoft Hololens 上运行。Hololens 在 Windows 10 API 上运行,但容量有限。

出现问题的地方是当尝试转换 pdf 并在 Hololens 上查看它们时。很简单,Hololens 无法在其已知文件夹(图片、文档等)之外创建或删除文件。

对于这个问题,我想象的解决方案是不要将转换后的 jpeg 文件写入磁盘,而是将它们写入内存并从那里查看它们。

在与 GhostScript 开发人员交谈时,有人告诉我 GhostScript.NET 做了我想做的事情 - 转换 pdf 并从内存中查看它们(我相信,它使用 Rasterizer/Viewer 类来做到这一点,但我还是不太明白出色地)。

我一直被引导查看最新的 GhostScript.NET 文档来确定这是如何完成的,但我只是不太了解它们来解决这个问题。

那么我的问题是,基于我现在如何使用ghostscript,我如何在我的项目中使用GhostScript.NET 将转换后的jpeg 写入内存并在那里查看它们?

这是我现在的做法(代码方面):

        //instantiate
        byte[] fileData;
        Texture2D tex = null;

        //if a PDF file exists at the current head path
        if (File.Exists(CurrentHeadPath))
        {
            //Transform pdf to jpg
            PdfToImage.PDFConvert pp = new PDFConvert();
            pp.OutputFormat = "jpeg"; //format
            pp.JPEGQuality = 100; //100% quality
            pp.ResolutionX = 300; //dpi
            pp.ResolutionY = 500;
            pp.OutputToMultipleFile = true;
            CurrentPDFPath = "Data/myFiles/pdfconvimg.jpg";

            //this call is what actually converts the pdf to jpeg files
            pp.Convert(CurrentHeadPath, CurrentPDFPath);

            //this just loads the first image
            if (File.Exists("Data/myFiles/pdfconvimg" + 1 + ".jpg"))
            {
                //reads in the jpeg file by bytes
                fileData = File.ReadAllBytes("Data/myFiles/pdfconvimg" + 1 + ".jpg");
                tex = new Texture2D(2, 2);
                tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.

                //Read Texture into RawImage component
                PdfObject.GetComponent<RawImage>().texture = tex;
                PdfObject.GetComponent<RawImage>().rectTransform.sizeDelta = new Vector2(288, 400);
                PdfObject.GetComponent<RawImage>().enabled = true;
            }

            else
            {
                Debug.Log("reached eof");
            }
        }

转换函数来自我从代码项目中获得的名为PDFConvert的脚本。特别是如何使用 Ghostscript API 将 PDF 转换为图像

4

1 回答 1

0

GhostScript.Net 文档中,查看标记为:“使用 GhostscriptRasterizer 类”的示例代码。具体如下几行:

Image img = _rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
img.Save(pageFilePath, ImageFormat.Png);

Image类似乎是System.Drawing包的一部分,并且 System.Drawing.Image 有另一个Save方法,其中第一个参数是System.IO.Stream

于 2017-08-01T14:30:42.330 回答