NiXPS
Monday, September 01, 2008
  Convert JPG to XPS
A lot of people ask for the ability that our NiXPS SDK can generate XPS files direct from all kinds of different file formats.

We are working on a general solution for all printable document formats.
But if the file you want to create an XPS from happens to be an image file, then this is quite easy to do with our SDK.

This is possible via our object model ever since our v2.0 SDK.
But to make it easier, as of NiXPS SDK v2.5.2, we are providing an extra convience function to do this.

In the following .NET example is shown you how to use our library to generate an XPS file from a JPEG file, with only a few lines of code:


static void TestGenerateXPSFromJPEG()
{
string lFile1 = "../../examples/testfiles/siena.jpg";
string lOutputFile = "../../examples/output/siena.xps";
NOHandler lPackage = NOPackage_createFromImage(System.Text.Encoding.UTF8.GetBytes(lFile1));
NOPackage_writePackageToFile(ref lPackage, System.Text.Encoding.UTF8.GetBytes(lOutputFile));
NOPackage_destroyPackage(ref lPackage);
}

(note: an 'NOPackage' is NiXPS SDK speak for an XPS file).

It all boils down to: NOPackage_createFromImage.
This call also works for easy conversion of PNG, HD Photo and TIFF files to XPS.

If you are using our library from C++ you can also make use of the same convenience function: NOPackage::createFromImage.

As an alternative you can access the XPS object model and program directly on top of the object level API of our library. Your code to achieve the same result would look like this:


void createXPSFromJPEG()
{
NOPackage *lPackage = NOPackage::createPackage(true);
NODocument lSourceDoc1=lPackage->getDocument(0);
NOPage lPage = lSourceDoc1.createPage();
NOImage lImage = lPage.addImage("../testfiles/siena.jpg");
NOXFixedPage lFixedPage = lPage.getFixedPage();
UInt32 lWidth = lImage.getWidthInPixels();
double lXFactor = lImage.getXResolution() / 96.;
UInt32 lHeight = lImage.getHeightInPixels();
double lYFactor = lImage.getYResolution() / 96.;

lFixedPage.intialize(lWidth*lXFactor,lHeight*lYFactor);

NOXCanvas lCanvas = lFixedPage.createCanvas(0);

UTF8String lFilename = lImage.getFileName();

char lRectangle[100];
sprintf(lRectangle,"M 0,0 L %f,0 %f,%f 0,%f z",
lWidth*lXFactor,lWidth*lXFactor,
lHeight*lYFactor,lHeight*lYFactor);

NOXPath lXPath = lCanvas.createPath(0);
lXPath.setData(NOString(lRectangle));

NOXCP_Brush lBrush = lXPath.createPathFill(0);
NOXImageBrush lImageBrush = lBrush.createImageBrush(0);

sprintf(lRectangle,"0,0,%f,%f",lWidth/lXFactor,lHeight/lYFactor);
lImageBrush.setViewbox(NOXViewBox(NOXStringBase(NOString(lRectangle))));
sprintf(lRectangle,"0,0,%f,%f",lWidth*lXFactor,lHeight*lYFactor);
lImageBrush.setViewport(NOXViewBox(NOString(lRectangle)));
lImageBrush.setTileMode(NiXPSObjects::None);
lImageBrush.setViewboxUnits(NiXPSObjects::Absolute);
lImageBrush.setViewportUnits(NiXPSObjects::Absolute);

NCommon::UTF8String lBaseString(lImage.getPartName());
lBaseString = "/" + lBaseString;
lImageBrush.setImageSource(NOString(lBaseString.c_str()));

lPackage->writePackageToFile(pOut);
NOPackage::destroyPackage(lPackage);
}


A bit more 'chatty', but it shows the flexibility of the object model.
You could use this also as a start to add more things on top (like text, more images, etc...).

If you want a trial version of our library: get it here.
You can also browse through the documentation of our library.

We appreciate any feedback and gladly help you out with all your questions.
 
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]