Word转Pdf后公式内容显示不正常

你好,在使用Aspose.Words(v25.1.0)将Word文件转为Pdf时,在保存前执行了邮件合并操作后,公式内容显示不正常了。

源文件:
公式问题.docx (12.2 KB)

正常的效果:

异常的效果:

正常效果使用代码:

Document doc =  new Document(@"C:\Users\AKSO-HG\Downloads\公式问题.docx");
string pdfFilePath = @"C:\Users\AKSO-HG\Downloads\公式问题.pdf";
PdfSaveOptions saveOption = new PdfSaveOptions();
saveOption.SaveFormat = SaveFormat.Pdf;
doc.Save(pdfFilePath, saveOption);

异常效果使用代码:

Document doc =  new Document(@"C:\Users\AKSO-HG\Downloads\公式问题.docx");
string pdfFilePath = @"C:\Users\AKSO-HG\Downloads\公式问题.pdf";
PdfSaveOptions saveOption = new PdfSaveOptions();
saveOption.SaveFormat = SaveFormat.Pdf;
var keys = new string[] { "A" };
var vals = new string[] { "B" };
doc.MailMerge.Execute(keys, vals);
doc.Save(pdfFilePath, saveOption);

@cqhg90258 出现此问题的原因是您的文档中的“QOUTE”字段已更新。如果您在 MS Word 中更新它,您将得到相同的结果。您可以使用以下代码禁用文档中这些字段的更新:

Document doc = new Document(@"C:\Temp\in.docx");

// Lock QUOTE fields in the document to avoid updating them.
foreach (Field f in doc.Range.Fields)
{
    if (f.Type == FieldType.FieldQuote)
        f.IsLocked = true;
}

string pdfFilePath = @"C:\Temp\out.pdf";
PdfSaveOptions saveOption = new PdfSaveOptions();
saveOption.SaveFormat = SaveFormat.Pdf;
var keys = new string[] { "A" };
var vals = new string[] { "B" };
doc.MailMerge.Execute(keys, vals);
doc.Save(pdfFilePath, saveOption);

因为不能确定Word中是否所有FieldQuote都可以设置IsLocked为true,有没有办法保证不设置IsLocked的情况下,保存结果显示正常呢

@cqhg90258 不幸的是,没有办法避免这种情况。您所能做的就是锁定此字段或取消链接(删除字段,只留下文本).