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.

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.

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.

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.

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.

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.

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