Skip to content

Instantly share code, notes, and snippets.

@aspose-xps
Last active October 30, 2018 20:51
Show Gist options
  • Save aspose-xps/b8f12d652363f445ceda6fb6e30b8e21 to your computer and use it in GitHub Desktop.
Save aspose-xps/b8f12d652363f445ceda6fb6e30b8e21 to your computer and use it in GitHub Desktop.
This Gist contains Java Examples for Aspose.XPS for Java
Aspose.XPS for Java
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// 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(doc.createImageBrush(dataDir + "R08LN_NN.jpg",
new Rectangle2D.Float(0f, 0f, 128f, 96f), new Rectangle2D.Float(0f, 0f, 64f, 48f)));
((XpsImageBrush)path.getFill()).setTileMode(XpsTileMode.Tile);
path.getFill().setOpacity(0.5f);
// Save resultant XPS document
doc.save(dataDir + "AddTiledImage_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// 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(doc.createImageBrush(dataDir + "QL_logo_color.tif", new Rectangle2D.Double(0f, 0f, 258.24f, 56.64f), new Rectangle2D.Double(50f, 20f, 193.68f, 42.48f)));
// Save resultant XPS document
doc.save(dataDir + "AddImage_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
// Create new XPS Document
XpsDocument doc = new XpsDocument(dataDir + "Aspose.xps");
// Insert an empty page at beginning of pages list
doc.insertPage(1, true);
// Save resultant XPS document
doc.save(dataDir + "AddPages_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Radial gradient stroked ellipse in the lower left
List<XpsGradientStop> stops = new LinkedList<XpsGradientStop>();
// Radial gradient stroked ellipse in the lower left
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"));
// New canvas
XpsCanvas canvas = doc.addCanvas();
path = canvas.addPath(path);
path.setStroke(doc.createRadialGradientBrush(new Point2D.Float(575f, 125f), new Point2D.Float(575f, 100f), 75f, 50f));
((XpsGradientBrush)path.getStroke()).setSpreadMethod(XpsSpreadMethod.Reflect);
((XpsGradientBrush)path.getStroke()).getGradientStops().addAll(stops);
stops.clear();
path.setStrokeThickness(12f);
// Save resultant XPS document
doc.save(dataDir + "AddEllipse_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// 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(dataDir + "uswebuncoated.icc", 1.0f, 1.000f, 0.000f, 0.000f, 0.000f)));
path.setStrokeThickness(12f);
// Save resultant XPS document
doc.save(dataDir + "AddRectangle_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
// Create new XPS Document
XpsDocument doc = new XpsDocument();
//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(dataDir + "AddText_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Add Text
XpsSolidColorBrush textFill = doc.createSolidColorBrush(Color.BLACK);
XpsGlyphs glyphs = doc.addGlyphs("Arial", 20, XpsFontStyle.Regular, 400f, 200f, "AVAJ rof SPX.esopsA");
glyphs.setBidiLevel(1);
glyphs.setFill(textFill);
// Save resultant XPS document
doc.save(dataDir + "AddEncodingText_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
XpsDocument doc = new XpsDocument();
// Just to demonstrate transparency
doc.addPath(doc.createPathGeometry("M120,0 H400 v1000 H120")).setFill(doc.createSolidColorBrush(Color.GRAY));
doc.addPath(doc.createPathGeometry("M300,120 h600 V420 h-600")).setFill(doc.createSolidColorBrush(Color.GRAY));
// 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(Color.BLUE));
// Add it to the current page
XpsPath path2 = doc.add(path1);
// path1 and path2 are the same as soon as path1 hasn't been placed inside any other element
// (which means that path1 had no parent element).
// Because of that rectangle's color on the page effectively turns to green
path2.setFill(doc.createSolidColorBrush(Color.GREEN));
// Now add path2 once again. Now path2 has parent. So path3 won't be the same as path2.
// Thus a new rectangle is painted on the page ...
XpsPath path3 = doc.add(path2);
// ... and we shift it 300 units lower ...
path3.setRenderTransform(doc.createMatrix(1, 0, 0, 1, 0, 300));
// ... and set red solid brush to fill it
path3.setFill(doc.createSolidColorBrush(Color.RED));
// Create new path4 with path2's geometry ...
XpsPath path4 = doc.addPath(path2.getData());
// ... shift it 300 units to the right ...
path4.setRenderTransform(doc.createMatrix(1, 0, 0, 1, 300, 0));
// ... and set blue solid fill
path4.setFill(doc.createSolidColorBrush(Color.BLUE));
// Add path4 once again.
XpsPath path5 = doc.add(path4);
// path4 and path5 are not the same again ...
// (move path5 300 units lower)
path5.setRenderTransform(path5.getRenderTransform().deepClone()); // to disconnect RenderTransform value from path4 (see next comment about Fill property)
path5.getRenderTransform().translate(0, 300);
// ... but if we set the opacity of Fill property, it will take effect on both path5 and path4
// because brush is a complex property value which remains the same for path5 and path4
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(1, 0, 0, 1, 600, 0));
// ... and set yellow solid fill
path6.setFill(doc.createSolidColorBrush(Color.YELLOW));
// Now add path6's clone ...
XpsPath path7 = doc.add(path6.deepClone());
// (move path5 300 units lower)
path7.setRenderTransform(path7.getRenderTransform().deepClone());
path7.getRenderTransform().translate(0, 300);
// ... and set opacity for path7
path7.getFill().setOpacity(0.8f);
// Now opacity effects independently as soon as property values are cloned along with the element
// The following code block is equivalent to the previous one.
// Add path6 itself. path6 and path7 are not the same. Although their Fill property values are the same
//XpsPath path7 = doc.Add(path6);
//path7.RenderTransform = path7.RenderTransform.Clone();
//path7.RenderTransform.Translate(0, 300);
// To "disconnect" path7's Fill property from path6's Fill property reassign it to its clone (or path6's Fill clone)
//path7.Fill = ((XpsSolidColorBrush)path7.Fill).Clone();
//path7.Fill.Opacity = 0.8f;
doc.save(dataDir + "WorkingWithTransparency_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
XpsDocument doc = new XpsDocument();
// New canvas
XpsCanvas canvas = doc.addCanvas();
// Rectangle in the middle left 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(1.0f, 0.0f, 0.0f)));
path.setOpacityMask(doc.createImageBrush(dataDir + "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(dataDir + "OpacityMask_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// 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)));
// Geometry for magenta grid VisualBrush
XpsPathGeometry pathGeometry = doc.createPathGeometry();
pathGeometry.addSegment(doc.createPolyLineSegment(
new Point2D.Float[] { new Point2D.Float(240f, 5f), new Point2D.Float(240f, 310f), new Point2D.Float(0f, 310f) }));
pathGeometry.getFigure(0).setStartPoint(new Point2D.Float(0f, 5f));
// Path for magenta grid
XpsPath gridPath = doc.createPath(pathGeometry);
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(gridPath);
// Red transparent rectangle in the middle top
XpsPath 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(dataDir + "AddGrid_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
XpsDocument doc = new XpsDocument();
// Horizontal gradient
List<XpsGradientStop> stops = new LinkedList<XpsGradientStop>();
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));
XpsPath path = doc.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
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);
stops.clear();
doc.save(dataDir + "HorizontalGradient.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
XpsDocument doc = new XpsDocument();
XpsPath path = doc.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
// Linear gradient
List<XpsGradientStop> stops = new LinkedList<XpsGradientStop>();
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));
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);
stops.clear();
doc.save(dataDir + "LinearGradient.xps");
// For complete examples and data files, please go to https://github.com/aspose-xps/Aspose.XPS-for-Java
XpsDocument doc = new XpsDocument();
XpsPath path = doc.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
// Vertical gradient
List<XpsGradientStop> stops = new LinkedList<XpsGradientStop>();
// Vertical gradient bellow
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));
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);
stops.clear();
doc.save(dataDir + "VrticalGradient.xps");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment