Link
Skip to main content

Manipulate PDF files in Java

v2025.05

JPedal provides a PdfManipulator class to allow for easy manipulation, editing, and creation of PDF files.

Load and write documents

Create a new PDF Manipulator

final PdfManipulator pdf = new PdfManipulator();

Load a document

Documents may be loaded from a File or byte[]

pdf.loadDocument(new File("inputFile.pdf"));
pdf.loadDocument(pdfBytes);

You may also load password-protected documents

pdf.loadDocument(new File("inputFile.pdf"), "password".getBytes());
pdf.loadDocument(pdfBytes, "password".getBytes());

Create a new, empty document

Creates a new document with a single empty page. You must provide the dimensions of the initial page. See the PaperSize Javadoc for common sizes.

pdf.newDocument(PaperSize.A4_LANDSCAPE);

Write a document

Documents may be written to a File or byte[]

pdf.writeDocument(new File("outputFile.pdf"));
final byte[] pdfBytes = pdf.writeDocument();

Close a document

You should close the document after you are finished to free resources

pdf.closeDocument();

Apply and reset manipulations

Apply a manipulation

Manipulations are added to a queue held by the PdfManipulator. They will be applied in the order they were added. Manipulations remain in the queue after apply() is called.

pdf.apply();

Reset manipulations

The PdfManipulator will retain its queue of manipulations so that you can apply identical edits to many documents without having to redo each manipulation. However, if you want to clear its queue to start with fresh edits, you may use reset().

pdf.reset();

Manipulations

Add a page

Insert one or multiple pages into the document, at the specified index. Pages indices start from 1. You must provide the dimensions of the initial page. See the PaperSize Javadoc for common sizes.

pdf.addPage(1, PaperSize.A4_LANDSCAPE);
pdf.addPage(new PageRanges("1-10"), PaperSize.LEGAL_PORTRAIT);

Copy a page

Copy a page and place the new copy anywhere in the document. Page indices start from 1.

pdf.copyPage(1, 2);

Remove a page

Remove one or multiple pages from the document. Page indices start from 1.

pdf.removePage(1);
pdf.removePage(new PageRanges("1-10"));

Rescale a page

Resizes one or multiple pages. Pages indices start from 1.

pdf.scalePage(1, 0.5f, 0.5f);
pdf.scalePage(new PageRanges("1-10"), 2.0f, 1.0f);

Rescale a page’s content

Resizes one or multiple pages’ content, leaving the page dimensions the same. Pages indices start from 1. You may provide translate values to move the scaled page content. The PDF origin is bottom-left, so a translate of 10, 10 moves the content 10 units away from the bottom-left corner. See the ScalePageContent Javadoc for preset translations, such as page center, or right-hand side.

pdf.scalePageContent(1, 0.5f, 0.5f, 0.0f, 0.0f);
pdf.scalePageContent(new PageRanges("1-10"), 0.5f, 0.5f, ScalePageContent.RIGHT);

Rotate a page

Rotates one or multiple pages. Page indices start from 1. Angles are specified in degrees and must be a multiple of 90. You may rotate a page based on its current rotation, or set a page’s rotation to an angle.

pdf.rotatePage(1, -90);
pdf.setPageRotation(1, 180);

Add text

Adds text to one or multiple pages. Pages indices start from 1. You must use one of the PDF base fonts. See the BaseFont Javadoc for these values.

pdf.addText(1, "Hello World", 10, 10, BaseFont.HelveticaBold, 12, 1, 0.3f, 0.2f);

Add image

Adds an image to one or multiple pages. Page indices start from 1. You will need a library that can read a BufferedImage. We recommend JDeli. You must provide a rectangle which defines the origin and dimensions of the image.

final BufferedImage img = new BufferedImage();
pdf.addImage(1, img, new float[] {0, 0, 100, 100});

Add shape

Adds a shape to one or multiple pages. Page indices start from 1. You may provide any arbitrary shape that implements Shape. You must provide an instance of DrawParameters to control the output. You must, at the very least, set a fill rule using setFillRule(int), because the default drawing mode will produce no output.

final Shape shape = new Rectangle2D.Float(56.7f, 596.64f, 131.53f, 139.25f);
final DrawParameters params = new DrawParameters();
params.setStrokeColor(new float[] {1, 0, 0});
params.setFillRule(DrawParameters.STROKE);
pdf.addShape(1, shape, params);

Add annotation

Adds an annotation to one or multiple pages. Page indices start from 1. The following annotation types are supported:

  • Text
  • Link
  • FreeText
  • Line
  • Square
  • Circle
  • Polygon
  • PolyLine
  • Highlight
  • Underline
  • Squiggly
  • StrikeOut
  • Caret
  • Stamp
  • Ink
  • FileAttachment - see Attach file

See the Annotation Javadoc for more information.

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);

Embed file

Embeds an arbitrary file into a PDF document.

pdf.embedFile(new File("embed.png"), "embedded-image");

Attach file

Embeds an arbitrary file into a PDF document, and adds a FileAttachment annotation to one or multiple pages. Page indices start from 1. You must provide a rectangle which defines the origin and dimensions of the annotation. You must provide a color for the annotation, which may be gray, RGB, or CMYK (1, 3, or 4 components).

pdf.attachFile(1, new File("embed.png"), "embedded-image", 
        new float[] {10.0f, 10.0f, 100.0f, 100.0f}, new float[] {0.7f, 0.3f, 0.4f});

Isolate pages

Isolates pages in a document (remove all other pages). Pages indices start from 1.

pdf.isolatePage(new PageRanges("1-2"));

N-up pages

Arrange a subset of pages in a grid formation on a single page or multiple pages. Page indices start from 1. By default, no scaling is applied to the resulting grid pages, their dimensions will be the sum of the contained pages. For use in printing, it is recommended to apply a scaling value to restore the grid pages dimensions to the same size as the contained pages. For example, a 2x2 grid will need a scaling factor of 0.5 to retain the original dimensions.

// 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);

Queries

Get a page’s media box

Obtain the media box for a specified page. Pages indices start from 1.

final float[] mediabox = pdf.getPageMediaBox(1);

Get a page’s crop box

Obtain the crop box for a specified page. Pages indices start from 1.

final float[] cropbox = pdf.getPageCropBox(1);

Get the page count

Obtain the number of pages in the document.

final int pages = pdf.getPageCount();

Determine if a document contains JavaScript

Determine if the document contains any JavaScript (ECMAScript).

final boolean javascript = pdf.containsJavaScript();

Sanitization

Remove JavaScript

Remove any JavaScript (ECMAScript) from the document. You do not need to call containsJavaScript() first as this method will return silently if the document does not contain any JavaScript.

pdf.removeJavaScript();

Remove embedded files

Remove any embedded/attached files from the document.

pdf.removeEmbeddedFiles();

Remove metadata

Removes the following document information:

  • Title
  • Author
  • Subject
  • Keywords
  • Creator
  • Producer
  • CreationDate
  • ModDate
  • Trapped

Also removes any XMP/XML metadata streams.

pdf.removeMetadata();

Why JPedal?

  • Actively developed commercial library with full support and no third party dependencies.
  • Process PDF files up to 3x faster than alternative Java PDF libraries.
  • Simple licensing options and source code access for OEM users.

Learn more about JPedal

Start Your Free Trial


Customer Downloads

Select Download