Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 15, 2024 08:29
Show Gist options
  • Save aspose-com-gists/bcee4811da013bc7a78dd41af768a9d2 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/bcee4811da013bc7a78dd41af768a9d2 to your computer and use it in GitHub Desktop.
Gists for Aspose.Page for Java
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
ImageFormat imageFormat = ImageFormat.PNG;
// Initialize PsDocument with PostScript file
PsDocument document = new PsDocument(dataDir + "input.ps");
// If you want to convert Postscript file despite of minor errors set this flag
boolean suppressErrors = true;
//Initialize options object with necessary parameters.
// Default image format is PNG and it is not mandatory to set it in ImageSaveOptions
// Default image size is 595x842 and it is not mandatory to set it in ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(suppressErrors);
// But if you need to specify size and image format use constructor with parameters
// ImageSaveOptions options = new ImageSaveOptions((new Dimension(595, 842), ImageFormat.Jpeg, suppressErrors);
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
//options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});
// Save PS to images bytes array, one bytes array for one page of the input document.
byte[][] imagesBytes = document.saveAsImage(device, options);
int i = 0;
for (byte [] imageBytes : imagesBytes) {
String imagePath = dataDir + "PSToImage" + i + "." + imageFormat.toString().toLowerCase();
FileOutputStream fs = new FileOutputStream(imagePath);
try {
fs.write(imageBytes, 0, imageBytes.length);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
fs.close();
}
i++;
}
//Review errors
if (suppressErrors) {
for (Exception ex : options.getExceptions()) {
System.out.println(ex.getMessage());
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize PDF output stream
FileOutputStream pdfStream = new FileOutputStream(dataDir + "PStoPDF.pdf");
// Initialize PsDocument with PostScript file
PsDocument document = new PsDocument(dataDir + "input.ps");
// If you want to convert Postscript file despite of minor errors set this flag
boolean suppressErrors = true;
//Initialize options object with necessary parameters.
// Default page size is 595x842 and it is not mandatory to set it in PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions(suppressErrors);
// But if you need to specify size and image format use following line
// PdfSaveOptions options = new PdfSaveOptions(suppressErrors, new Dimension(595, 842));
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
//options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});
// Save PS as PDF
document.saveAsPdf(dataDir + "PStoPDF.pdf", options);
//Review errors
if (suppressErrors) {
for (Exception ex : options.getExceptions()) {
System.out.println(ex.getMessage());
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.BmpSaveOptions options = new com.aspose.xps.rendering.BmpSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[]{1, 2, 6});
// Save XPS document as images bytes array. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.saveAsImage(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize XPS input stream
// Load XPS document form the stream
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.JpegSaveOptions options = new com.aspose.xps.rendering.JpegSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save XPS document as images bytes array. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.saveAsImage(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize PDF output stream
FileOutputStream pdfStream = new FileOutputStream(dataDir + "XPStoPDF.pdf");
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.PdfSaveOptions options = new com.aspose.xps.rendering.PdfSaveOptions();
options.setJpegQualityLevel(100);
options.setImageCompression(com.aspose.xps.rendering.PdfImageCompression.Jpeg);
options.setTextCompression(com.aspose.xps.rendering.PdfTextCompression.Flate);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save XPS document as PDF
document.saveAsPdf(dataDir + "XPStoPDF.pdf", options);
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.PngSaveOptions options = new com.aspose.xps.rendering.PngSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save XPS document as images bytes array. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.saveAsImage(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.TiffSaveOptions options = new com.aspose.xps.rendering.TiffSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save XPS document as images bytes array. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.saveAsImage(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
//Initialize document
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-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
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-page/Aspose.Page-for-Java
//Initialize document
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");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-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-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// 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-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Create new XPS Document
final XpsDocument doc = new XpsDocument();
try {
// Set first page's size.
doc.getPage().setWidth(540f);
doc.getPage().setHeight(220f);
// Draw the image box.
Rectangle2D imageBox = new Rectangle2D.Float(10f, 10f, 200f, 200f);
XpsPath path = doc.addPath(doc.getUtils().createRectangle(imageBox));
path.setStroke(doc.createSolidColorBrush(Color.BLACK));
// Add an image to fit width.
path = doc.getUtils().createImage(dataDir + "R08LN_NN.jpg", imageBox, ImageMode.FitToWidth);
// Prevent tiling.
((XpsImageBrush)path.getFill()).setTileMode(XpsTileMode.None);
doc.add(path);
// Add an image to fit width.
doc.add(doc.getUtils().createImage(
dataDir + "R08LN_NN.jpg", new Rectangle2D.Float(220f, 10f, 200f, 100f), ImageMode.FitToHeight));
// Add an image to fit width.
doc.add(doc.getUtils().createImage(
dataDir + "R08LN_NN.jpg", new Rectangle2D.Float(430f, 10f, 100f, 200f), ImageMode.FitToBox));
// Save resultant XPS document
doc.save(dataDir + "UseImageUtilsXPS_out.xps");
} finally {
if (doc != null)
doc.close();
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// 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-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
String fontDir = dataDir + "necessary_fonts/";
// Open an XPS Document
final XpsDocument doc = new XpsDocument(dataDir + "Sample3.xps");
try {
// Create a font
final InputStream fontStream = new FileInputStream(fontDir + "arialbd.ttf");
try {
// Create options for conversion to PDF
PdfSaveOptions options = new PdfSaveOptions();
// Set the filter for the pages that need conversion
options.setPageNumbers(new int[] { 2, 6, 7, 13 });
// Add the event handler that will execute right before the conversion of each page
options.getBeforePageSavingEventHandlers().add(new NavigationInjector(doc.createFont(fontStream), options.getPageNumbers()));
// Save resultant XPS document
doc.saveAsPdf(dataDir + "ModifyPageOnConversion_out.pdf", options);
} finally {
if (fontStream != null)
fontStream.close();
}
} finally {
if (doc != null)
doc.close();
}
/**
* The class to handle the before-page event while converting an XPS document.
*/
public static class NavigationInjector extends BeforePageSavingEventHandler
{
private final XpsFont _font;
private List<Integer> _pageNumbers;
public NavigationInjector(XpsFont font, int[] pageNumbers)
{
_font = font;
if (pageNumbers == null || pageNumbers.length == 0)
return;
// Turn the page number array into a sorted collection of unique values.
SortedMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
for (int pn : pageNumbers)
map.put(pn, 0);
_pageNumbers = new ArrayList<Integer>(map.keySet());
}
/**
* The action itself to be triggered on a before-page event.
* @param args The even arguments.
*/
@Override
public void handle(BeforeSavingEventArgs<PageAPI> args)
{
PageAPI api = args.getElementAPI();
XpsGlyphs glyphs;
// For all pages in the output PDF except the first one...
if (args.getOutputPageNumber() > 1)
{
// ...insert a hyperlink to the first page...
glyphs = api.createGlyphs(_font, 15f, 5f, api.getHeight() - 10f, "[First]");
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(_pageNumbers == null ? 1 : _pageNumbers.get(0)));
api.add(glyphs);
// ...and to the previous page.
glyphs = api.createGlyphs(_font, 15f, 60f, api.getHeight() - 10f, "[Prev]");
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(
_pageNumbers == null ? args.getAbsolutePageNumber() - 1 : _pageNumbers.get(args.getOutputPageNumber() - 2)));
api.add(glyphs);
}
// For all pages in the output PDF except the last one...
if ((_pageNumbers != null && args.getOutputPageNumber() < _pageNumbers.size()) ||
(_pageNumbers == null && args.getOutputPageNumber() < api.getTotalPageCount()))
{
// ...insert a hyperlink to the next page...
glyphs = api.createGlyphs(_font, 15f, 110f, api.getHeight() - 10f, "[Next]");
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(
_pageNumbers == null ? args.getAbsolutePageNumber() + 1 : _pageNumbers.get(args.getOutputPageNumber())));
api.add(glyphs);
// ...and to the last page.
glyphs = api.createGlyphs(_font, 15f, 160f, api.getHeight() - 10f, "[Last]");
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(
_pageNumbers == null ? api.getTotalPageCount() : _pageNumbers.get(_pageNumbers.size() - 1)));
api.add(glyphs);
}
// Insert a page number in the bottom-right corner.
glyphs = api.createGlyphs(_font, 15f, api.getWidth() - 20f, api.getHeight() - 10f, Integer.toString(args.getOutputPageNumber()));
glyphs.setFill(api.createSolidColorBrush(Color.BLACK));
api.add(glyphs);
// Add an outline entry to display the links to the converted pages in the navigation pane of a PDF viewer.
api.addOutlineEntry(MessageFormat.format("Page {0}", args.getOutputPageNumber()), 1, args.getAbsolutePageNumber());
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Create new XPS document
final XpsDocument document = new XpsDocument();
try {
// Set a custom job-level print ticket
document.setJobPrintTicket(new JobPrintTicket(
// Specify input bin.
new JobInputBin(InputBin.InputBinOption.Manual.clone().add(
InputBin.FeedFace.FaceDown, InputBin.FeedDirection.LongEdgeFirst, new InputBin.MediaSheetCapacity(100))),
// Specify output bin.
new JobOutputBin(new OutputBin.OutputBinOption(OutputBin.BinType.Sorter),
new OutputBin.OutputBinOption(OutputBin.BinType.Stacker, new OutputBin.MediaSheetCapacity(100))),
// Specify page orientation.
new PageOrientation(PageOrientation.PageOrientationOption.Landscape),
// Specify duplex mode fof the output.
new JobDuplexAllDocumentsContiguously(Duplex.DuplexOption.twoSidedLongEdge(Duplex.DuplexMode.Automatic)),
// Specify the color settings for the output.
new PageOutputColor(PageOutputColor.PageOutputColorOption.Grayscale(0, 8))));
// Save the document with the custom job-level print ticket.
document.save(dataDir + "output1.xps");
} finally {
if (document != null)
document.close();
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// 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-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// 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-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Create new XPS Document
final 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 Point2D.Float(120f, 120f), 100f));
path.setFill(doc.createSolidColorBrush(Color.GREEN));
doc.add(path);
// Inscribe a regular pentagon in the circle.
path = doc.createPath(doc.getUtils().createRegularInscribedNGon(5, new Point2D.Float(120f, 120f), 100f));
path.setFill(doc.createSolidColorBrush(Color.RED));
doc.add(path);
// Circumscribe a regular hexagon around the circle.
path = doc.createPath(doc.getUtils().createRegularCircumscribedNGon(6, new Point2D.Float(120f, 120f), 100f));
path.setStroke(doc.createSolidColorBrush(Color.MAGENTA));
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 Point2D.Float(340f, 120f), 100f, -45f, 45f));
path.setStroke(doc.createSolidColorBrush(Color.RED));
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 Point2D.Float(340f, 120f), 100f, -45f, 45f));
path.setFill(doc.createSolidColorBrush(Color.BLACK));
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(Color.RED));
doc.add(path);
// Draw an ellipse with center (580, 120) and radii 50 and 100.
path = doc.createPath(doc.getUtils().createEllipse(new Point2D.Float(580f, 120f), 50f, 100f));
path.setFill(doc.createSolidColorBrush(Color.YELLOW));
doc.add(path);
doc.save(dataDir + "UseShapeUtilsXPS_out.xps");
} finally {
if (doc != null)
doc.close();
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// 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-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// 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-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize document
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-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Create a new XPS document
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-page/Aspose.Page-for-Java
// instantiate License object
com.aspose.page.License license = new com.aspose.page.License();
// license file path information
license.setLicense("Aspose.Total.Java.lic");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// Initialize License Instance
com.aspose.page.License license = new com.aspose.page.License();
// Set license from Stream
license.setLicense(new java.io.FileInputStream("Aspose.Total.Java.lic"));
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// 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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment