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);
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"));
Get a page’s media box
Obtain the media box for a specified page.
final float[] mediabox = pdf.getPageMediaBox(1);
Get a page’s crop box
Obtain the crop box for a specified page.
final float[] cropbox = pdf.getPageCropBox(1);
Get the page count
Obtain the number of pages in the document.
final int pages = pdf.getPageCount();
Rescale a page
Resizes one or multiple pages.
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. 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);
Add text
Adds text to one or multiple pages. 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);
Isolate pages
Isolates pages in a document (remove all other pages).
pdf.isolatePage(new PageRanges("1-2"));