Draw onto an XPS file
Here's another example of the capabilities of our new NiXPS SDK v2.6.
The NiXPS SDK v2.6 has the ability to do processing on existing XPS files. This means that you can modify, or in this case decorate, XPS files that are coming f.i. from a different source.
In this example I demonstrate how easy it is to get access to the content stream of an XPS page, and as an example draw a red square on the page.
This code could be a nice starting point for an application that would like to add a barcode, draw a watermark, etc...
So, to start off, we are going to process a given XPS file, and write the result out to another location (in C#):
public static void test_open_drawrectangle_save()
{
string pIn="../../examples/testfiles/Office2007_Powerpoint_Drawing_Fills_Texture.xps";
string pOut="../../examples/output/open_drawrectangle_save.xps";
Next, read the XPS file, and get a handler to the page, and page content.
NOPackage lPackage = NOPackage.readPackageFromFile(pIn);
// get first page
NOPage lPage = lPackage.getDocument(0).getPage(0);
// get content stream
NOXFixedPage lContent = lPage.getFixedPage();
Now, let's do some drawing. We add a path, with a square geometry and in a transparant red
// append a rectangle
// a rectangle is a path, we will add this in a seperate canvas (group)
// and we will put this canvas at the end of the stack, so it will be displayed on top
NOXPath lPath=lContent.createCanvas(lContent.getNumCanvas()).createPath(0);
lPath.intialize();
// geometry: (100,100)-(200,200)
lPath.setData("M 100,100 L 200,100 L 200,200 L 100,200 Z");
// color: ARGB - so full red, transparant overlay
lPath.setFill("#99FF0000");
That's about it. Now save and close:
// write the XPS file out
lPackage.writePackageToFile(pOut);
NOPackage.destroyPackage(ref lPackage);
}
The result is the input file, with a red, transparent square that lies over on the first page.
As the NiXPS SDK uses XPS natively, the only change you'll find in the file is that square, all the rest is exactly the smae as the input document.