Add Footnotes and Endnotes in PDF using Java.

A footnote is a note or text given at the bottom of a specific page, whereas, the endnote is a note placed at the end of a document. We usually use footnotes or endnotes for references, explanations, or comments in the document. We can add footnotes/endnotes to the PDF document programmatically. In this article, we will learn how to add footnotes and endnotes in PDF documents using Java.

The following topics shall be covered in this article:

Java API to Add Footnotes and Endnotes in PDF

For adding footnotes and endnotes to a PDF document, we will be using Aspose.PDF for Java API. It allows us to generate, modify, convert, render, secure and print supported documents without using Adobe Acrobat. Please either download the JAR of the API or just add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>22.1</version>
</dependency>

Add Footnotes to a PDF using Java

We can add footnotes at the end of the page in a PDF document by following the steps given below:

  1. Firstly, create an instance of the Document class.
  2. Next, add a Page to the PagesCollection of the document.
  3. Then, create a TextFragment.
  4. Next, set the Footnote value for the TextFragment object.
  5. Then, add TextFragment to Paragraphs collection
  6. Optionally, repeat above steps to add more TextFragments for multiple Footnote values.
  7. Finally, save the PDF file using the Document.Save() method. It takes the path of the output file as an argument.

The following code sample shows how to add footnotes to a PDF document using Java.

// This code example demonstrates how to add footnotes to a PDF document.
// Create an instance of Document
Document document = new Document();
// Add page to pages collection of PDF
Page page = document.getPages().add();
// Create a TextFragment
TextFragment text = new TextFragment("Hello World");
// Set Footnote value for TextFragment
text.setFootNote(new Note("foot note for Hello World!"));
// Add TextFragment to paragraphs collection of first page of document
page.getParagraphs().add(text);
// Create another TextFragment
text = new TextFragment("Aspose.Pdf for Java");
// Set Footnote for second TextFragment
text.setFootNote(new Note("foot note for second text fragment!"));
// Add second text fragment to paragraphs collection of PDF file
page.getParagraphs().add(text);
// Save the document
document.save("C:\\Files\\PDF\\sample_footnote.pdf");
Add Footnotes to a PDF using Java.

Add Footnotes to a PDF using Java.

Add an Image to Footnote in PDF

We can add an image in the footnotes of a PDF document by following the steps given below:

  1. Firstly, create an instance of the Document class.
  2. Next, add a Page to the PagesCollection of the document.
  3. Then, create a TextFragment.
  4. Next, set the Footnote value for the TextFragment object.
  5. Then, add an image to the TextFragment object.
  6. After that, add TextFragment to Paragraphs collection
  7. Finally, save the PDF file using the Document.Save() method. It takes the path of the output file as an argument.

The following code sample shows how to add an image in the footnotes of a PDF document using Java.

// This code example demonstrates how to insert image to footnotes.
// Create an instance of Document
Document document = new Document();
// Add page to pages collection of PDF
Page page = document.getPages().add();
// Create a TextFragment
TextFragment text = new TextFragment("Hello World");
// Set Footnote value for TextFragment
text.setFootNote(new Note());
// Add image
Image image = new Image();
image.setFile("C:\\Files\\PDF\\aspose_logo.jpg");
image.setFixHeight(20);
text.getFootNote().getParagraphs().add(image);
// Create a text fragment
TextFragment footNote = new TextFragment(" foot note for Hello World!");
footNote.getTextState().setFontSize(20);
footNote.setInLineParagraph(true);
text.getFootNote().getParagraphs().add(footNote);
// Add TextFragment to paragraphs collection of first page of document
page.getParagraphs().add(text);
// Save the document
document.save("C:\\Files\\PDF\\image_footnote.pdf");
Add an Image to Footnote in PDF.

Add an Image to Footnote in PDF.

Insert a Table to Footnote in PDF

We can also add a table in the footnotes of a PDF document by following the steps given below:

  1. Firstly, create an instance of the Document class.
  2. Next, add a Page to the PagesCollection of the document.
  3. Then, create a TextFragment.
  4. Next, set the Footnote value for the TextFragment object.
  5. Then, add a table to the TextFragment object.
  6. After that, add TextFragment to Paragraphs collection
  7. Finally, save the PDF file using the Document.Save() method. It takes the path of the output file as an argument.

The following code sample shows how to add a table in the footnotes of a PDF document using Java.

// This code example demonstrates how to add table to footnotes.
// Create Document instance
Document document = new Document();
// Add page to pages collection of PDF
Page page = document.getPages().add();
// Create TextFragment instance
TextFragment text = new TextFragment("Hello World");
// Set Footnote value for TextFragment
text.setFootNote(new Note());
// Insert table
Table table = new Table();
table.getRows().add().getCells().add().getParagraphs().add(new TextFragment("Row 1 Cell 1"));
table.getRows().get_Item(0).getCells().add().getParagraphs().add(new TextFragment("Row 1 Cell 2"));
table.getRows().add().getCells().add().getParagraphs().add(new TextFragment("Row 2 Cell 1"));
table.getRows().get_Item(1).getCells().add().getParagraphs().add(new TextFragment("Row 2 Cell 2"));
text.getFootNote().getParagraphs().add(table);
// Add TextFragment to paragraphs collection of first page of document
page.getParagraphs().add(text);
// Save the document
document.save("C:\\Files\\PDF\\Table_footnote.pdf");
Insert a Table to Footnote in PDF.

Insert a Table to Footnote in PDF.

Customize Footnote Label and Line Style using Java

We can customize the label of footnotes and footnote line style in a PDF document by following the steps given below:

  1. Firstly, create an instance of the Document class.
  2. Next, add a Page to the PagesCollection of the document.
  3. Then, initialize a GraphInfo object to customize the line style.
  4. Set GraphInfo object properties such as LineWidth, Color, DashArray, etc.
  5. Next, create a TextFragment.
  6. Then, set the Footnote value for the TextFragment object.
  7. Next, initialize TextState object and set various properties e.g., ForegroundColor, FontStyle, etc.
  8. After that, add TextFragment to Paragraphs collection
  9. Optionally, repeat above steps to add more TextFragments for multiple Footnote values.
  10. Finally, save the PDF file using the Document.Save() method. It takes the path of the output file as an argument.

The following code sample shows how to add customize the footnotes label and line style using Java.

// This code example demonstrates how to customize footnotes label and line style.
// Create Document instance
Document document = new Document();
// Add page to pages collection of PDF
Page page = document.getPages().add();
// Customize Line style
// create GraphInfo object
GraphInfo graph = new GraphInfo();
// set line width as 2
graph.setLineWidth(2);
// set the color for graph object
graph.setColor(Color.getRed());
// set dash array value as 3
graph.setDashArray(new int[] { 3 });
// set dash phase value as 1
graph.setDashPhase(1);
// set footnote line style for page as graph
page.setNoteLineStyle(graph);
// Create TextFragment instance
TextFragment text = new TextFragment("Hello World");
// Set Footnote value for TextFragment
text.setFootNote(new Note("foot note for Hello World!"));
// Customize label
text.getFootNote().setText("FOOTNOTE-1");
TextState ts = new TextState();
ts.setForegroundColor(Color.getBlue());
ts.setFontStyle(FontStyles.Italic);
text.getFootNote().setTextState(ts);
// Add TextFragment to paragraphs collection of first page of document
page.getParagraphs().add(text);
// Create another TextFragment
text = new TextFragment("Aspose.Pdf for Java");
// Set Footnote for second text fragment
text.setFootNote(new Note("foot note for second text fragment!"));
// Add second text fragment to paragraphs collection of PDF file
page.getParagraphs().add(text);
// Save the document
document.save("C:\\Files\\PDF\\customize_footnote.pdf");
Customize Footnote Label and Line Style using Java.

Customize Footnote Label and Line Style using Java.

Add Footnotes to Existing PDF using Java

We can add footnotes to an existing PDF document by following the steps given below:

  1. Firstly, load a PDF file using the Document class.
  2. Next, get a particular page by its index.
  3. Then, create a TextFragmentAbsorber object with search phrase as input.
  4. After that, call the accept() method to search the input phrase from the page.
  5. Next, get the first occurrence of search phrase from TextFragmentCollection.
  6. Then, create an empty TextFragment and set its position on the page.
  7. Also, set the Footnote value for the TextFragment object.
  8. After that, add TextFragment to Paragraphs collection
  9. Finally, save the PDF file using the Document.Save() method. It takes the path of the output file as an argument.

The following code sample shows how to add footnotes to an existing PDF document using Java.

// This code example demonstrates how to add Footnotes to an existing PDF document.
// Load an existing PDF document
Document document = new Document("C:\\Files\\PDF\\sample.pdf");
// Create TextAbsorber object to find all instances of the text phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Class");
// Get particular page
Page page = document.getPages().get_Item(2);
// Accept the absorber for second page of document
page.accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Get first occurrence of the text
TextFragment textFragment = textFragmentCollection.get_Item(1);
// Create an empty TextFragment
TextFragment text = new TextFragment("");
// Set Position
Position position = textFragment.getPosition();
position = new Position(position.getXIndent() + 26, position.getYIndent());
text.setPosition(position);
// Set Footnote value for TextFragment
text.setFootNote(new Note("This is example footnote added in an existing PDF!"));
// Add second text fragment to paragraphs collection of PDF file
page.getParagraphs().add(text);
// Save the document
document.save("C:\\Files\\PDF\\Text_Added.pdf");
Add-Footnotes-to-Existing-PDF-using-Java

Add Footnotes to Existing PDF using Java.

Add Endnotes to PDF using Java

We can also add endnotes at the end of a PDF document by following the steps given below:

  1. Firstly, create an instance of the Document class.
  2. Next, add a Page to the PagesCollection of the document.
  3. Then, create a TextFragment.
  4. Next, set the Endnote value for the TextFragment object.
  5. Optionally, set a custom label for the Endnote.
  6. After that, add TextFragment to Paragraphs collection.
  7. Finally, save the PDF file using the Document.Save() method. It takes the path of the output file as an argument.

The following code sample shows how to add endnotes to a PDF document using Java.

// This code example demonstrates how to add Endnotes to a PDF document.
// Create a Document instance
Document doc = new Document();
// Add page to pages collection of PDF
Page page = doc.getPages().add();
// Create TextFragment instance
TextFragment text = new TextFragment("Hello World");
// Set FootNote value for TextFragment
text.setEndNote(new Note("sample End note"));
// Specify custom label for FootNote
text.getEndNote().setText(" Aspose(2015)");
// Add TextFragment to paragraphs collection of first page of document
page.getParagraphs().add(text);
// Save the PDF file
doc.save("C:\\Files\\PDF\\EndNote.pdf");

Get a Free API License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to:

  • add footnotes and endnotes to a PDF document using Java;
  • add an image or a table to footnotes in PDF;
  • customize the label of footnote and line style using Java;
  • add footnotes to an existing PDF document using Java.

Besides, you can learn more about Aspose.PDF for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also