Introduction
PDF manipulation is a common requirement for Java developers working on document management systems, automation tools, or business applications. Whether you need to add pages, resize content, insert watermarks, or annotate files, having a reliable and easy-to-use Java PDF SDK is essential.
JPedal’s PDF Manipulation Toolset
Recently, we added some new PDF manipulation features to our Java PDF SDK JPedal. JPedal is the best Java PDF library for developers.
These features include:
- Add a new page
- Remove a page
- Resize a page
- Resize a page’s content
- Add text to a page
- And more…
Today I’m going to show you how to perform some basic edits to a PDF document
Getting Started
First, you need to create a new instance of PdfManipulator.
final PdfManipulator pdf = new PdfManipulator();
Next, you need to load the desired document. You may load a PDF from a
File
or a byte[]
.
pdf.loadDocument(new File("inputFile.pdf"));
Resize Page Content
Now, let’s shrink the content on the first page, leaving a gap at the top.
pdf.scalePageContent(1, 1.0f, 0.8f, ScalePageContent.BOTTOM);
Add Text to a Page
In the newly created space, we can add some text.
final float[] mediabox = pdf.getPageMediaBox(1);
pdf.addText(1, "Hello World", mediabox[2] / 2, mediabox[3] – 30, BaseFont.Helvetica, 12, 1, 0.3f, 0.2f);
Now we need to apply the manipulations and write the result to a new file.
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));
Finally, we should close the document to free resources. You can optionally reset the
PdfManipulator
to remove the manipulations from its queue.
pdf.closeDocument();
Comparison: Before vs After
Conclusion
With JPedal’s new PDF manipulation features, editing PDF documents in Java is easier and more flexible than ever. Try out these features in your next Java project, and let us know what other PDF editing capabilities you’d like to see!