【问题标题】:Aspose.Words save Word document as pdf loses formattingAspose.Words 将 Word 文档保存为 pdf 格式丢失
【发布时间】:2018-08-14 17:02:35
【问题描述】:

我正在使用 Aspose.Words for .NET 替换文档中的一些合并字段,然后将文件另存为 PDF,但是,由于转换为 PDF,我的格式变得混乱(即使对于非合并字段) (参考图片)。代码很简单,所以我看不到我缺少什么。

word文档,预处理:

生成的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;
    }

任何帮助将不胜感激。

【问题讨论】:

  • 你试过latest version of Aspose.Words for .NET18.8吗?如果问题仍然存在,请压缩并上传您的 1) 输入 Word 文档 2) Aspose.Words 18.8 生成的输出文件显示不希望的行为和 3) 简单的控制台应用程序(没有编译错误的源代码)到 Dropbox 并在此处共享下载链接以进行测试。我与 Aspose 一起担任开发人员宣传员。
  • @AwaisHafeez 感谢您的回复。我为您创建了这个 ZIP:dropbox.com/s/2jsxytfnkm07h26/AsposeWordsDemo.zip?dl=0 - 它包含输入文档 (ExampleDocument.docx)、生成的输出文件 (merged.pdf) 和控制台应用程序 (Program.cs)。
  • 出现问题是因为缺少字体。详情请参考this article。在您的情况下,请在执行此 Aspose.Words 代码的机器上安装“Verdana”、“Arial”和“Cambria”字体。只需将这些字体文件从 Windows 机器复制到其他机器即可。我与 Aspose 一起担任开发人员宣传员。
  • @AwaisHafeez 感谢您的回复。我刚刚确认这确实是我的 Mac 上缺少字体的问题。由于我正在构建的应用程序最终将在 Windows 上运行,这意味着没有问题需要修复。随时发表您的评论作为答案,我会接受。

标签: asp.net-core aspose aspose.words


【解决方案1】:

由于缺少字体而出现此问题。详情请参考以下文章。

How Aspose.Words Uses True Type Fonts

在您的情况下,您需要在执行此 Aspose.Words 代码的机器上安装“Verdana”、“Arial”和“Cambria”字体。只需将这些字体文件从 Windows 机器复制到其他 MAC 机器即可。

我与 Aspose 合作,担任开发人员宣传员。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多