Unity C# Aspose.words的使用

一、

二、

三、

四、邮件合并(填写域)

4.1. 文本域填写

实例代码

string tempFile = Application.streamingAssetsPath +"\\Templates\\template.doc"string[] fieldNames = {
    
     "ID" }; 			//域名
string[] values = {
    
     "5" };      			/Document doc = new Document(tempFile);
doc.MailMerge.Execute(fieldNames, values);
doc.Save(Application.persistentDataPath + "\\Export\\out.doc");

4.2. 图像域填写

五、 打印机

5.1. 使用默认打印机打印

实例代码

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello world!");

// 1 - 使用默认打印机打印:
doc.Print();

5.2. 使用指定打印机打印

实例代码

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello world!");

// 2 - 按名称指定我们希望打印文档的打印机:
string myPrinter = PrinterSettings.InstalledPrinters[4];
doc.Print(myPrinter);

5.3. 获取打印机列表

实例代码

foreach (string printer in PrinterSettings.InstalledPrinters)
{
    
    
	Debug.Log(index +":"+printer);
    index++;
 }

5.4. Unity遇到的错误

  • 错误描述:
    无法调用Print或带参数的Print方法。
  • 错误提示:
    OutOfMemoryException: Not enough memory to complete operation [GDI+ status: OutOfMemory] System.Drawing.GDIPlus.CheckStatus (System.Drawing.Status status) (at <7026bacc22c446e78b11ff130428baf7>:0) System.Drawing.Graphics.FromHdc (System.IntPtr hdc) (at <7026bacc22c446e78b11ff130428baf7>:0) System.Drawing.Printing.PrintDocument.Print () (at <7026bacc22c446e78b11ff130428baf7>:0) (wrapper remoting-invoke-with-check) System.Drawing.Printing.PrintDocument.Print() Aspose.Words.Document.Print (System.Drawing.Printing.PrinterSettings printerSettings, System.String documentName) (at <4d44192afd6745cb0e8eaf873e4443ac>:0) Aspose.Words.Document.Print (System.Drawing.Printing.PrinterSettings printerSettings) (at <4d44192afd6745cb0e8eaf873e4443ac>:0) Aspose.Words.Document.Print () (at <4d44192afd6745cb0e8eaf873e4443ac>:0)
  • 造成原因:
    缺少System.Drawing.dll
  • 解决办法:
    当前Unity版本编辑器下的System.Drawing.dll导入到Asserts中。

六、 Unity发布

6.4 Unity遇到的错误

  • 错误描述
    在unity Editor中正常运行,发布后提示错误。
  • 错误提示:
    NullReferenceException: Object reference not set to an instance of an object
    Rethrow as FileCorruptedException: The document appears to be corrupted and cannot be loaded.
  • 错误原因:
    缺少依赖包。
  • 解决办法:
  1. 首先确定ProjectSetting -> Player -> Other Settings -> Api Compatibility Level。
  2. 查找在Editor安装路径的对应库(I18N.*Aspose.word.dllSystem.Drawing.dll):
    .NET Standard 2.0:\Editor\Data\MonoBleedingEdge\lib\mono\2.0-api
    .NET 4.x:\Editor\Data\MonoBleedingEdge\lib\mono\4.5
  3. 其中 .NET Standard2.0 的System.Drawing.dll在使用过程中有点局限,支持不太好。
  4. 参考文章:https://blog.csdn.net/qq_37484084/article/details/126000485

猜你喜欢

转载自blog.csdn.net/l1179237106/article/details/129186943