C# 从目录中按章节拆分PDF

C# 从目录中按章节拆分PDF,c#,pdf,gembox-pdf,C#,Pdf,Gembox Pdf,我正在使用,我需要将PDF文件中的各个章节提取为单独的PDF文件 第一页(也可能是第二页)包含TOC(目录),我需要根据它拆分其余的PDF页面: 此外,那些被拆分的PDF文档应按其包含的章节命名。 我可以根据每个文档的页数拆分PDF(我使用以下方法计算得出): 使用(var source=PdfDocument.Load(“Chapters.pdf”)) { int pagesPerSplit=3; int count=source.Pages.count; for(int index=1;i

我正在使用,我需要将PDF文件中的各个章节提取为单独的PDF文件

第一页(也可能是第二页)包含TOC(目录),我需要根据它拆分其余的PDF页面:

此外,那些被拆分的PDF文档应按其包含的章节命名。
我可以根据每个文档的页数拆分PDF(我使用以下方法计算得出):

使用(var source=PdfDocument.Load(“Chapters.pdf”))
{
int pagesPerSplit=3;
int count=source.Pages.count;
for(int index=1;index

但是我不知道如何阅读和处理TOC,并根据其项目合并章节拆分。

您应该遍历文档的书签()并根据书签目标页拆分它

例如,尝试以下方法:

using (var source = PdfDocument.Load("Chapters.pdf"))
{
    PdfOutlineCollection outlines = source.Outlines;

    PdfPages pages = source.Pages;
    Dictionary<PdfPage, int> pageIndexes = pages
        .Select((page, index) => new { page, index })
        .ToDictionary(item => item.page, item => item.index);

    for (int index = 0, count = outlines.Count; index < count; ++index)
    {
        PdfOutline outline = outlines[index];
        PdfOutline nextOutline = index + 1 < count ? outlines[index + 1] : null;

        int pageStartIndex = pageIndexes[outline.Destination.Page];
        int pageEndIndex = nextOutline != null ?
            pageIndexes[nextOutline.Destination.Page] :
            pages.Count;

        using (var destination = new PdfDocument())
        {
            while (pageStartIndex < pageEndIndex)
            {
                destination.Pages.AddClone(pages[pageStartIndex]);
                ++pageStartIndex;
            }

            destination.Save($"{outline.Title}.pdf");
        }
    }
}
using (var source = PdfDocument.Load("Chapters.pdf"))
{
    PdfOutlineCollection outlines = source.Outlines;

    PdfPages pages = source.Pages;
    Dictionary<PdfPage, int> pageIndexes = pages
        .Select((page, index) => new { page, index })
        .ToDictionary(item => item.page, item => item.index);

    for (int index = 0, count = outlines.Count; index < count; ++index)
    {
        PdfOutline outline = outlines[index];
        PdfOutline nextOutline = index + 1 < count ? outlines[index + 1] : null;

        int pageStartIndex = pageIndexes[outline.Destination.Page];
        int pageEndIndex = nextOutline != null ?
            pageIndexes[nextOutline.Destination.Page] :
            pages.Count;

        using (var destination = new PdfDocument())
        {
            while (pageStartIndex < pageEndIndex)
            {
                destination.Pages.AddClone(pages[pageStartIndex]);
                ++pageStartIndex;
            }

            destination.Save($"{outline.Title}.pdf");
        }
    }
}
destination.Save($"{outline.Title.Substring(outline.Title.IndexOf(' ') + 1)}.pdf");