Bookmark examples
Examples written in answer to questions such as:
fetchbookmarktitles:
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.interactive;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfOutline;
import com.itextpdf.kernel.pdf.PdfReader;
import java.io.File;
import java.io.Writer;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
public class FetchBookmarkTitles {
public static final String DEST = "./target/txt/bookmarks.txt";
public static final String SRC = "./src/main/resources/pdfs/bookmarks.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FetchBookmarkTitles().manipulatePdf(DEST);
}
public void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));
// This method returns a complete outline tree of the whole document.
// If the flag is false, the method gets cached outline tree (if it was cached
// via calling getOutlines method before).
PdfOutline outlines = pdfDoc.getOutlines(false);
List<PdfOutline> bookmarks = outlines.getAllChildren();
pdfDoc.close();
List<String> titles = new ArrayList<>();
for (PdfOutline bookmark : bookmarks) {
addTitle(bookmark, titles);
}
// See title's names in the console
for (String title : titles) {
System.out.println(title);
}
createResultTxt(dest, titles);
}
/*
* This recursive method calls itself if an examined bookmark entry has kids.
* The method writes bookmark title to the passed list
*/
private void addTitle(PdfOutline outline, List<String> result) {
String bookmarkTitle = outline.getTitle();
result.add(bookmarkTitle);
List<PdfOutline> kids = outline.getAllChildren();
if (kids != null) {
for (PdfOutline kid : kids) {
addTitle(kid, result);
}
}
}
private void createResultTxt(String dest, List<String> titles) throws IOException {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) {
for (int i = 0; i < titles.size(); i++) {
writer.write("Title " + i + ": " + titles.get(i) + "\n");
}
}
}
}
C#
C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Pdf;
namespace iText.Samples.Sandbox.Interactive
{
public class FetchBookmarkTitles
{
public static readonly String DEST = "results/txt/bookmarks.txt";
public static readonly String SRC = "../../../resources/pdfs/bookmarks.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new FetchBookmarkTitles().ManipulatePdf(DEST);
}
public void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));
// This method returns a complete outline tree of the whole document.
// If the flag is false, the method gets cached outline tree (if it was cached
// via calling getOutlines method before).
PdfOutline outlines = pdfDoc.GetOutlines(false);
IList<PdfOutline> bookmarks = outlines.GetAllChildren();
pdfDoc.Close();
List<String> titles = new List<String>();
foreach (PdfOutline bookmark in bookmarks)
{
AddTitle(bookmark, titles);
}
// See title's names in the console
foreach (String title in titles)
{
Console.WriteLine(title);
}
CreateResultTxt(dest, titles);
}
// This recursive method calls itself if an examined bookmark entry has kids.
// The method writes bookmark title to the passed list
private void AddTitle(PdfOutline outline, List<String> result)
{
String bookmarkTitle = outline.GetTitle();
result.Add(bookmarkTitle);
IList<PdfOutline> kids = outline.GetAllChildren();
if (kids != null)
{
foreach (PdfOutline kid in kids)
{
AddTitle(kid, result);
}
}
}
private void CreateResultTxt(String dest, List<String> titles)
{
using (StreamWriter writer = new StreamWriter(dest))
{
for (int i = 0; i < titles.Count; i++)
{
writer.Write("Title " + i + ": " + titles[i] + "\n");
}
}
}
}
}
changebookmarks
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.stamper;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfOutline;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import java.io.File;
import java.util.List;
public class ChangeBookmarks {
public static final String DEST = "./target/sandbox/stamper/change_bookmarks.pdf";
public static final String SRC = "./src/main/resources/pdfs/bookmarks.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ChangeBookmarks().manipulatePdf(DEST);
}
public void changeList(List<PdfOutline> list) {
for (PdfOutline entry : list) {
PdfArray array = ((PdfArray) entry.getContent().get(PdfName.Dest));
for (int i = 0; i < array.size(); i++) {
if (PdfName.Fit.equals(array.get(i))) {
array.set(i, PdfName.FitV);
array.add(new PdfNumber(60));
}
}
}
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfOutline outlines = pdfDoc.getOutlines(false);
List<PdfOutline> children = outlines.getAllChildren().get(0).getAllChildren();
changeList(children);
pdfDoc.close();
}
}
C#
C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Pdf;
namespace iText.Samples.Sandbox.Stamper
{
public class ChangeBookmarks
{
public static readonly String DEST = "results/sandbox/stamper/change_bookmarks.pdf";
public static readonly String SRC = "../../../resources/pdfs/bookmarks.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new ChangeBookmarks().ManipulatePdf(DEST);
}
public void ChangeList(IList<PdfOutline> list)
{
foreach (PdfOutline entry in list)
{
PdfArray array = ((PdfArray)entry.GetContent().Get(PdfName.Dest));
for (int i = 0; i < array.Size(); i++)
{
if (PdfName.Fit.Equals(array.Get(i)))
{
array.Set(i, PdfName.FitV);
array.Add(new PdfNumber(60));
}
}
}
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfOutline outlines = pdfDoc.GetOutlines(false);
IList<PdfOutline> children = outlines.GetAllChildren()[0].GetAllChildren();
ChangeList(children);
pdfDoc.Close();
}
}
}