document.getPages().add(page); // Puts the page in the pages collection.
Dimension2D pageSize = page.getSize();
// 2. Create a content composer for the page!
PrimitiveComposer composer = new PrimitiveComposer(page);
// 3. Drawing the page contents...
try
{
composer.setFont(
new StandardType1Font(
document,
StandardType1Font.FamilyEnum.Courier,
true,
false
),
32
);
}
catch(Exception e)
{throw new RuntimeException(e);}
{
BlockComposer blockComposer = new BlockComposer(composer);
blockComposer.begin(new Rectangle2D.Double(30,0,pageSize.getWidth()-60,50),AlignmentXEnum.Center,AlignmentYEnum.Middle);
blockComposer.showText("Miscellaneous");
blockComposer.end();
}
composer.beginLocalState();
composer.setLineJoin(LineJoinEnum.Round);
composer.setLineCap(LineCapEnum.Round);
// 3.1. Polygon.
composer.drawPolygon(
new Point2D[]
{
new Point2D.Double(100,200),
new Point2D.Double(150,150),
new Point2D.Double(200,150),
new Point2D.Double(250,200)
}
);
// 3.2. Polyline.
composer.drawPolyline(
new Point2D[]
{
new Point2D.Double(300,200),
new Point2D.Double(350,150),
new Point2D.Double(400,150),
new Point2D.Double(450,200)
}
);
composer.stroke();
// 3.3. Rectangle (both squared and rounded).
int x = 50;
int radius = 0;
while(x < 500)
{
if(x > 300)
{
composer.setLineDash(3,5,5);
}
composer.setFillColor(new DeviceRGBColor(1,x/500,x/500));
composer.drawRectangle(
new Rectangle2D.Double(x,250,150,100),
radius // NOTE: radius parameter determines the rounded angle size.
);
composer.fillStroke();
x += 175;
radius += 10;
}
composer.end(); // End local state.
composer.beginLocalState();
composer.setFont(
composer.getState().getFont(),
12
);
// 3.4. Line cap parameter.
int y = 400;
for(LineCapEnum lineCap
: EnumSet.allOf(LineCapEnum.class))
{
composer.showText(
lineCap + ":",
new Point2D.Double(50,y),
AlignmentXEnum.Left,
AlignmentYEnum.Middle,
0
);
composer.setLineWidth(12);
composer.setLineCap(lineCap);
composer.drawLine(
new Point2D.Double(120,y),
new Point2D.Double(220,y)
);
composer.stroke();
composer.beginLocalState();
composer.setLineWidth(1);
composer.setStrokeColor(DeviceRGBColor.White);
composer.setLineCap(LineCapEnum.Butt);
composer.drawLine(
new Point2D.Double(120,y),
new Point2D.Double(220,y)
);
composer.stroke();
composer.end(); // End local state.
y += 30;
}
// 3.5. Line join parameter.
y += 50;
for(LineJoinEnum lineJoin
: EnumSet.allOf(LineJoinEnum.class))
{
composer.showText(
lineJoin + ":",
new Point2D.Double(50,y),
AlignmentXEnum.Left,
AlignmentYEnum.Middle,
0
);
composer.setLineWidth(12);
composer.setLineJoin(lineJoin);
Point2D.Double[] points = new Point2D.Double[]
{
new Point2D.Double(120,y+25),
new Point2D.Double(150,y-25),
new Point2D.Double(180,y+25)
};
composer.drawPolyline(points);
composer.stroke();
composer.beginLocalState();
composer.setLineWidth(1);
composer.setStrokeColor(DeviceRGBColor.White);
composer.setLineCap(LineCapEnum.Butt);
composer.drawPolyline(points);
composer.stroke();
composer.end(); // End local state.
y += 50;
}
composer.end(); // End local state.
// 3.6. Clipping.
/*
NOTE: Clipping should be conveniently enclosed within a local state
in order to easily resume the unaltered drawing area after the operation completes.
*/
composer.beginLocalState();
composer.drawPolygon(
new Point2D[]
{
new Point2D.Double(220,410),
new Point2D.Double(300,490),
new Point2D.Double(450,360),
new Point2D.Double(430,520),
new Point2D.Double(590,565),
new Point2D.Double(420,595),
new Point2D.Double(460,730),
new Point2D.Double(380,650),
new Point2D.Double(330,765),
new Point2D.Double(310,640),
new Point2D.Double(220,710),
new Point2D.Double(275,570),
new Point2D.Double(170,500),
new Point2D.Double(275,510)
}
);
composer.clip();
// Showing a clown image...
// Instantiate a jpeg image object!
Image image = Image.get(getInputPath() + java.io.File.separator + "images" + java.io.File.separator + "Clown.jpg"); // Abstract image (entity).
// Show the image!
composer.showXObject(
image.toXObject(document),
new Point2D.Double(
170,
320
),
new Dimension(450,0)
);
composer.end(); // End local state.
// 4. Flush the contents into the page!
composer.flush();
}