About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Tuesday, December 15, 2009

Converting XPS to Bitmap

One thing I had to do recently, for a SSRS report, was attach a 3 page pdf to a notice that is mailed out. Since, I can't just attach a pdf directly to the report, I figured I'll just take the images.

So, first thing I did was print the pdf to a XPS file. Now I just need to take that XPS file and convert it to some type of usable image that SSRS can recognize.

Found a solution on one of the MSDN message boards; however, I had to make some updates to it:

Code Snippet
  1. static public void SaveXpsPageToBitmap(string xpsFileName)
  2. {
  3.     DirectoryInfo di = new DirectoryInfo(xpsFileName);
  4.     XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
  5.     FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
  7.     //DocumentReferenceCollection drc = docSeq.References;
  8.     for (int i = 0; i < docSeq.DocumentPaginator.PageCount; i++)
  9.     {
  10.         DocumentPage docPage = docSeq.DocumentPaginator.GetPage(i);
  11.         BitmapImage bitmap = new BitmapImage();
  12.         RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)docPage.Size.Width, (int)docPage.Size.Height, 96, 96, PixelFormats.Default);
  14.         renderTarget.Render(docPage.Visual);
  16.         BitmapEncoder encoder = new BmpBitmapEncoder();
  17.         encoder.Frames.Add(BitmapFrame.Create(renderTarget));
  19.         FileStream pageOutStream = new FileStream(di.FullName.Substring(0,di.FullName.Length - 4) + "_Page_" + (i+1).ToString() + ".bmp", FileMode.Create, FileAccess.Write);
  20.         encoder.Save(pageOutStream);
  21.         pageOutStream.Close();
  22.     }
  23. }

So this program works nicely, only problem I currently have is that I have to convert these large size bitmaps (~5MB per page) to something more reasonable. Especially, since the SSRS deployment gives me a SOAP error because of the size. I'll probably just end up converting them to Jpeg later on.

1 comment:

free online image converter said...

Imverter is great software proficient to perform images transformations with the implementation of few simple steps. Just add files by selecting Add File button. Select any image format for conversion including JPG, BMP, TIFF, PNG, GIF, PDF, DWG and PCX. This give an industrial look to output converted images.