Aspose.Cells 生成“多个一级书签”的pdf

@Peyton.Xu
aspose.cells还有个问题麻烦您一下。
我这有个excel文件,是由两个文件合并而成,被合并的两个文件里面的工作表个数、名称基本上一致。现在要把此文件转为pdf文件,并且自动添加pdf书签。书签编写要求:当工作表名中包含“封面”,则此书签为一级书签,之后的工作表为二级书签,直到碰到 工作表名包含”封面“,再改为一级书签,…如此操作。
我写了如下代码,第一个”封面“书签可实现一级书签,之后的一级书签不会实现,麻烦帮我看看是什么原因。我估计把PdfBookmarkEntry用法想得太简单了。

void ExcelToPdf(string path, string newFilePath) //生成文本格式的PDF文件
{
ArrayList lst = new ArrayList();
Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook(path);
Worksheet sheet = excel.Worksheets[0];
Aspose.Cells.Rendering.PdfBookmarkEntry PdfBookmark = new Aspose.Cells.Rendering.PdfBookmarkEntry();

        for (int i = 0; i < excel.Worksheets.Count-1; i++) 
        {
            sheet = excel.Worksheets[i];
            if (sheet.Name.Contains("封面"))  //一级书签
            {
               // PdfBookmark = new Aspose.Cells.Rendering.PdfBookmarkEntry();
                PdfBookmark.Destination = sheet.Cells["A1"];
                PdfBookmark.Text = sheet.Cells["C7"].Value.ToString();
            }

            else  //二级书签
            {
                Aspose.Cells.Rendering.PdfBookmarkEntry subPdfBookmark = new Aspose.Cells.Rendering.PdfBookmarkEntry();
                subPdfBookmark.Destination = sheet.Cells["A1"];
                subPdfBookmark.Text = sheet.Cells["A1"].Value.ToString();
                lst.Add(subPdfBookmark);
            }

        }
        PdfBookmark.SubEntry = lst;
        PdfSaveOptions opts = new PdfSaveOptions();
        opts.Bookmark = PdfBookmark;
        excel.Save(newFilePath, opts);  

    }

报价合并文件(包).zip (30.7 KB)

@sweetime
请尝试以下代码,rootBookmark.Text = ""; 是为了生成多个一级书签:

void ExcelToPdf(string path, string newFilePath) //生成文本格式的PDF文件
{
    Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook(path);
    Worksheet sheet = excel.Worksheets[0];
    Aspose.Cells.Rendering.PdfBookmarkEntry rootBookmark = new Aspose.Cells.Rendering.PdfBookmarkEntry();
    //set to Empty or null
    rootBookmark.Text = "";
    rootBookmark.SubEntry = new ArrayList();

    Aspose.Cells.Rendering.PdfBookmarkEntry currentLevel1BookmarkEntry = null;
    //the last sheet is excluded?
    for (int i = 0; i < excel.Worksheets.Count - 1; i++)
    {
        sheet = excel.Worksheets[i];
        if (sheet.Name.Contains("封面"))  //一级书签
        {
            PdfBookmarkEntry leve1Bookmark = new Aspose.Cells.Rendering.PdfBookmarkEntry();
            leve1Bookmark.Destination = sheet.Cells["A1"];
            leve1Bookmark.Text = sheet.Cells["C7"].Value.ToString();
            leve1Bookmark.SubEntry = new ArrayList();

            rootBookmark.SubEntry.Add(leve1Bookmark);
            currentLevel1BookmarkEntry = leve1Bookmark;
        }

        else  //二级书签
        {
            if (currentLevel1BookmarkEntry != null)
            {
                Aspose.Cells.Rendering.PdfBookmarkEntry level2Bookmark = new Aspose.Cells.Rendering.PdfBookmarkEntry();
                level2Bookmark.Destination = sheet.Cells["A1"];
                level2Bookmark.Text = sheet.Cells["A1"].Value.ToString();

                currentLevel1BookmarkEntry.SubEntry.Add(level2Bookmark);
            }
            
        }

    }
    PdfSaveOptions opts = new PdfSaveOptions();
    opts.Bookmark = rootBookmark;
    excel.Save(newFilePath, opts);
}

@Peyton.Xu
完美,非常感谢。