Aspose.words将文档转为pdf的时候,处理下划线出现异常

首先提供原稿的word文件和使用aspose.words转换的pdf文件:

word文件是原稿,pdf是使用aspose.words转换来的

files.zip (45.0 KB)

主要问题如下:

1、原稿word的文档样式,下划线都是正常的

2、使用aspose转换后的pdf样式,可以看到下划线空白地方被自动去除了

这个是aspose本身原因还是有参数需要调整?

我使用的代码片段:

public static void ConvertWord2Pdf(
    string documentFileName,
    string SavePdfName,
    string tempSaveDocxPath,
    Aspose.Words.Saving.PdfCompliance PdfComplanceOption,
    Aspose.Words.Settings.MsWordVersion OptiMizMswVersion,
    bool UpdateField,
    Aspose.Words.Saving.ColorMode SavePdfColorMode,
    bool EmbedFonts,
    string DocumentPassword,
    bool UseLocalOffice)
{
    string DocumentName, SaveDocxName;

    if (!UseLocalOffice)
    {
        //设置加载word文档的时候参数
        Aspose.Words.Loading.LoadOptions TempDocuemntLoadOption = new Aspose.Words.Loading.LoadOptions()
        {
            ConvertMetafilesToPng = false,
            MswVersion = OptiMizMswVersion,
            Password = DocumentPassword,
        };

        TempDocuemntLoadOption.LanguagePreferences
            .AddEditingLanguage(Aspose.Words.Loading.EditingLanguage.ChinesePRC);

        AspWordsWarningCallBack AspCallBack = new AspWordsWarningCallBack();

        Aspose.Words.Fonts.FontSettings TempFontSettings = new Aspose.Words.Fonts.FontSettings();
        TempFontSettings.SubstitutionSettings.DefaultFontSubstitution.Enabled = true;
        TempFontSettings.SubstitutionSettings.TableSubstitution.Enabled = false;
        TempFontSettings.SubstitutionSettings.FontInfoSubstitution.Enabled = false;
        TempFontSettings.SubstitutionSettings.FontConfigSubstitution.Enabled = false;
        TempFontSettings.SubstitutionSettings.FontNameSubstitution.Enabled = false;
        TempFontSettings.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "SimSun";


        TempDocuemntLoadOption.FontSettings = TempFontSettings;


        //设置PDF保存参数
        Aspose.Words.Saving.PdfSaveOptions TempPdfaveOption = new Aspose.Words.Saving.PdfSaveOptions()
        {
            AllowEmbeddingPostScriptFonts = true,
            EmbedFullFonts = EmbedFonts,
            FontEmbeddingMode = Aspose.Words.Saving.PdfFontEmbeddingMode.EmbedAll,
            ColorMode = SavePdfColorMode,
            ImageColorSpaceExportMode = Aspose.Words.Saving.PdfImageColorSpaceExportMode.Auto,
            ImageCompression = Aspose.Words.Saving.PdfImageCompression.Auto,
            PageMode = Aspose.Words.Saving.PdfPageMode.UseOutlines,
            ZoomBehavior = Aspose.Words.Saving.PdfZoomBehavior.None,
            SaveFormat = Aspose.Words.SaveFormat.Pdf,
            UseHighQualityRendering = true,
            UseAntiAliasing = true,
            PreblendImages = true,
            JpegQuality = 100,
            Compliance = PdfComplanceOption,
            ExportGeneratorName = false,
            UpdateFields = UpdateField,
        };

        //设置PDF保存带word中的目录
        TempPdfaveOption.OutlineOptions.DefaultBookmarksOutlineLevel = 0;
        TempPdfaveOption.OutlineOptions.CreateMissingOutlineLevels = false;
        TempPdfaveOption.OutlineOptions.ExpandedOutlineLevels = 3;
        TempPdfaveOption.OutlineOptions.HeadingsOutlineLevels = 9;
        TempPdfaveOption.HeaderFooterBookmarksExportMode = Aspose.Words.Saving.HeaderFooterBookmarksExportMode.All;


        //设置Docx保存参数
        Aspose.Words.Saving.OoxmlSaveOptions TempDocxSaveOption = new Aspose.Words.Saving.OoxmlSaveOptions()
        {
            Compliance = Aspose.Words.Saving.OoxmlCompliance.Iso29500_2008_Transitional,
            SaveFormat = Aspose.Words.SaveFormat.Docx,
            KeepLegacyControlChars = true,
            UpdateFields = UpdateField,
        };

        Aspose.Words.Document TempDocument = new Aspose.Words.Document(documentFileName, TempDocuemntLoadOption)
        {
            RemovePersonalInformation = true,
        };

        TempDocument.CompatibilityOptions.OptimizeFor(OptiMizMswVersion);
        TempDocument.CompatibilityOptions.BalanceSingleByteDoubleByteWidth = true;//区分全角和半角字符

        DocumentName = System.IO.Path.GetFileNameWithoutExtension(documentFileName);

        SaveDocxName = System.IO.Path.Combine(tempSaveDocxPath, $"{DocumentName}.docx");


        TempDocument.Save(SaveDocxName, TempDocxSaveOption);

        Aspose.Words.Document TempDocxDocument = new Aspose.Words.Document(SaveDocxName, TempDocuemntLoadOption);

        TempDocxDocument.WarningCallback = AspCallBack;

        //TempDocxDocument.FontSettings = TempFontSettings;
        //TempDocxDocument.LayoutOptions.KeepOriginalFontMetrics = true;
        TempDocxDocument.Save(SavePdfName, TempPdfaveOption);

@jidesheng 遗憾的是,我无法用最新的 Aspose.Words 库重现这个问题。请告诉我您使用的是什么版本的 Aspose.Words?

24.7.0 会不会是因为某些加载参数导致的?比如我代码中的load设置为2010样式?

@jidesheng 是的,使用 OptimizeFor 导致了问题。

感谢您报告此问题。 我们已经在我们的内部问题跟踪系统中打开了以下新工单,并将根据 免费支持政策 中提到的条款提供它们的修复:

Issue ID(s): WORDSNET-27205

如果您需要优先支持以及直接联系我们的付费支持管理团队,您可以获得 付费支持服务

@jidesheng 我们已完成问题分析,建议您使用

doc.CompatibilityOptions.UlTrailSpace = true;

设置 OptimizeFor 之后。

谢谢,使用后问题已解决,请问这个是不是就不属于aspose.words本身的问题了?只是对应设置没打开?

@jidesheng 根据分析,MS Word 和 Aspose.Words 的行为存在一些差异。在使用 CompatibilityOptions.OptimizeFor() 时,我们会清除所有现有选项,但 MS Word 不会清除先前设置版本中的现有(非冲突)选项。因此,在这种情况下,我们建议您采用变通方法来解决问题。