Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The APDFL CreatePattern sample app, translated to PDFJT
/*
* ****************************************************************************
*
* Copyright 2009-2012 Adobe Systems Incorporated. All Rights Reserved.
* Portions Copyright 2014 Datalogics Incorporated.
*
* NOTICE: Datalogics and Adobe permit you to use, modify, and distribute
* this file in accordance with the terms of the license agreement
* accompanying it. If you have received this file from a source other
* than Adobe or Datalogics, then your use, modification, or distribution of it
* requires the prior written permission of Adobe or Datalogics.
*
* ***************************************************************************
*/
package pdfjt.notshipped;
import com.adobe.pdfjt.core.exceptions.PDFIOException;
import com.adobe.pdfjt.core.exceptions.PDFInvalidDocumentException;
import com.adobe.pdfjt.core.exceptions.PDFInvalidParameterException;
import com.adobe.pdfjt.core.exceptions.PDFSecurityException;
import com.adobe.pdfjt.core.license.LicenseManager;
import com.adobe.pdfjt.core.types.ASRectangle;
import com.adobe.pdfjt.pdf.content.Content;
import com.adobe.pdfjt.pdf.content.InstructionFactory;
import com.adobe.pdfjt.pdf.content.InstructionQueue;
import com.adobe.pdfjt.pdf.contentmodify.ContentWriter;
import com.adobe.pdfjt.pdf.contentmodify.ModifiableContent;
import com.adobe.pdfjt.pdf.document.PDFContents;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.document.PDFResources;
import com.adobe.pdfjt.pdf.graphics.PDFRectangle;
import com.adobe.pdfjt.pdf.graphics.colorspaces.PDFColorSpacePattern;
import com.adobe.pdfjt.pdf.graphics.patterns.PDFPatternTiling;
import com.adobe.pdfjt.pdf.page.PDFPage;
import com.adobe.pdfjt.pdf.page.PDFPageTree;
import pdfjt.util.SampleFileServices;
/**
* Reproduces the APDFL CreatePattern sample app.
*
*/
public class CreatePattern {
static final String OUTPUT_PATH = "output/CreatePattern/TestPatterns.pdf";
static final double UPI = 72; // Units Per Inch.
public static void main(String[] args) throws Exception {
// Set the path to where PDFJT can find a license file
//
// For non license managed versions of PDFJT this can be ignored/removed
LicenseManager.setLicensePath(".");
PDFDocument pdfDoc = null;
try {
/*
* Create an ASRectangle object to initialize an empty PDF. This one
* will create a letter-size page.
*/
double[] rectangle = { 0, 0, UPI * 17 / 2, 11 * UPI };
ASRectangle rect = new ASRectangle(rectangle);
/*
* Use that rectangle to generate a new, blank document
*/
pdfDoc = PDFDocument
.newInstance(rect, PDFOpenOptions.newInstance());
/*
* Get the pageTree, then the first page. We'll need it later.
*/
PDFPageTree pageTree = pdfDoc.requirePages();
CreateBoxesWithPatterns(pdfDoc, pageTree.getPage(0));
SampleFileServices.savePDFDocument(OUTPUT_PATH, pdfDoc);
System.out.println("Successful output to " + OUTPUT_PATH);
} finally {
if (pdfDoc != null) {
pdfDoc.close();
}
}
}
/**
* Create a set of PDF instructions for drawing a square of a given scale,
* rotation and position.
*
* @param scale
* @param angle
* @param pos_x
* @param pos_y
* @return InstructionQueue
*/
private static InstructionQueue CreateSquare(double scale, double angle,
double pos_x, double pos_y) {
final double LEN = 10;
double sinR = Math.sin(angle) * scale;
double cosR = Math.cos(angle) * scale;
InstructionQueue q = new InstructionQueue();
q.enqueue(InstructionFactory.newGSave());
q.enqueue(InstructionFactory.newConcatMatrix(cosR, sinR, -sinR, cosR,
pos_x, pos_y));
q.enqueue(InstructionFactory.newMoveTo(0, 0));
q.enqueue(InstructionFactory.newLineTo(0, LEN));
q.enqueue(InstructionFactory.newLineTo(LEN, LEN));
q.enqueue(InstructionFactory.newLineTo(LEN, 0));
q.enqueue(InstructionFactory.newCloseFillAndStrokePath());
q.enqueue(InstructionFactory.newGRestore());
return q;
}
/**
* Create the content for a Pattern.
*
* @param pdfDoc
* @return PDFContents
* @throws PDFIOException
* @throws PDFInvalidDocumentException
* @throws PDFSecurityException
* @throws PDFInvalidParameterException
*/
private static PDFContents CreateBoxesWithColors(PDFDocument pdfDoc)
throws PDFIOException, PDFInvalidDocumentException,
PDFSecurityException, PDFInvalidParameterException {
// 7 different colors for 7 different squares to match the APDFL
// squares.
double colors[][] = { { 0.9, 0.7, 0.5 }, { 0.9, 0, 0.9 },
{ 0, 0.9, 0.9 }, { 0.9, 0.9, 0 }, { 0, 0, 0.9 }, { 0, 0.9, 0 },
{ 0.9, 0, 0 } };
// these factors are from the APDFL sample. the position factors are in
// inches.
double squareFactors[][] = { { 3.2, 1.0, 0.7, 0.6 },
{ 4.3, 1.3, 0.4, 0.5 }, { 3.2, 1.0, 0.3, -0.2 },
{ 3.5, 2.2, 0.3, 0.6 }, { 4.0, 1.2, 0.7, 0.2 },
{ 3.0, 2.5, 0.9, 0.3 }, { 2.5, 0.1, 0.0, 0.1 } };
ContentWriter pen = ContentWriter.newInstance(ModifiableContent
.newInstance(pdfDoc));
pen.write(InstructionFactory.newLineWidth(0.5));
for (int i = 0; i < squareFactors.length; i++) {
pen.write(InstructionFactory.newColorFill(colors[i]));
double strokeColor[] = { colors[i][1], colors[i][2], colors[i][0] };
pen.write(InstructionFactory.newColorStroke(strokeColor));
double factors[] = squareFactors[i];
double posX = factors[2] * UPI;
double posY = factors[3] * UPI;
InstructionQueue squareQ = CreateSquare(factors[0], factors[1],
posX, posY);
while (!squareQ.isEmpty()) {
pen.write(squareQ.dequeue());
}
}
Content newContent = pen.close();
return newContent.getContents();
}
/**
* Adds to the given page a bunch of squares filled in with a pattern
* created from a bunch of colored squares.
*
* @param pdfDoc
* @param page
* @throws PDFInvalidDocumentException
* @throws PDFIOException
* @throws PDFSecurityException
* @throws PDFInvalidParameterException
*/
private static void CreateBoxesWithPatterns(PDFDocument pdfDoc, PDFPage page)
throws PDFInvalidDocumentException, PDFIOException,
PDFSecurityException, PDFInvalidParameterException {
final double STEP = 60;
PDFRectangle pattRect = PDFRectangle.newInstance(pdfDoc, 0, 0, STEP,
STEP);
PDFPatternTiling pattern = PDFPatternTiling.newInstance(pdfDoc,
pattRect, PDFResources.newInstance(pdfDoc), 1, 1, STEP, STEP);
pattern.setContents(CreateBoxesWithColors(pdfDoc));
double altFillColor[] = { 0.5, 0.5, 0.5 };
ContentWriter pen = ContentWriter.newInstance(ModifiableContent
.newInstance(pdfDoc));
pen.write(PDFColorSpacePattern.newInstance(pdfDoc), true);
pen.write(pattern, true, altFillColor);
pen.write(InstructionFactory.newDeviceRGBStroke(0, 0, 0)); // black
pen.write(InstructionFactory.newLineWidth(0.25));
// these factors are from the APDFL sample. the position factors are in
// inches.
double squareFactors[][] = { { 6.0, 2.4, 2.0, 2.0 },
{ 8.0, 1.4, 4.0, 4.0 }, { 10.0, 0.0, 6.0, 6.0 },
{ 14.0, 0.8, 2.0, 6.0 } };
for (int i = 0; i < squareFactors.length; i++) {
double factors[] = squareFactors[i];
double posX = factors[2] * UPI;
double posY = factors[3] * UPI;
InstructionQueue squareQ = CreateSquare(factors[0], factors[1],
posX, posY);
while (!squareQ.isEmpty()) {
pen.write(squareQ.dequeue());
}
}
Content newContent = pen.close();
page.setResources(newContent.getResources());
page.setContents(newContent.getContents());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.