Get or Set PDF Metadata in Java
If you mold the metadata of your PDFs to allow for SEO optimization via keywords, you will be able to increase the online searchability of your document.
Join the DZone community and get the full member experience.Join For Free
Due to their fixed and presentable nature, PDF files are widely used in web applications by both users and businesses. Each of these files contains ‘metadata,' which essentially translates to data about data. PDF metadata contains supplementary information on the document, such as its author, subject, title, creation date, and more. If a PDF file was initially created by a transfer from an original source document (i.e. DocX, PPT, etc.) additional information, such as the file size and whether the file has been optimized for the Web, is automatically added as well.
So why is this PDF metadata relevant to your business? If you have PDF documents that are accessible on your website or application, the metadata can enable search engines to easily locate the documents. Therefore, if you mold the metadata of your PDFs to allow for keywords that may be picked up by search engines, you will be able to increase the searchability of your document.
The following APIs will allow you to extract or set metadata from PDF files and either edit or leverage the information to meet your business’s needs.
To begin, we first need to install the Maven SDK by adding a reference to the repository in pom.xml:
Then we can add a reference to the dependency:
Once the installation is complete, we can add the imports to the top of the controller and configure the API key. If you don’t already have an API key, you can register for a free account on the Cloudmersive website to retrieve it.
If you simply wish to gather metadata from the PDF, the following function will perform the action for you—all you need for input is the target PDF file.
However, if you are looking to edit/set metadata for a PDF document, you will use the following API function instead:
In order for the above operation to run smoothly, be sure to input the desired metadata information as well:
In conclusion, we hope that the tools provided in this tutorial can assist in optimizing PDF metadata for your personal or business requirements.
Opinions expressed by DZone contributors are their own.