Skip to main content
Skip table of contents

Removing Items from a PDF's Outline Tree

Introduction

A PDF's document outline tree (otherwise known as its bookmarks) serves as an important source of shortcuts for users to navigate across their documents. With iText Core version 7.1.12, we have added functionality for removing elements from an existing PDF's outline tree. Previously, there existed full functionality for adding and reading outlines, but you now have full control over the structure.

Removing the entire Outline Tree

Below is the outline tree of our input document which we will be modifying (displayed via Adobe Acrobat):


The following code snippet removes all contents from that outline tree:

JAVA

JAVA
package support.JiraArticles;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;
import java.io.IOException;

public class removeAllOutlines {

    public static final String DEST = "results/outlinechanged.pdf";

    public static void main(String[] args) throws IOException, InterruptedException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new removeOutlines().removeAllOutlines(DEST);
    }
    
    public void removeWholeOutlineSubtree(String DEST) throws IOException {
        String input = "resources/example_document_2.pdf";
        PdfReader reader = new PdfReader(input);
        PdfWriter writer = new PdfWriter(DEST);
        PdfDocument pdfDocument = new PdfDocument(reader, writer);
        pdfDocument.getOutlines(true).removeOutline();
        pdfDocument.close();

    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Pdf;

namespace example_2
{
    public class RemoveAllOutlines {
        public const String DEST = "../../results/removedAllutlinePdf.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new RemoveAllOutlines().RemoveWholeOutlineSubtree(DEST);
        }
        public virtual void RemoveWholeOutlineSubtree(String DEST)
        {
            String input = sourceFolder + "my_output_pdf.pdf";
            PdfReader reader = new PdfReader(input);
            PdfWriter writer = new PdfWriter(DEST);
            PdfDocument pdfDocument = new PdfDocument(reader, writer);
            pdfDocument.GetOutlines(true).RemoveOutline();
            pdfDocument.Close();
            pdfDocument.Close();
        }
    }
}

Here you can see a screenshot of the same document with the outline tree removed:


Removing Parts of an Outline Tree

We have also added functionality for removing specific sections of the outline tree. The following is a code sample which removes the first subtree within the outline tree.

JAVA

JAVA
package support.JiraArticles;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfOutline;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;
import java.io.IOException;


public class removeOneOutline {

    public static final String DEST = "results/outlinechanged.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new removeOneOutline().removeOneOutline(DEST);
    }

    public void removeOneOutlineSubtree(String DEST) throws IOException {
        String input = "resources/example_document_2.pdf";
        PdfReader reader = new PdfReader(input);
        PdfWriter writer = new PdfWriter(DEST);
        PdfDocument pdfDocument = new PdfDocument(reader, writer);
        PdfOutline root = pdfDocument.getOutlines(true);
        PdfOutline toRemove = root.getAllChildren().get(0);
        toRemove.removeOutline();
        pdfDocument.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Pdf;

namespace example_1 
{
    public class RemoveOneOutline {
        public const String DEST = "../../results/removedOutlinePdf.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new RemoveOneOutline().RemoveOneOutlineSubtree(DEST);
        }
        public virtual void RemoveOneOutlineSubtree(String DEST)
        {
            String input = sourceFolder + "my_output_pdf.pdf";
            PdfReader reader = new PdfReader(input);
            PdfWriter writer = new PdfWriter(DEST);
            PdfDocument pdfDocument = new PdfDocument(reader, writer);
            PdfOutline root = pdfDocument.GetOutlines(true);
            PdfOutline toRemove = root.GetAllChildren().Get(0);
            toRemove.RemoveOutline();
            pdfDocument.Close();
        }
    }
}

Here is the outline tree after that code is executed:


Attached is the example PDF document used as input for the above code snippets.

example_document_2.pdf


JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.