Select to view content in your preferred language

Create PDF ?

10364
76
07-20-2010 06:02 AM
CraigPerreault
Deactivated User
Does anyone have a code sample to create a PDF file using the Silverlight API?
0 Kudos
76 Replies
dotMorten_esri
Esri Notable Contributor
Take a look at this PDF libary: http://silverpdf.codeplex.com/
0 Kudos
MarkCederholm
Frequent Contributor
That's fine as long as you're not trying to render a map with a layer defined by a URL, because then you can't access the WriteableBitmap pixels to create the image to add to the PDF.  I haven't been able to find a true code-behind solution.  There may be a way to do it through a custom web service, but I haven't figured out how to do that either.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Using the SL4 print with a PDF driver is another option, but there is some UI side effect (the user has to select the PDF printer)
0 Kudos
MarkCederholm
Frequent Contributor
I did find a code-behind workaround to access the restricted Map content after all!  The basic approach is to export dynamic and tiled service layers through the REST API to image streams and overlay the results in a WriteableBitmap.  You can then render graphics layers and UIElements on top of that.  Probably more work than just using a PDF browser extension, but what the heck.

Anyway, I've created a first draft of a PDF creation class and added it to the code gallery as well as my web site:  http://www.pierssen.com/arcgis10/silverlight.htm 🙂
0 Kudos
KevinSesock
Emerging Contributor
I did find a code-behind workaround to access the restricted Map content after all!  The basic approach is to export dynamic and tiled service layers through the REST API to image streams and overlay the results in a WriteableBitmap.  You can then render graphics layers and UIElements on top of that.  Probably more work than just using a PDF browser extension, but what the heck.

Anyway, I've created a first draft of a PDF creation class and added it to the code gallery as well as my web site:  http://www.pierssen.com/arcgis10/silverlight.htm 🙂


I took a look at your code and have been playing around with it. Great work! Unfortunately, I've found a bug, or rather an intentional limitation (on the Microsoft side). Your line of code:
i = wbm.Pixels[0];
(located on line 252) is throwing a handled error of type System.Security.SecurityException. The error is: "WriteableBitmap has protected content. Pixel access is not allowed." After research, I found out this is due to accessing maps from other sites. For example, if your base layers are coming from ESRI (and maybe Bing, I'm not sure), you'll get this error. This is due to the fact that WriteableBitmap has been prevented from accessing anything in another domain. Setting clientaccess.xml or crossdomain.xml doesn't seem to help.

This is a built-in limitation with WriteableBitmap as a security measure.

More information is available here:

http://forums.silverlight.net/forums/t/148589.aspx

And here:

http://forums.silverlight.net/forums/t/118030.aspx

I'm looking through seeing how best to work around this issue, but due to the fact that some of our layers are coming from ESRI, I'm not sure how we can address this. Any ideas?
0 Kudos
MarkCederholm
Frequent Contributor
You need to study the code more closely.  In the download, that line is within a try block to test for restricted content.  If restricted content is encountered, then the workaround is invoked.
0 Kudos
KevinSesock
Emerging Contributor
I did find a code-behind workaround to access the restricted Map content after all!  The basic approach is to export dynamic and tiled service layers through the REST API to image streams and overlay the results in a WriteableBitmap.  You can then render graphics layers and UIElements on top of that.  Probably more work than just using a PDF browser extension, but what the heck.

Anyway, I've created a first draft of a PDF creation class and added it to the code gallery as well as my web site:  http://www.pierssen.com/arcgis10/silverlight.htm 🙂


I see where you're catching the System.Security.SecurityException from WriteableBitmap for the layer defined by the URL in your code. The situation I'm running into seems to be related to the fact that by the time the code is trying to do document.Save(_streamOutPDF) (on line 198), silverPDF (or rather PdfSharp) has already closed the document. I'm getting a "PrepareForSave: Number of deleted unreachable objects: 1" from what looks to be PdfSharp.Pdf.PdfDocument.Save, followed by two "A first chance exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll". I'm not familiar with SilverPDF, not to mention PdfSharp. Any thoughts on how to work around this?

Probably a newbie situation.
0 Kudos
MarkCederholm
Frequent Contributor
Interesting.  I haven't encountered that error myself, but I'm wondering if in your case it takes so long to create the image that the output stream provided by Silverlight is expiring.  Try asking for the output PDF file right at the start of the PDF creation stage and see if that solves the problem.
0 Kudos
KevinSesock
Emerging Contributor
Interesting.  I haven't encountered that error myself, but I'm wondering if in your case it takes so long to create the image that the output stream provided by Silverlight is expiring.  Try asking for the output PDF file right at the start of the PDF creation stage and see if that solves the problem.


That's what I'm doing, actually.

                SaveFileDialog savePDF = new SaveFileDialog();
                savePDF.DefaultExt = ".pdf";
                savePDF.ShowDialog();
                System.IO.Stream PDFstream = savePDF.OpenFile();
                PDFNew.SetOutputStream(PDFstream);
                PDFNew.DoExport();
0 Kudos