Manipulate PDF pages
Add, Copy, Delete, Resize pages in an existing PDF Document
How to Manipulate PDF Pages in Java
final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
pdf.addPage(1, PaperSize.A4_LANDSCAPE);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
pdf.copyPage(1, 2);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
pdf.removePage(1);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
pdf.scalePage(1, 0.5f, 0.5f);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
// Arrange pages 1-10 in a 2x2 grid using as many pages as necessary.
pdf.nUp(new PageRanges(1, 10), 2, 2);
// Arrange pages 1-10 in a 2x2 grid using as many pages as necessary with a scaling factor of one half.
pdf.nUp(new PageRanges(1, 10), 2, 2, 0.5f);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
final Annotation[] annotations = new Annotation[2];
// Text box annotation with size 12 TimesNewRoman font red color and center aligned text.
annotations[0] = new FreeText(new float[] {400, 600, 500, 700}, "hello!", new float[] {1.0f, 0.0f, 0.0f}, BaseFont.TimesRoman, 12, Quadding.CENTRED);
// Hyperlink annotation with blue color.
annotations[1] = new Link(new float[] {200, 600, 300, 700}, new float[] {0.0f, 0.0f, 1.0f}, "https://idrsolutions.com/");
pdf.addAnnotation(1, annotations);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
final BufferedImage img = new BufferedImage();
pdf.addImage(1, img, new float[] {0, 0, 100, 100});
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
pdf.addText(1, "Hello World", 10, 10, BaseFont.HelveticaBold, 12, 1, 0.3f, 0.2f);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));JPedal is a complete Java toolbox for working with PDF Documents
Need to merge, split, sign, sanitize, or optimize a whole document instead? See Process PDF Documents in Java.
| Feature | What it does | Tutorial |
|---|---|---|
| Add images | Insert an image into one or more pages, either as an interactive annotation or drawn directly into the page content. | How to Add Images to PDF Files |
| Add pages | Insert one or more new pages into the document at a specified index. | How to Manipulate PDF Documents in Java |
| Add text | Add text to one or more pages at a specified position, font, and size. | How to Manipulate PDF Documents in Java |
| Copy pages | Copy a page and place the new copy anywhere in the document. | How to Duplicate Pages in a PDF |
| Isolate pages | Remove every page except a specified range, keeping only the pages you isolate. | |
| N-up | Arrange a subset of pages in a grid on a single page, for printing multiple pages per sheet. | How to N-up Pages in PDF Files |
| Remove pages | Remove one or more pages from the document. | How to Remove a Page from a PDF |
| Rotate pages | Rotate one or more pages by a multiple of 90 degrees. | How to Manipulate PDF Documents in Java |
| Scale page content | Resize a page's content while leaving the page dimensions unchanged. | How to Manipulate PDF Documents in Java |
| Scale pages | Resize one or more pages by a scale factor or to a target size. | |
| Split pages | Split a document into individual pages or ranges, one output file per split. |
JPedal has the advantage of running in the same Java virtual machine, so we save a lot of CPU resources.
- T. Büngener (Software Architect InSign)
Frequently Asked Questions
Can I rotate a page by any angle?
No. Rotation angles must be a multiple of 90 degrees.
What's the difference between scaling a page and scaling its content?
Scaling a page resizes the page dimensions themselves. Scaling a page's content resizes what's drawn on it while the page dimensions stay the same.
Can I use any font when adding text to a page?
No. Text added this way must use one of the standard PDF base fonts, such as Helvetica or Times-Roman, not an embedded or custom font.
Can I chain multiple page operations, like rotating and adding text, in one pass?
Yes. PdfManipulator queues up manipulations and applies them all in the order you added them with a single apply() and writeDocument() call, so you can rotate, add text, add images and more in one workflow instead of a separate read/write cycle per operation.
Do I need any external tools to manipulate PDF pages with JPedal?
No. These operations run entirely inside your JVM through the JPedal library, with no native binaries or external processes required.
JPedal is absolutely easy to use and it provided the best results of all libraries we tested over the years. We need to create renditions of PDF files so that people can view them conveniently in the browser.
- Roman K. (Developer at German Digital Big Data Platform)
Support for JPedal is done extremely well and for continuously using the software, what the customer needs is good support for the issues we face. And it is superb in that respect.
- Developer (Large Multinational Corporation)