unity aspon word excel ppt pdf转图片

最近做多点查询,需要在项目中展示word excel ppt 和pdf
思路是转换为图片
多方查询,成功使用aspon完成,走了很多弯路,在此记录,方便后来人

准备工作
将以下.dll导入unity的plugins文件夹 资源及完整项目可以通过文章结尾的链接免费下载unity aspon word excel ppt pdf转图片_第1张图片
word代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Aspose.Words;
using Aspose.Words.Saving;
using System.IO;
using UnityEngine.UI;

/// 
/// 加载word
/// 
public class LoadWordTest : MonoBehaviour
{
    public static LoadWordTest Instance;

    public GameObject imageprefab;
    public Transform content;

    private void Start()
    {
        Instance = this;
    }

    /// 
    /// 加载Word并转换为图片
    /// 
    public void LoadWordGo(string path)
    {
        //清理content下的旧物体
        for (int i = 0; i < content.childCount; i++)
        {
            Destroy(content.GetChild(i).gameObject);
        }


        Document doc = new Document(path);

        ImageSaveOptions iso = new ImageSaveOptions(SaveFormat.Jpeg);
        iso.PrettyFormat = true; //漂亮的格式
        iso.UseAntiAliasing = true; //抗锯齿
        for (int i = 0; i < doc.PageCount; i++)
        {
            iso.PageIndex = i;
            //声明内存流,将图片转换为内存流,再由流转换为byte数组,然后用texture2d加载byte数组
            using (MemoryStream ms = new MemoryStream())
            {
                doc.Save(ms, iso);
                byte[] buff = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buff, 0, (int)ms.Length);

                //注意这个iamge的命名空间为system.drawing不是unity.ui,这个图片的目的是提供图片的宽高
                System.Drawing.Image sizeImage = System.Drawing.Image.FromStream(ms);

                Texture2D texture2D = new Texture2D(sizeImage.Width, sizeImage.Height);
                texture2D.LoadImage(buff);

                Image image = Instantiate(imageprefab, content).GetComponent<Image>();
                //根据转化出来的图片的大小设置unity image的大小
                image.rectTransform.sizeDelta = new Vector2(sizeImage.Width, sizeImage.Height);
                //用texture2d为精灵赋值
                image.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
            }
        }

    }

}

excel代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Drawing.Imaging;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using System.IO;
using UnityEngine.UI;

/// 
/// 加载excel并转为图片
/// 
public class LoadExcelTest : MonoBehaviour
{
    public static LoadExcelTest Instance;

    public GameObject imageprefab;
    public Transform content;

    private void Start()
    {
        Instance = this;
    }

    /// 
    /// 加载excel
    /// 
    public void LoadExcelGO(string path)
    {
        //清理content下的旧物体
        for (int i = 0; i < content.childCount; i++)
        {
            Destroy(content.GetChild(i).gameObject);
        }

        //查找整个文档
        Workbook book = new Workbook(path);
        //获取一个工作表
        Worksheet sheet = book.Worksheets[0];

        //设置边距
        sheet.PageSetup.LeftMargin = 0;
        sheet.PageSetup.RightMargin = 0;
        sheet.PageSetup.BottomMargin = 0;
        sheet.PageSetup.TopMargin = 0;

        //设置图片的打印格式
        ImageOrPrintOptions imageOptions = new ImageOrPrintOptions();
        imageOptions.ImageFormat = ImageFormat.Jpeg;
        imageOptions.OnePagePerSheet = true;
        imageOptions.PrintingPage = PrintingPageType.IgnoreBlank;

        //excel转图片的转换器对象
        SheetRender sr = new SheetRender(sheet, imageOptions);

        //声明内存流,将图片转换为内存流,再由流转换为byte数组,然后用texture2d加载byte数组
        using (MemoryStream ms = new MemoryStream())
        {
            sr.ToImage(0, ms);
            byte[] buff = new byte[ms.Length];
            ms.Seek(0, SeekOrigin.Begin);
            ms.Read(buff, 0, (int)ms.Length);

            //注意这个iamge的命名空间为system.drawing不是unity.ui,这个图片的目的是提供图片的宽高
            System.Drawing.Image sizeImage = System.Drawing.Image.FromStream(ms);

            Texture2D texture2D = new Texture2D(sizeImage.Width, sizeImage.Height);
            texture2D.LoadImage(buff);

            Image image = Instantiate(imageprefab, content).GetComponent<Image>();
            //根据转化出来的图片的大小设置unity image的大小
            image.rectTransform.sizeDelta = new Vector2(sizeImage.Width, sizeImage.Height);
            //用texture2d为精灵赋值
            image.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
        }

    }

}

ppt代码

using Aspose.Slides;
using System;
using System.Drawing.Imaging;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class LoadPPTTest : MonoBehaviour
{
    public static LoadPPTTest Instance;

    public GameObject imageprefab;
    public Transform content;

    private void Start()
    {
        Instance = this;
    }
    public void LoadPPTGO(string pptPath)
    {
        //清理content下的旧物体
        for (int i = 0; i < content.childCount; i++)
        {
            Destroy(content.GetChild(i).gameObject);
        }


        Presentation presentation = new Aspose.Slides.Presentation(pptPath);
        //遍历文档(只做示例使用自己根据需求拓展)
        for (int i = 0; i < presentation.Slides.Count; i++)
        {
            ISlide slide = presentation.Slides[i];
            var bitmap = slide.GetThumbnail(1f, 1f);


            //声明内存流,将图片转换为内存流,再由流转换为byte数组,然后用texture2d加载byte数组
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, ImageFormat.Jpeg);
                byte[] buff = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buff, 0, (int)ms.Length);

                //注意这个iamge的命名空间为system.drawing不是unity.ui,这个图片的目的是提供图片的宽高
                System.Drawing.Image sizeImage = System.Drawing.Image.FromStream(ms);

                Texture2D texture2D = new Texture2D(sizeImage.Width, sizeImage.Height);
                texture2D.LoadImage(buff);

                Image image = Instantiate(imageprefab, content).GetComponent<Image>();
                //根据转化出来的图片的大小设置unity image的大小
                image.rectTransform.sizeDelta = new Vector2(sizeImage.Width, sizeImage.Height);
                //用texture2d为精灵赋值
                image.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
            }

        }
    }
}

pdf代码

using Aspose.Pdf;
using Aspose.Pdf.Devices;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using UnityEngine;

/// 
/// PDF转图片
/// 
public class LoadPDFTest : MonoBehaviour
{
    public static LoadPDFTest Instance;

    public GameObject imageprefab;
    public Transform content;

    private void Start()
    {
        Instance = this;
    }
    /// 
    /// 加载PDF
    /// 
    public void LoadPDFGo(string path)
    {
        //清理content下的旧物体
        for (int i = 0; i < content.childCount; i++)
        {
            Destroy(content.GetChild(i).gameObject);
        }

        Document pdf = new Document(path);


        for (int i = 1; i <= pdf.Pages.Count; i++)
        {
            //PngDevice pngDevice = new PngDevice((int)pdf.Pages[i].PageInfo.Width,(int)pdf.Pages[i].PageInfo.Height);
            PngDevice pngDevice = new PngDevice((int)pdf.Pages[i].PageInfo.Width*2,(int)pdf.Pages[i].PageInfo.Height*2);

            //声明内存流,将图片转换为内存流,再由流转换为byte数组,然后用texture2d加载byte数组
            using (MemoryStream ms = new MemoryStream())
            {
                pngDevice.Process(pdf.Pages[i], ms);
                byte[] buff = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buff, 0, (int)ms.Length);

                //注意这个iamge的命名空间为system.drawing不是unity.ui,这个图片的目的是提供图片的宽高
                System.Drawing.Image sizeImage = System.Drawing.Image.FromStream(ms);

                Texture2D texture2D = new Texture2D(sizeImage.Width, sizeImage.Height);
                texture2D.LoadImage(buff);

                UnityEngine.UI.Image image = Instantiate(imageprefab, content).GetComponent<UnityEngine.UI.Image>();
                //根据转化出来的图片的大小设置unity image的大小
                image.rectTransform.sizeDelta = new Vector2(sizeImage.Width, sizeImage.Height);
                //用texture2d为精灵赋值
                image.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
            }

        }

    }

}

遇到的问题:
1 程序在编辑器运行正常,打包(开发构建)出来后报NotSupportedException: Encoding XXX data ,这是因为字符集的问题,
找到untiy的安装目录 Editor/Data/MonoBleedingEdge/lib/mono/unityjit里找到 I18N开头的几个.dll,拖进项目的plugins文件夹 里就行,注意在mono下的其他文件夹里也有这几个.dll,但是体积都比较小,有些拖进plugins里不能解决打包报错的问题,估计是没有unityjit里的全,unityjit里这几个文件的体积是最大的
2 运行后,转换出来的图片最上方有水印,因为aspon需要破解,目前我的演示中已经使用破解版,没有水印了

资源下载地址: 免费下载

你可能感兴趣的:(unity,unity,游戏引擎)