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, and 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);
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);
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"));
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();