我正在使用Aspose.Words for .NET替换文档中的某些合并字段,然后将文件另存为PDF,但是,转换为PDF时,我的格式变得混乱了(即使是非合并字段也是如此) (请参阅图像)。代码很简单,所以我看不到我缺少的东西。
如您所见,有些字段缩进了一些,而不是很好地对齐了。
我用于生成PDF和替换合并字段的代码是:
public async Task<Stream> GenerateContractAsync(string requestRegistrationId)
{
var requestRegistration = await _requestRegistrationRepository
.FindRequestRegistration(requestRegistrationId)
.Include(rr => rr.Request.QualityType)
.Include(rr => rr.User)
.SingleOrDefaultAsync();
var file = await _fileService
.LoadFileAsync("Concept contract.docx");
var user = requestRegistration.User;
var document = new Aspose.Words.Document(file);
document.MailMerge.Execute(
new[]
{
"EmployeeName", "EmployeeDateOfBirth", "EmployeePlaceOfBirth", "EmployeeSSN", "EmployeeCity",
"EmployeeAddress", "ContractStartDate", "EmployeeFunction", "HourlyWage", "WageDeductionApplied"
},
new object[]
{
user.FullName, $"{user.Birthday:dd-MM-yyyy}", "Oss", user.Bsn, user.City,
$"{user.PostalCode}, {user.City}", $"{requestRegistration.Request.StartDate:dd-MM-yyyy}",
requestRegistration.Request.QualityType.Name, $"{requestRegistration.Request.HourlyRate:C}",
user.PayrollTaxDiscountEnabled ? "Ja" : "Nee"
}
);
var mergedDocumentStream = new MemoryStream();
document.Save(mergedDocumentStream, SaveFormat.Pdf);
#if DEBUG
mergedDocumentStream.Seek(0, SeekOrigin.Begin);
await _fileService.SaveFileToDiskAsync($"{user.Id}-{DateTimeOffset.Now:g}.pdf", "", mergedDocumentStream);
#endif
mergedDocumentStream.Seek(0, SeekOrigin.Begin);
return mergedDocumentStream;
}
任何帮助将不胜感激。
答案 0 :(得分:1)
由于缺少字体而出现问题。有关详细信息,请参阅以下文章。
How Aspose.Words Uses True Type Fonts
在您的情况下,您需要在执行此Aspose.Words代码的计算机上安装“ Verdana”,“ Arial”和“ Cambria”字体。只需将这些字体文件从Windows计算机复制到其他MAC计算机即可。
我与Aspose一起担任开发人员推广人员。