Skip to content

Instantly share code, notes, and snippets.

@JoelGeraci-Datalogics
Created August 3, 2015 20:37
Show Gist options
  • Save JoelGeraci-Datalogics/b96c9004c1c8fe42f80c to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/b96c9004c1c8fe42f80c to your computer and use it in GitHub Desktop.
Create spreads from individual PDF pages
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.document;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.ByteWriter;
import com.adobe.internal.io.InputStreamByteReader;
import com.adobe.pdfjt.core.types.ASCoordinate;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.document.PDFSaveFullOptions;
import com.adobe.pdfjt.pdf.graphics.PDFRectangle;
import com.adobe.pdfjt.pdf.page.PDFPage;
import com.adobe.pdfjt.services.manipulations.PMMOptions;
import com.adobe.pdfjt.services.manipulations.PMMPagesMapping;
import com.adobe.pdfjt.services.manipulations.PMMService;
import java.io.InputStream;
import java.net.URL;
import pdfjt.util.SampleFileServices;
/**
* This sample creates page spreads from an input document. The first and last
* page are simply copied. Internal pages are merged where the even pages are on
* the left of the spread and odd pages are on the right. This sample assumes
* all pages in the source document are the same size.
*/
public class CreateSpreads {
private static final String inputPDF = "http://dev.datalogics.com/cookbook/document/Emerging_Space_Report.pdf";
private static final String outputDir = "cookbook/Document/output/";
static public void main(String[] args) throws Exception {
try {
// Read in the input file
InputStream fis = new URL(inputPDF).openStream();
ByteReader byteReader = new InputStreamByteReader(fis);
PDFDocument inputPDFDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
PDFPage inputPageOne = inputPDFDocument.requirePages().getPage(0);
/*
* Create a new output document where the first page is the same
* size as the first page of the input document.
*/
PDFDocument outputPDFDocument = PDFDocument
.newInstance(inputPageOne.getMediaBox().getRectangle(), PDFOpenOptions.newInstance());
/*
* Instantiate the PMMService so that we can use it to overlay pages
* from the input document onto new pages in the output document.
*/
PMMService pmmService = new PMMService(outputPDFDocument);
PDFPage outputPageOne = outputPDFDocument.requirePages().getPage(0);
// Overlay page one of the input document onto page one of the
// output
// document.
pmmService.overlayPages(PMMPagesMapping.newInstance(inputPageOne, outputPageOne), true, PMMOptions.newInstanceAll());
/*
* Create a rectangle that is twice as wide as the pages in the
* input document. We'll use this to create new pages that will be
* used to assemble the spreads.
*/
PDFRectangle mediaBoxForNewPages = PDFRectangle.newInstance(outputPDFDocument, 0, 0, inputPageOne.getMediaBox().width() * 2,
inputPageOne.getMediaBox().height());
for (int i = 2; i < inputPDFDocument.requirePages().getCount(); i += 2) {
// Create the new page to hold the spread
PDFPage newSpread = PDFPage.newInstance(outputPDFDocument, mediaBoxForNewPages);
// Get the pages we need to create the spread from the input
// document
PDFPage leftSide = inputPDFDocument.requirePages().getPage(i - 1);
PDFPage rightSide = inputPDFDocument.requirePages().getPage(i);
/*
* Overlay the left side with no translation; aligned in the
* lower left corner.
*/
pmmService.overlayPages(PMMPagesMapping.newInstance(leftSide, newSpread), true, PMMOptions.newInstanceAll());
/*
* Create a new set of PMMOptions that will shift the content
* for the right half of the spread to the right by the width of
* the left side and then overlay the page.
*/
PMMOptions translatedPMMOptions = PMMOptions.newInstanceAll();
ASCoordinate asCoordinate = new ASCoordinate(leftSide.getMediaBox().width(), 0);
translatedPMMOptions.setTranslationCoords(asCoordinate);
pmmService.overlayPages(PMMPagesMapping.newInstance(rightSide, newSpread), true, translatedPMMOptions);
// Add the new spread to the end of the document.
outputPDFDocument.requirePages().getLastPage().appendPage(newSpread);
}
/*
* Add the last page the same way we added the first.
*/
PDFPage inputPageLast = inputPDFDocument.requirePages().getLastPage();
PDFPage outputPageLast = PDFPage.newInstance(outputPDFDocument, outputPDFDocument.requirePages().getPage(0).getMediaBox());
pmmService.overlayPages(PMMPagesMapping.newInstance(inputPageLast, outputPageLast), true, PMMOptions.newInstanceAll());
outputPDFDocument.requirePages().getLastPage().appendPage(outputPageLast);
// Save the file.
SampleFileServices.createDir(outputDir);
ByteWriter outputFile = SampleFileServices.getRAFByteWriter(outputDir + "Emerging_Space_Report_Spreads.pdf");
outputPDFDocument.save(outputFile, PDFSaveFullOptions.newInstance());
} finally {
//
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment