NiXPS
Monday, October 13, 2008
  NiXPS SDK by example: selecting and enumerating objects on an XPS page

The NiXPS SDK provides a an object model of the XPS page content to its users.
This has the nice effect that objects can be refered to by their ID, and all sorts of other nice things become possible like selections etc...

I would like to illustrate some of these aspects via an example that I've sent to a customer the other day.
This customer is looking for ways to get hold of the geometrics of all the objects on an XPS page, and wants to do further calculations based on that.
This is fairly easy to do with our NiXPS SDK v2.5.3, and I'll describe the example and code here (in C++).

First - some housekeeping: opening a file, and getting a handler to page we're interested in:

void test_walk_content(ostream &os)
{
NOPackage *lPackage=NOPackage::readPackageFromFile("../testfiles/Virtual_PC_Technical_Overview_2007.xps");

// first document, first page
NODocument lDoc=lPackage->getDocument(0);
NOPage lPage=lDoc.getPage(0);


I use a test file here that I pulled from the internet.
Next, we need to make sure that the NiXPS library calculates the geometric information for the particular page.
Currently this is a by-product of rendering, so we instruct the library to render to a very small buffer.
(In v2.6 we are going to decouple geometric calculation from rendering.)

// render to calculate bounding boxes
UInt32 lBufferLength = 10 * 10 * 3;
UInt8 *lBuffer = (UInt8 *) malloc(lBufferLength);
memset(lBuffer,0,lBufferLength);
lPage.renderToImage(lBuffer,10,10,true);
free(lBuffer);


Next we select everything on the page, and get hold of the selections list, which is a NOSelection type:

// select all content on the page
lPage.selectAll();
// get selection
NOSelection lSelection = lPage.getCopyOfSelection();


With this NOSelection type, we can now enumerate all objects:

// enumerate the selection, get the boundng box (in XPS document coordinates), and
// print this out, together with the type of object and its ID
UInt32 lNumberOfSelectedObjects = lSelection.getNumberOfSelectedObjects();
UInt32 i=0; for (;i<lNumberOfSelectedObjects;i++)
{


For every selected object, we get the bounding box

NOBase lSelectedObject = lSelection.getSelectedObject(i);
NCommon::doubleRect lRect = lPage.getBoundingBox(lSelectedObject.mID);


And we find out the type, and store that in a string for printing it out later:

std::string lType="unknown";
switch (lSelectedObject.mType)
{
case xPath: lType="Path"; break;
case xGlyphs: lType="Glyphs"; break;
default: lType="SomethingElse"; break;
}


We now summerize everything by printing it out, this is also the end of our loop:

os << i << ": ID:" << lSelectedObject.mID << " Type:" << lType << " bbox [" << lRect.mTop << "," << lRect.mBottom << "," << lRect.mLeft << "," << lRect.mRight << "]" << std::endl;
}


and it also concludes our function

}


There is a lot of powerful functionality like this in our NiXPS SDK, everything is listed in our api documentation.
But an API reference doesn't really have the same illustrative power, as a real code example holds.
That's why we ship code examples in our SDK, and continue to enhance this by adding more samples to the SDK.
 
Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives
September 2006 / October 2006 / November 2006 / December 2006 / January 2007 / February 2007 / March 2007 / April 2007 / May 2007 / June 2007 / July 2007 / August 2007 / September 2007 / October 2007 / November 2007 / December 2007 / January 2008 / February 2008 / March 2008 / April 2008 / May 2008 / June 2008 / July 2008 / August 2008 / September 2008 / October 2008 / November 2008 / December 2008 / January 2009 / February 2009 / March 2009 / April 2009 / May 2009 / June 2009 / July 2009 / August 2009 / September 2009 / October 2009 / November 2009 / December 2009 / January 2010 / February 2010 / March 2010 / April 2010 / May 2010 / June 2010 / July 2010 / September 2010 / October 2010 / November 2010 / January 2011 /

NiXPS home
XPS info from the creators
    follow me on Twitter
    Add to Technorati Favorites

    Subscribe to
    Posts [Atom]