Example: create XPS and add hyperlink using NiXPS SDK
A customer asked me for a code sample to illustrate the creation of an XPS document, and the ability to make content link to a hyperlink.
It might be of interst to others, so I am posting it on the blog.
This sample works with NiXPS SDK 2.6.6, and uses .NET.
First: create an XPS package with a document:
NOPackage lPackage=NOPackage.createPackage(1,"out.xps");
Now: create a page and add content using the document model.
We do this by getting the root FixedPage, create a Canvas inside, and store a Glyphs element which can hold a string of text.
{
NOPage lPage=lPackage.getDocument(0).createPage();
NOXFixedPage lContent=lPage.getFixedPage();
lContent.intialize(8.3*96,11.7*96); // A4: 210x297mm or 8.3x11.7inch
NOXCanvas lCanvas=lContent.createCanvas(0);
lCanvas.intialize();
NOXGlyphs lGlyphs=lCanvas.createGlyphs(0);
We also need to create and supply a NOFont to be used. We load this up from disk, where we have stored Arial.ttf, an opentype font we would like to use.
We then fill the Glyphs.
NOFont lFont=lPage.addFont("../testfiles/Arial.ttf");
NOString lURI = new NOString("/" + lFont.getPartName());
lGlyphs.intialize(20, lURI.getHandler(), 300, 50);
lGlyphs.setFill(new NOString("#FF000000").getHandler());
lGlyphs.setUnicodeString(new NOString("Take me to http://nixps.com").getHandler());
Next: the link. We name the Glyphs, and add a NavigateURI attribute which gives us the intented behaviour to link to a website.
lGlyphs.setName(new NOString("page_1").getHandler());
lGlyphs.setFixedPageNavigateUri(new NOString("http://www.nixps.com").getHandler());
}
Now we're done. So all left is to write out the file and clean-up.
// write out and close
lPackage.writePackageToFile("out.xps");
NOPackage.destroyPackage(ref lPackage);
XAML with databinding to PDF with NiXPS
A lot of customers ask me how they can go from XAML with databinding to PDF.
Our SDK can be a great part of a solution for this, if you go over XPS.
Here is a great
article that outlines how you can go from XAML with databinding to XPS with WPF.