Combine XPS documents: add header and footer
In a document workflow it is very common that different parts of a document come from separate sources.
The typical example is the document flow of invoices.
Generally invoices are generated by software that is tied to the accountancy department of a company. This invoice is then printed on company letterhead, and send out.
Ideally: to keep documents fully electronic, this last stage, combine invoice and letterhead, needs to be done in software. And both XPS and NiXPS can come to the rescue here.
First of all, getting an XPS version of an invoice out of your accounting software is very easy. With help of the free virtual XPS printer that is delivered with windows you can print from your software to an XPS file.
Of course, in a document flow invoice generation is much more automated. Chances are the accounting software can be customized to print, or even an integration with .NET is possible, so XPS can be generated on the fly.
Then combining this invoice with a company logo and a footer can be done with our upcoming NiXPS SDK v2.6.2 and the new function: copyPageOnTop().
Literally in a few lines of C# it is possible to combine any number of XPS pages, the NiXPS SDK takes care of the housekeeping needed to do this.
Let me walk you through an example for this; We have an invoice (invoice.xps) file, which we will combine with our company logo (logo.xps) and a footer (footer.xps).
First thing to do in the function: open up all our XPS files:
public static void Main(string[] args)
{
NOPackage lInvoice = NOPackage.readPackageFromFile("../testfiles/invoice.xps");
NOPackage lLogo = NOPackage.readPackageFromFile("../testfiles/logo.xps");
NOPackage lFooter = NOPackage.readPackageFromFile("../testfiles/footer.xps");
The logo.xps has the logo in page 1. Our invoice is also a one pager; So to combine them we will copy page 1 of the logo.xps on top of page 1 of invoice.xps.
This will return a handle to a NOXCanvas:
NOXCanvas lLogoCanvas=lInvoice.getDocument(0).getPage(0).copyPageOnTop(lLogo.getDocument(0).getPage(0));
This NOXCanvas is a handle to the content that was inserted.
This allows to do some customization: in this example we will apply a transformation matrix to scale the logo to 60%, and move it to position (30,30):
lLogoCanvas.setRenderTransform("0.6,0,0,0.6,30,30");
Same thing for the footer, copy page on top, and transform:
NOXCanvas lFooterCanvas = lInvoice.getDocument(0).getPage(0).copyPageOnTop(lFooter.getDocument(0).getPage(0));
lFooterCanvas.setRenderTransform("0.72,0,0,0.72,0,100");
Write out the file and clean-up:
lInvoice.writePackageToFile("../output/out.xps");
NOPackage.destroyPackage(ref lInvoice);
NOPackage.destroyPackage(ref lLogo);
NOPackage.destroyPackage(ref lFooter);
}
And that's it!
To download the SDK in trial: fill out this form, you'll receive a download link (and no spam).