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 = wbm.Pixels[0];
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.
SaveFileDialog savePDF = new SaveFileDialog(); savePDF.DefaultExt = ".pdf"; savePDF.ShowDialog(); System.IO.Stream PDFstream = savePDF.OpenFile(); PDFNew.SetOutputStream(PDFstream); PDFNew.DoExport();
Sorry, I should have been more specific. In PDFExporter.cs, comment out all the _streamOutPDF lines and move your SaveFileDialog lines to just before the document.Save line. If that's actually the problem, then I'll revise the class accordingly.
Close. Unfortunately SaveFileDialog has to be called by a user-initiated event, like a Button_Click event. I created a new property for a SaveFileDialog object, and passed it in from my Button_Click event after showing the dialog, then created the stream from the SaveFileDialog.OpenFile() method inside PDFExporter.cs. Thanks for the suggestion, I now have a PDF file to work with.Unfortunately, I seem to be missing layers. How exactly does your class determine the order of your layers?
using PdfSharp.Pdf; private void PDFbutton_MouseLeftButtonUp(object sender, MouseEventArgs e) { SaveFileDialog savePDF = new SaveFileDialog(); savePDF.Filter = "PDF file format|*.pdf"; savePDF.DefaultExt = ".pdf"; if (savePDF.ShowDialog() == true) { System.IO.Stream PDFstream = savePDF.OpenFile(); PDFExporter PDFNew = new PDFExporter(); PDFNew.SetMap(myMap); PDFNew.SetOutputStream(PDFstream); PDFNew.DoExport(); } }
I got it....Thanks Kevin! Great Tool.
#region PDF creation AssemblyPart apSilverPDF; System.IO.Stream PDFStream; /// <summary> /// Handle "Create PDF" button click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click(object sender, RoutedEventArgs e) { LoadPDFLibraryAndSaveFile(); } /// <summary> /// Load the silverPDF library if it hasn't been loaded already /// or use the library if already loaded /// </summary> private void LoadPDFLibraryAndSaveFile() { SaveFileDialog savePDF = new SaveFileDialog(); savePDF.Filter = "PDF file format|*.pdf"; savePDF.DefaultExt = ".pdf"; if (savePDF.ShowDialog() == true) { //catch the error if the user tries to save over a file that is currently open try { PDFStream = savePDF.OpenFile(); //if silverPDF library is not loaded already, load it if (apSilverPDF == null) { LoadPDFLibrary(); } else { //if the library is loaded, just save the PDF file SavePDF(); } } catch (IOException ioe) { //tell the user the file is open MessageBox.Show("Selected PDF file is open. Close the file and try again!", "Open File Error", MessageBoxButton.OK); } } } /// <summary> /// Send a request to load the silverPDF library dynamically /// </summary> private void LoadPDFLibrary() { WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); client.OpenReadAsync(new Uri("silverPDF.dll", UriKind.Relative)); } /// <summary> /// Load the library and use it to create the PDF /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { if ((e.Error == null) && (e.Cancelled == false)) { apSilverPDF = new AssemblyPart(); apSilverPDF.Load(e.Result); SavePDF(); } else { MessageBox.Show("There was a problem loading PDF Library", "PDF Library Loading Error", MessageBoxButton.OK); } } /// <summary> /// Save the PDF File /// </summary> private void SavePDF() { PDFExporter PDFNew = new PDFExporter(); //add map to the PDF PDFNew.SetMap(Map); //add navigation with scale bar to the PDF PDFNew.AddElement(navigationGrid); PDFNew.SetOutputStream(PDFStream); PDFNew.DoExport(); } #endregion
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { // If the app is running outside of the debugger then report the exception using // the browser's exception mechanism. On IE this will display it a yellow alert // icon in the status bar and Firefox will display a script error. if (!System.Diagnostics.Debugger.IsAttached) { // NOTE: This will allow the application to continue running after an exception has been thrown // but not handled. // For production applications this error handling should be replaced with something that will // report the error to the website and stop the application. e.Handled = true; //This is required to swallow an error when saving a file over a file that is open. //This is a bug, and application crashes if this line is not there. if (e.ExceptionObject.StackTrace.IndexOf("SaveFileStream") != -1) return; Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); }); } }
private void Print(object sender, RoutedEventArgs e) { SaveFileDialog savePDF = new SaveFileDialog(); savePDF.Filter = "PDF file format|*.pdf"; savePDF.DefaultExt = ".pdf"; if (savePDF.ShowDialog() == true) { try { System.IO.Stream PDFstream = savePDF.OpenFile(); PDFExporter PDFNew = new PDFExporter(); PDFNew.SetMap(MyMap); PDFNew.SetOutputStream(PDFstream); PDFNew.DoExport(); } catch (IOException ioe) { //tell user the file is open MessageBox.Show("Selected PDF file is open. Close file and try again!", "Open File Error", MessageBoxButton.OK); } } }
public void SetMap(Map TheMap) { _Map = TheMap; }
public void SetMap(Map TheMap) { _Exporter.SetMap(TheMap); }
I am trying to implement a PDF printing option and have chosen to use the sample code from Mark C. The problem I am having is that I get the "Map Not Set" error in the PDFExporter IntStartExport() catch. So, my map is essentially coming up as null.My question then, is how do I set my map resource for this?This is what I did:First, I downloaded the SilverPDF DLL and added it to my project as a reference.I then added a UserControl (ExportPDF.XAML) to my application and used the code from Mark C's download. I also added the PDFExporter.cs to my project (both the user control and the PDFExporter are in the root of the project). I created a namespace to my project and call the usercontrol from the namespace, ie: <showTest:ExportPDF x:Name="whatever" ></showTest:ExportPDF>There doesn't seem to be a Map="" property available to bind the map to the user control. I've played with this from PDFExporter.cs: public void SetMap(Map TheMap) { _Map = TheMap; } And this from ExportPDF.xaml.cs: public void SetMap(Map TheMap) { _Exporter.SetMap(TheMap); } For reference my project is named "showTest" and my map element name is "MyMap"I'm no code junky, so I apologize for not being able to get too specific. I also will not claim that the answer is not totally obvious, because it probably is... Thanks for any and all help!!!!
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.