Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/0f27529eeec3363277f726c80960740b to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/0f27529eeec3363277f726c80960740b to your computer and use it in GitHub Desktop.
Add graphics elements in XPS document with Java

Working with Graphics Elements in XPS Examples

These code snippets show how to add and manipulate graphics elements in XPS documents using Aspose.Page for Java. The library enables drawing shapes, gradients, and text programmatically in XPS files.

Key Use Cases

  • Draw shapes, lines, curves
  • Add text and images
  • Use solid, gradient, image, and visual brushes
  • Apply transforms, clipping, and transparency

How to Run Examples

  1. Reference Aspose.Page for Java in your project.
  2. Copy the required code snippet into your project.
  3. Download a temporary license and set it up as described here or use a paid license.
  4. Run the example.

Restrictions

Evaluation mode limits the number of manipulated XPS elements to 4. Set up a valid temporary or paid license to unlock full functionality.

Related Documentation

Related Resources

Requirements

  • Java 8 or higher
  • Aspose.Page for Java library
Aspose.Page for Java – Add graphics elements in XPS Examples
// Paint rectangle with diagonal gradient fill in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-gradient/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "DiagonalGradient_outXPS.xps";
// Initialize List of XpsGradentStop
List<XpsGradientStop> stops = new ArrayList<>();
// Add Colors to Gradient
stops.add(doc.createGradientStop(doc.createColor(0, 142, 4), 0f));
stops.add(doc.createGradientStop(doc.createColor(255, 202, 0), 0.144531f));
stops.add(doc.createGradientStop(doc.createColor(255, 250, 0), 0.264648f));
stops.add(doc.createGradientStop(doc.createColor(255, 0, 0), 0.414063f));
stops.add(doc.createGradientStop(doc.createColor(233, 0, 255), 0.544922f));
stops.add(doc.createGradientStop(doc.createColor(107, 27, 190), 0.694336f));
stops.add(doc.createGradientStop(doc.createColor(63, 0, 255), 0.844727f));
stops.add(doc.createGradientStop(doc.createColor(0, 199, 80), 1f));
// Create new path by defining geometery in abbreviation form
XpsPath path = doc.addPath(doc.createPathGeometry("M 10,10 L 228,10 228,100 10,100"));
path.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 20f, 70f));
path.setFill(doc.createLinearGradientBrush(new Point2D.Float(10f, 10f), new Point2D.Float(228f, 100f)));
((XpsGradientBrush)path.getFill()).getGradientStops().addAll(stops);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Add ellipse to XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/graphic-utilities/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "AddEllipse_outXPS.xps";
// Radial gradient stroked ellipse in the lower left
List<XpsGradientStop> stops = new ArrayList<>();
stops.add(doc.createGradientStop(doc.createColor(0, 0, 255), 0f));
stops.add(doc.createGradientStop(doc.createColor(255, 0, 0), .25f));
stops.add(doc.createGradientStop(doc.createColor(0, 255, 0), .5f));
stops.add(doc.createGradientStop(doc.createColor(255, 255, 0), .75f));
stops.add(doc.createGradientStop(doc.createColor(255, 0, 0), 1f));
XpsPath path = doc.addPath(doc.createPathGeometry("M 20,250 A 100,50 0 1 1 220,250 100,50 0 1 1 20,250"));
path.setStroke(doc.createRadialGradientBrush(new Point2D.Float(575f, 125f), new Point2D.Float(575f, 100f), 75f, 50f));
((XpsGradientBrush)path.getStroke()).setSpreadMethod(com.aspose.xps.XpsSpreadMethod.Reflect);
((XpsGradientBrush)path.getStroke()).getGradientStops().addAll(stops);
stops.clear();
path.setStrokeThickness(12f);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Add grid with visual brush to XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-visual-brush/
XpsDocument doc = new XpsDocument();
String outputFileName = "AddGrid_out.xps";
// Geometry for magenta grid VisualBrush
XpsPathGeometry pathGeometry = doc.createPathGeometry();
Point2D.Float[] points = new Point2D.Float[] {
new Point2D.Float(240f, 5f),
new Point2D.Float(240f, 310f),
new Point2D.Float(0f, 310f)
};
pathGeometry.addSegment(doc.createPolyLineSegment(points));
pathGeometry.getPathFigures().get(0).setStartPoint(new Point2D.Float(0f, 5f));
// Canvas for magenta grid VisualBrush
XpsCanvas visualCanvas = doc.createCanvas();
XpsPath visualPath = visualCanvas.addPath(
doc.createPathGeometry("M 0,4 L 4,4 4,0 6,0 6,4 10,4 10,6 6,6 6,10 4,10 4,6 0,6 Z"));
visualPath.setFill(doc.createSolidColorBrush(doc.createColor(1f, .61f, 0.1f, 0.61f)));
XpsPath gridPath = doc.createPath(pathGeometry);
//Create Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
gridPath.setFill(doc.createVisualBrush(visualCanvas,
new Rectangle2D.Float(0f, 0f, 10f, 10f), new Rectangle2D.Float(0f, 0f, 10f, 10f)));
((XpsVisualBrush)gridPath.getFill()).setTileMode(XpsTileMode.Tile);
// New canvas
XpsCanvas canvas = doc.addCanvas();
canvas.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 268f, 70f));
// Add grid
canvas.addPath(pathGeometry);
// Red transparent rectangle in the middle top
XpsPath path = canvas.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
path = canvas.addPath(doc.createPathGeometry("M 10,10 L 228,10 228,100 10,100"));
path.setFill(doc.createSolidColorBrush(doc.createColor(1.0f, 0.0f, 0.0f)));
path.setOpacity(0.7f);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Paint rectangle with horizontal gradient fill in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-gradient/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "HorizontalGradient_outXPS.xps";
// Initialize List of XpsGradentStop
List<XpsGradientStop> stops = new ArrayList<>();
stops.add(doc.createGradientStop(doc.createColor(255, 244, 253, 225), 0.0673828f));
stops.add(doc.createGradientStop(doc.createColor(255, 251, 240, 23), 0.314453f));
stops.add(doc.createGradientStop(doc.createColor(255, 252, 209, 0), 0.482422f));
stops.add(doc.createGradientStop(doc.createColor(255, 241, 254, 161), 0.634766f));
stops.add(doc.createGradientStop(doc.createColor(255, 53, 253, 255), 0.915039f));
stops.add(doc.createGradientStop(doc.createColor(255, 12, 91, 248), 1f));
// Create new path by defining geometery in abbreviation form
XpsPath path = doc.addPath(doc.createPathGeometry("M 10,210 L 228,210 228,300 10,300"));
path.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 20f, 70f));
path.setFill(doc.createLinearGradientBrush(new Point2D.Float(10f, 0f), new Point2D.Float(228f, 0f)));
((XpsGradientBrush)path.getFill()).getGradientStops().addAll(stops);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Add image to XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-images/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "AddImage_outXPS.xps";
// Add Image
XpsPath path = doc.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
//Creating a matrix is optional, it can be used for proper positioning
path.setRenderTransform(doc.createMatrix(0.7f, 0f, 0f, 0.7f, 0f, 20f));
//Create Image Brush
path.setFill((XpsImageBrush)doc.createImageBrush(getDataDir() + "QL_logo_color.tif", new Rectangle2D.Float(0f, 0f, 258.24f, 56.64f), new Rectangle2D.Float(50f, 20f, 193.68f, 42.48f)));
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Add Rectangle to XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/graphic-utilities/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "AddRectangleXPS_out.xps";
// CMYK (blue) solid color stroked rectangle in the lower left
XpsPath path = doc.addPath(doc.createPathGeometry("M 20,10 L 220,10 220,100 20,100 Z"));
path.setStroke(doc.createSolidColorBrush(
doc.createColor(getDataDir() + "uswebuncoated.icc", 1.0f, 1.000f, 0.000f, 0.000f, 0.000f)));
path.setStrokeThickness(12f);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Paint rectangle with tiled image (texture pattern) in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-images/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "AddTextureTilingPattern_outXPS.xps";
// Tile image
// ImageBrush filled rectangle in the right top bellow
XpsPath path = doc.addPath(doc.createPathGeometry("M 10,160 L 228,160 228,305 10,305"));
path.setFill((XpsImageBrush)doc.createImageBrush(getDataDir() + "R08LN_NN.jpg", new Rectangle2D.Float(0f, 0f, 128f, 96f), new Rectangle2D.Float(0f, 0f, 64f, 48f)));
((XpsImageBrush)path.getFill()).setTileMode(com.aspose.xps.XpsTileMode.Tile);
path.getFill().setOpacity(0.5f);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Add text to XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-text/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "AddText_out.xps";
//Create a brush
XpsSolidColorBrush textFill = doc.createSolidColorBrush(Color.BLACK);
//Add glyph to the document
XpsGlyphs glyphs = doc.addGlyphs("Arial", 12, XpsFontStyle.Regular, 300f, 450f, "Hello World!");
glyphs.setFill(textFill);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Demonstrates transparency in shapes in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-transparency/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "AddTransparentObject_outXPS.xps";
// Just to demonstrate transparency - draw background rectangles
doc.addPath(doc.createPathGeometry("M120,0 H400 v1000 H120")).setFill(doc.createSolidColorBrush(doc.createColor(128,128,128)));
doc.addPath(doc.createPathGeometry("M300,120 h600 V420 h-600")).setFill(doc.createSolidColorBrush(doc.createColor(128,128,128)));
// Create path with closed rectangle geometry
XpsPath path1 = doc.createPath(doc.createPathGeometry("M20,20 h200 v200 h-200 z"));
// Set blue solid brush to fill path1
path1.setFill(doc.createSolidColorBrush(doc.createColor(0, 0, 255)));
// Add it to the current page
XpsPath path2 = doc.add(path1);
// path1 and path2 are the same as long as path1 has no parent. Setting fill on path2 affects shared brush
path2.setFill(doc.createSolidColorBrush(doc.createColor(0, 128, 0)));
// Now add path2 once again. Now path2 has parent. So path3 won't be the same as path2.
XpsPath path3 = doc.add(path2);
// shift it 300 units lower
path3.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 0f, 300f));
// set red solid brush to fill it
path3.setFill(doc.createSolidColorBrush(doc.createColor(255, 0, 0)));
// Create new path4 with path2's geometry ...
XpsPath path4 = doc.addPath(path2.getData());
// shift it 300 units to the right ...
path4.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 300f, 0f));
// and set blue solid fill
path4.setFill(doc.createSolidColorBrush(doc.createColor(0, 0, 255)));
// Add path4 once again.
XpsPath path5 = doc.add(path4);
// move path5 300 units lower and disconnect render transform from path4
path5.setRenderTransform(path5.getRenderTransform().deepClone());
path5.getRenderTransform().translate(0f, 300f);
// set the opacity of Fill property - this will affect both path5 and path4 if brush instance is shared
path5.getFill().setOpacity(0.8f);
// Create new path6 with path2's geometry ...
XpsPath path6 = doc.addPath(path2.getData());
// shift it 600 units to the right ...
path6.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 600f, 0f));
// and set yellow solid fill
path6.setFill(doc.createSolidColorBrush(doc.createColor(255, 255, 0)));
// Now add path6's clone ...
XpsPath path7 = doc.add(path6.deepClone());
// move it 300 units lower
path7.setRenderTransform(path7.getRenderTransform().deepClone());
path7.getRenderTransform().translate(0f, 300f);
// set opacity for path7
path7.getFill().setOpacity(0.8f);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Paint rectangle with vertical gradient fill in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-gradient/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "VerticalGradient_outXPS.xps";
// Initialize List of XpsGradentStop
List<XpsGradientStop> stops = new ArrayList<>();
stops.add(doc.createGradientStop(doc.createColor(253, 255, 12, 0), 0f));
stops.add(doc.createGradientStop(doc.createColor(252, 255, 154, 0), 0.359375f));
stops.add(doc.createGradientStop(doc.createColor(252, 255, 56, 0), 0.424805f));
stops.add(doc.createGradientStop(doc.createColor(253, 255, 229, 0), 0.879883f));
stops.add(doc.createGradientStop(doc.createColor(252, 255, 255, 234), 1f));
// Create new path by defining geometery in abbreviation form
XpsPath path = doc.addPath(doc.createPathGeometry("M 10,110 L 228,110 228,200 10,200"));
path.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 20f, 70f));
path.setFill(doc.createLinearGradientBrush(new Point2D.Float(10f, 110f), new Point2D.Float(10f, 200f)));
((XpsGradientBrush)path.getFill()).getGradientStops().addAll(stops);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Applying clips to the canvases in XPS document.
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "ApplyClips_outXPS.xps";
// Create main canvas, common for all page elements
XpsCanvas canvas1 = doc.addCanvas();
// Make left and top offsets in the main canvas
canvas1.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 20f, 10f));
// Create rectangle path geometry
XpsPathGeometry rectGeom = doc.createPathGeometry("M 0,0 L 500,0 500,300 0,300 Z");
// Create a fill for rectangles
XpsBrush fill = doc.createSolidColorBrush(doc.createColor(12, 15, 159));
// Add another canvas with clip to the main canvas
XpsCanvas canvas2 = canvas1.addCanvas();
// Create circle geometry for clip
XpsPathGeometry clipGeom = doc.createPathGeometry("M250,250 A100,100 0 1 1 250,50 100,100 0 1 1 250,250");
canvas2.setClip(clipGeom);
// Create rectangle in this canvas and fill it
XpsPath rect = canvas2.addPath(rectGeom);
rect.setFill(fill);
// Add the second canvas with stroked rectangle to the main canvas
XpsCanvas canvas3 = canvas1.addCanvas();
// Create rectangle in this canvas and stroke it
rect = canvas3.addPath(rectGeom);
rect.setStroke(fill);
rect.setStrokeThickness(2);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Using shape utilities for adding shapes to XPS document.
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// ARGB solid color filled rectangle
XpsPath rect1 = doc.addPath(doc.createPathGeometry("M 20,10 L 220,10 220,100 20,100 Z"));
rect1.setFill(doc.createSolidColorBrush(doc.createColor(222, 12, 15, 159)));
// ARGB solid color filled rectangle, another way
XpsPath rect2 = doc.addPath(doc.createPathGeometry("M 20,210 L 220,210 220,300 20,300 Z"));
rect2.setFill(doc.createSolidColorBrush(doc.createColor(222, 12, 15, 159)));
// sRGB solid color filled rectangle
XpsPath rect3 = doc.addPath(doc.createPathGeometry("M 20,410 L 220,410 220,500 20,500 Z"));
rect3.setFill(doc.createSolidColorBrush(doc.createColor(12, 15, 159)));
// scRGB solid color filled rectangle
XpsPath rect4 = doc.addPath(doc.createPathGeometry("M 20,610 L 220,610 220,700 20,700 Z"));
rect4.setFill(doc.createSolidColorBrush(doc.createColor(0.08706f, 0.04706f, 0.05882f, 0.62353f)));
// CMYK (blue) solid color filled rectangle
XpsPath rect5 = doc.addPath(doc.createPathGeometry("M 20,810 L 220,810 220,900 20,900 Z"));
rect5.setFill(doc.createSolidColorBrush(
doc.createColor(getDataDir() + "uswebuncoated.icc", 1.0f, 1.000f, 0.000f, 0.000f, 0.000f)));
// Save resultant XPS document
doc.save(getOutputDir() + "ApplyDifferentColorSpaces_outXPS.xps");
// Applying transformations to the canvases in XPS document.
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "ApplyTransforms_outXPS.xps";
// Create main canvas, common for all page elements
XpsCanvas canvas1 = doc.addCanvas();
// Make left and top offsets in the main canvas
canvas1.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 20f, 10f));
// Create rectangle path geometry
XpsPathGeometry rectGeom = doc.createPathGeometry("M 0,0 L 200,0 200,100 0,100 Z");
// Create a fill for rectangles
XpsBrush fill = doc.createSolidColorBrush(doc.createColor(12, 15, 159));
// Add new canvas without any transformations to the main canvas
XpsCanvas canvas2 = canvas1.addCanvas();
// Create rectangle in this canvas and fill it
XpsPath rect = canvas2.addPath(rectGeom);
rect.setFill(fill);
// Add new canvas with translate transformation to the main canvas
XpsCanvas canvas3 = canvas1.addCanvas();
// Translate this canvas to position new rectangle below previous rectnagle
canvas3.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 0f, 200f));
// Translate this canvas to right side of page
canvas3.getRenderTransform().translate(500f, 0f);
// Create rectangle in this canvas and fill it
rect = canvas3.addPath(rectGeom);
rect.setFill(fill);
// Add new canvas with double scale transformation to the main canvas
XpsCanvas canvas4 = canvas1.addCanvas();
// Translate this canvas to position new rectangle below previous rectnagle
canvas4.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 0f, 400f));
// Scale this canvas
canvas4.getRenderTransform().scale(2f, 2f);
// Create rectangle in this canvas and fill it
rect = canvas4.addPath(rectGeom);
rect.setFill(fill);
// Add new canvas with rotation around a point transformation to the main canvas
XpsCanvas canvas5 = canvas1.addCanvas();
// Translate this canvas to position new rectangle below previous rectnagle
canvas5.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 0f, 800f));
// Rotate this canvas around a point on 45 degrees
canvas5.getRenderTransform().rotateAround(45f, new java.awt.geom.Point2D.Float(100f, 50f));
rect = canvas5.addPath(rectGeom);
rect.setFill(fill);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Change direction of text from left-to-right to right-to-left, as in Hebrew or Arabic texts, in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-text/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "AddTextRTL_out.xps";
// Add Text
XpsSolidColorBrush textFill = doc.createSolidColorBrush(Color.BLACK);
XpsGlyphs glyphs = doc.addGlyphs("Arial", 20, XpsFontStyle.Regular, 400f, 200f, "TEN. rof SPX.esopsA");
//Change direction of text from left-to-right to right-to-left
glyphs.setBidiLevel(1);
glyphs.setFill(textFill);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Apply opacity mask for texture pattern in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-transparency/
// Create new XPS Document
XpsDocument doc = new XpsDocument();
String outputFileName = "OpacityMask_out.xps";
//Add Canvas to XpsDocument instance
XpsCanvas canvas = doc.addCanvas();
// Rectangle with opacity masked by ImageBrush
XpsPath path = canvas.addPath(doc.createPathGeometry("M 10,180 L 228,180 228,285 10,285"));
path.setFill(doc.createSolidColorBrush(doc.createColor(255, 0, 0)));
path.setOpacityMask((XpsImageBrush)doc.createImageBrush(getDataDir() + "R08SY_NN.tif", new Rectangle2D.Float(0f, 0f, 128f, 192f),
new Rectangle2D.Float(0f, 0f, 64f, 96f)));
((XpsImageBrush)path.getOpacityMask()).setTileMode(XpsTileMode.Tile);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Using image utilities for adding images to XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/graphic-utilities/
String outputFileName = "UseImageUtilsXPS_out.xps";
// Create new XPS Document
XpsDocument doc = new XpsDocument();
try {
// Set first page's size.
doc.getPage().setWidth(540f);
doc.getPage().setHeight(220f);
// Draw the image box.
java.awt.geom.Rectangle2D.Float imageBox = new java.awt.geom.Rectangle2D.Float(10f, 10f, 200f, 200f);
XpsPath path = doc.addPath(doc.getUtils().createRectangle(imageBox));
path.setStroke(doc.createSolidColorBrush(doc.createColor(0, 0, 0)));
// Add an image to fit width.
path = doc.getUtils().createImage(getDataDir() + "R08LN_NN.jpg", imageBox, com.aspose.xps.ImageMode.FitToWidth);
// Prevent tiling.
((XpsImageBrush)path.getFill()).setTileMode(com.aspose.xps.XpsTileMode.None);
doc.add(path);
// Add other images using utils
doc.add(doc.getUtils().createImage(getDataDir() + "R08LN_NN.jpg", new Rectangle2D.Float(220f, 10f, 200f, 100f), com.aspose.xps.ImageMode.FitToHeight));
doc.add(doc.getUtils().createImage(getDataDir() + "R08LN_NN.jpg", new Rectangle2D.Float(430f, 10f, 100f, 200f), com.aspose.xps.ImageMode.FitToBox));
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
} finally {
// nothing to dispose explicitly here
}
// Using shape utilities for adding shapes to XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/graphic-utilities/
String outputFileName = "UseShapeUtilsXPS_out.xps";
// Create new XPS Document
XpsDocument doc = new XpsDocument();
try {
// Set first page's size.
doc.getPage().setWidth(650f);
doc.getPage().setHeight(240f);
// Draw a circle with center (120, 120) and radius 100.
XpsPath path = doc.createPath(doc.getUtils().createCircle(new java.awt.geom.Point2D.Float(120f, 120f), 100f));
path.setFill(doc.createSolidColorBrush(doc.createColor(0, 255, 0)));
doc.add(path);
// Inscribe a regular pentagon in the circle.
path = doc.createPath(doc.getUtils().createRegularInscribedNGon(5, new java.awt.geom.Point2D.Float(120f, 120f), 100f));
path.setFill(doc.createSolidColorBrush(doc.createColor(255, 0, 0)));
doc.add(path);
// Circumscribe a regular hexagon around the circle.
path = doc.createPath(doc.getUtils().createRegularCircumscribedNGon(6, new java.awt.geom.Point2D.Float(120f, 120f), 100f));
path.setStroke(doc.createSolidColorBrush(doc.createColor(255, 0, 255)));
path.setStrokeThickness(3f);
doc.add(path);
// Draw a sector of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
path = doc.createPath(doc.getUtils().createPieSlice(new java.awt.geom.Point2D.Float(340f, 120f), 100f, -45f, 45f));
path.setStroke(doc.createSolidColorBrush(doc.createColor(255, 0, 0)));
path.setStrokeThickness(5f);
doc.add(path);
// Draw a segment of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
path = doc.createPath(doc.getUtils().createCircularSegment(new java.awt.geom.Point2D.Float(340f, 120f), 100f, -45f, 45f));
path.setFill(doc.createSolidColorBrush(doc.createColor(0, 0, 0)));
doc.add(path);
// Draw a rectangle with the top left vertex (530, 20), width 100 units and height 200 units.
path = doc.createPath(doc.getUtils().createRectangle(new Rectangle2D.Float(530f, 20f, 100f, 200f)));
path.setStroke(doc.createSolidColorBrush(doc.createColor(255, 0, 0)));
doc.add(path);
// Draw an ellipse with center (580, 120) and radii 50 and 100.
path = doc.createPath(doc.getUtils().createEllipse(new java.awt.geom.Point2D.Float(580f, 120f), 50f, 100f));
path.setFill(doc.createSolidColorBrush(doc.createColor(255, 255, 0)));
doc.add(path);
doc.save(getOutputDir() + outputFileName);
} finally {
// nothing to dispose explicitly
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment