请问,在使用Aspose.PDF 21.7版本合并PDF,生成书签和目录时,部分中文乱码问题。方法代码如下

private void MergePdfTogeter(bool createBookmarks = false)
{
if (listBox1.Items.Count > 0)
{
Document document = new Document();
//存放书签的页面
List ints = new List();
int currentPage = 1;
foreach (var item in fileNameWithPostfix)
{
Document tempDoc = new Document(item);
document.Pages.Add(tempDoc.Pages);
if (createBookmarks == true)
{
string bookmarkTitle = System.IO.Path.GetFileNameWithoutExtension(item);
OutlineItemCollection bookmark = new OutlineItemCollection(document.Outlines)
{
Title = bookmarkTitle,
Action = new GoToAction(document.Pages[currentPage])
};
ints.Add(currentPage);
document.Outlines.Add(bookmark);
currentPage += document.Pages.Count;
}
}

     if (string.IsNullOrEmpty(fileNameSaveAs))
     {
         MessageBox.Show("请输入要保存的文件名");
     }
     else
     {
         document.Save(fileNameSaveAs);
     }
     if (createBookmarks == true)
     {
         // 创建新文档并插入目录页
         Document newdocument = new Document(fileNameSaveAs);
         Aspose.Pdf.Page tocPage = newdocument.Pages.Insert(1);

         TocInfo tocInfo = new TocInfo();

         // 创建目录标题
         TextFragment title = new TextFragment("目录")
         {
             TextState = { FontSize = 20, FontStyle = FontStyles.Bold, Font = FontRepository.FindFont("SimSun") }
         };                    
         title.TextState.Font.IsEmbedded = true;
         tocInfo.Title = title;
         tocPage.TocInfo = tocInfo;

         // 创建目录项
         for (int i = 0; i < fileNameWithPostfix.Length; i++)
         {
             var heading = new Aspose.Pdf.Heading(1)
             { TocPage = tocPage, DestinationPage = newdocument.Pages[i + 2], Top = newdocument.Pages[i + 2].Rect.Height };

             var segment = new TextSegment
             {
                 Text = Path.GetFileNameWithoutExtension(fileNameWithPostfix[i]),
                 TextState = { Font = FontRepository.FindFont("SimSun"), FontSize = 12 }
             };

             heading.Segments.Add(segment);

             tocPage.Paragraphs.Add(heading);
         }
         newdocument.Save("mergedOutput_withToc.pdf");

         // 保存输出
     }
     MessageBox.Show($"Pdf合并完成,共合并{listBox1.Items.Count}份文件");
 }

}

@wgewfndx

请您确认一下

  • 系统中已正确安装并存在支持汉字的字体
  • 另外,请确保您使用的是24.11版本的API

如果问题仍然存在,请分享一些示例文件以及最少的代码片段来重现问题,以便我们可以在我们的环境中测试该场景并相应地解决它。

试剂领用流程.pdf (34.4 KB)

下面是示例代码,其中的文档路径需要换成您本地测试的路径,感谢!
Document document = new Document(@“C:\Users\wgewf_zomh3mr\Desktop\324\试剂领用流程.pdf”);
Aspose.Pdf.Page tocPage = document.Pages.Insert(1);
TocInfo tocInfo = new TocInfo();
// 创建目录标题
TextFragment title = new TextFragment(“目录”)
{
TextState = { FontSize = 20, FontStyle = FontStyles.Bold, Font = FontRepository.FindFont(“SimSun”) }
};
title.TextState.Font.IsEmbedded = true;
tocInfo.Title = title;
tocPage.TocInfo = tocInfo;
// 创建目录项
var heading = new Aspose.Pdf.Heading(1)
{ TocPage = tocPage, DestinationPage = document.Pages[1], Top = document.Pages[1].Rect.Height };
var segment = new TextSegment
{
Text = “试剂领用临时流程”,
TextState = { Font = FontRepository.FindFont(“SimSun”), FontSize = 12 }
};
segment.TextState.Font.IsEmbedded = true;
heading.Segments.Add(segment);
tocPage.Paragraphs.Add(heading);

document.Save(@“C:\Users\wgewf_zomh3mr\Desktop\324\添加后的试剂领用流程.pdf”);

@wgewfndx

您是否确定所有 Windows 字体和中文字体(例如: SimSun 是否正确安装在您的系统中?我们使用 24.11 版本的 API 进行了测试,并获得了附加的输出 PDF,看起来不错。
添加后的试剂领用流程.pdf (176.3 KB)