If you have ever printed slides for a presentation and wanted to save paper when sharing handouts, you might have encountered N-up formatting. N-up is a common pre-print layout technique when working with PDF files and it means arranging multiple pages on a single sheet. For example, 2-up places two pages side by side, while 4-up puts four smaller pages on one sheet.
In the post, I will walk you through the N-up process using our PDF library JPedal.
Download JPedal
You will need to download the JPedal trial jar.
Add the jar to your project
For this example, I am going to use Maven. If you want to use another build system, check out the setup page on our support site.
Install the downloaded jar to your local Maven repository:
mvn install:install-file -Dfile= -DgroupId=com.idrsolutions -Dpackaging=jar -DartifactId=jpedal -Dversion=1.0
Add the following to your pom.xml file:
<dependencies>
<dependency>
<groupId>com.idrsolutions</groupId>
<artifactId>jpedal</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Add to your code
Create an instance of PDF Manipulator and call the N-up method. Applying a scaling factor of one half means the new sheet will be the same size as the original page, which is ideal for printing.
final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
// Arrange all pages in a 2×2 grid using as many sheets as necessary with a scaling factor of one half.
pdf.nUp(new PageRanges(1, pdf.getPageCount()), 2, 2, 0.5f);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));
pdf.closeDocument();
pdf.reset();
Learn more about the PDF Manipulator API.
We can help you better understand the PDF format as developers who have been working with the format for more than 2 decades!