Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 19, 2022 09:17
Show Gist options
  • Save aspose-com-gists/e2a414e32be0dbc3b04f3b070f51d51c to your computer and use it in GitHub Desktop.
Save aspose-com-gists/e2a414e32be0dbc3b04f3b070f51d51c to your computer and use it in GitHub Desktop.
Add Text or Insert Image in XPS Document Programmatically in 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 egaP.esopsA");
glyphs.setBidiLevel(1);
glyphs.setFill(textFill);
// Save resultant XPS document
doc.save("AddEncodingText_out.xps");
// 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("AddText_out.xps");
// 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("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("AddImage_out.xps");
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// 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("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("AddTiledImage_out.xps");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment