105 lines
4.0 KiB
C#
105 lines
4.0 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using Aspose.Words;
|
||
using Aspose.Words.Saving;
|
||
using System.IO;
|
||
using UnityEngine.UI;
|
||
using System.Drawing.Drawing2D;
|
||
using System.Drawing.Text;
|
||
using System.Drawing;
|
||
using System.Threading.Tasks;
|
||
|
||
/// <summary>
|
||
/// 加载word
|
||
/// </summary>
|
||
public class LoadWordTest : MonoBehaviour
|
||
{
|
||
public static LoadWordTest Instance;
|
||
|
||
public GameObject imageprefab;
|
||
public Transform content;
|
||
|
||
private void Start()
|
||
{
|
||
Instance = this;
|
||
Debug.Log(Application.streamingAssetsPath + "/ict" + SetJson.instance.ReadLanguage() + "使用文档.docx");
|
||
LoadWordGo(Application.streamingAssetsPath + "/ict"+ SetJson.instance.ReadLanguage() + "使用文档.docx");
|
||
//LoadWordGo("C:\\Users\\PC\\Desktop\\ict"+ SetJson.instance.ReadLanguage()+".docx");
|
||
/*
|
||
if (SetJson.instance.ReadLanguage() == "Java")
|
||
{
|
||
// LoadWordGo(Application.streamingAssetsPath+"/ictJava使用文档.docx");
|
||
LoadWordGo("C:\\Users\\PC\\Desktop\\ictJava.docx");
|
||
}
|
||
else if (SetJson.instance.ReadLanguage() == "Python")
|
||
{
|
||
// LoadWordGo(Application.streamingAssetsPath + "/ictPython使用文档.docx");
|
||
LoadWordGo("C:\\Users\\PC\\Desktop\\ictPython.docx");
|
||
}
|
||
else
|
||
{
|
||
// LoadWordGo(Application.streamingAssetsPath + "/ictC使用文档.docx");
|
||
LoadWordGo("C:\\Users\\PC\\Desktop\\ictC.docx");
|
||
}
|
||
*/
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载Word并转换为图片
|
||
/// </summary>
|
||
public async void LoadWordGo(string path)
|
||
{
|
||
//清理content下的旧物体
|
||
for (int i = 0; i < content.childCount; i++)
|
||
{
|
||
Destroy(content.GetChild(i).gameObject);
|
||
}
|
||
await Task.Delay(1000);
|
||
Document doc = new Document(path);
|
||
|
||
ImageSaveOptions iso = new ImageSaveOptions(SaveFormat.Jpeg);
|
||
iso.PrettyFormat = true; //漂亮的格式
|
||
iso.UseAntiAliasing = true; //抗锯齿
|
||
iso.Resolution = 200; //iso.Resolution = 600;
|
||
GraphicsQualityOptions gs = new GraphicsQualityOptions()
|
||
{
|
||
SmoothingMode = SmoothingMode.AntiAlias,
|
||
TextRenderingHint = TextRenderingHint.AntiAliasGridFit,
|
||
CompositingMode = CompositingMode.SourceOver,
|
||
CompositingQuality = CompositingQuality.AssumeLinear, //参数可以调整测试下效果
|
||
InterpolationMode = InterpolationMode.High,//最高质量图片 参数可以调整测试下效果
|
||
StringFormat = StringFormat.GenericTypographic
|
||
};
|
||
|
||
iso.GraphicsQualityOptions = gs;
|
||
iso.JpegQuality = 100;
|
||
|
||
|
||
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);
|
||
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
}
|