/// <summary> /// Converts XPS file to PDF. /// </summary> private void ConvertToPdf(string xpsFilename, string pdfFilename) { if (!File.Exists(xpsFilename)) { return; } // wait until file is released GcHelper.Collect(1000); try { var converter = new XPSToPdfConverter(); converter.Settings.EmbedCompleteFont = true; var document = converter.Convert(xpsFilename); document.Save(pdfFilename); document.Close(true); } catch (Exception ex) { MessageService.Current.Warn("Failed to convert to PDF: " + ex.Message); return; } OpenPdfResult(pdfFilename); }
public ActionResult XPSToPDF(string createPDF) { string dataPath = _hostingEnvironment.WebRootPath + @"/PDF/"; //Load XPS file to stream FileStream inputStream = new FileStream(dataPath + "XPStoPDF.xps", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); XPSToPdfConverter converter = new XPSToPdfConverter(); //Convert XPS document into PDF document PdfDocument document = converter.Convert(inputStream); //Save PDF document MemoryStream stream = new MemoryStream(); document.Save(stream); //Close the PDF document document.Close(true); stream.Position = 0; //Download the PDF document in the browser. FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf"); fileStreamResult.FileDownloadName = "XPSToPDF.pdf"; return(fileStreamResult); }
public byte[] XPSToPDF(Stream file) { _logger.LogDebug($"Start convert xps file"); byte[] buff; try { using (MemoryStream memoryStream = new MemoryStream()) { XPSToPdfConverter converter = new XPSToPdfConverter(); using (PdfDocument document = converter.Convert(file)) { document.Save(memoryStream); memoryStream.Position = 0; buff = memoryStream.ToArray(); _logger.LogDebug("FileInfoPDFService.XPSToPDF....OK"); return(buff); } } } catch (Exception ex) { _logger.LogError(ex.Message); } return(null); }
private void SaveFile(string filter) { //To Represent SaveFile Dialog Box SaveFileDialog m_SaveFileDialog = new SaveFileDialog(); //Assign the selected extension to the SavefileDialog filter m_SaveFileDialog.Filter = filter; //To display savefiledialog bool?istrue = m_SaveFileDialog.ShowDialog(); string filenamechanged; if (istrue == true) { //assign the filename to a local variable string extension = System.IO.Path.GetExtension(m_SaveFileDialog.FileName).TrimStart('.'); string fileName = m_SaveFileDialog.FileName; if (extension != "") { if (extension.ToLower() != ExportType.ToLower()) { fileName = fileName + "." + ExportType.ToLower(); } if (ExportType.ToLower() == "pdf") { filenamechanged = fileName + ".xps"; ExportSettings.IsSaveToXps = true; //Assigning exportstream from the saved file this.ExportSettings.FileName = filenamechanged; // Method to Export the SfDiagram (this.Info as IGraphInfo).Export(); var converter = new XPSToPdfConverter { }; var document = new PdfDocument(); document = converter.Convert(filenamechanged); document.Save(fileName); document.Close(true); } else { if (ExportType.ToLower() == "xps") { ExportSettings.IsSaveToXps = true; } //Assigning exportstream from the saved file this.ExportSettings.FileName = fileName; // Method to Export the SfDiagram (this.Info as IGraphInfo).Export(); } } } }
/* * NEED A CLIENTPROFILE VERSION * * private static bool ConvertorXLS(string filename, string pdf_filename) * { * ExcelEngine engine = new ExcelEngine(); * IApplication application = engine.Excel; * IWorkbook book = application.Workbooks.Open(filename); * * ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings(); * settings.TemplateDocument = new PdfDocument(); * settings.LayoutOptions = LayoutOptions.NoScaling; * settings.DisplayGridLines = GridLinesDisplayStyle.Invisible; * * ExcelToPdfConverter converter = new ExcelToPdfConverter(book); * PdfDocument pdf_document = pdf_document = converter.Convert(settings); * * pdf_document.Save(pdf_filename); * pdf_document.Close(true); * return true; * } */ private static bool ConvertorXPS(string filename, string pdf_filename) { XPSToPdfConverter converter = new XPSToPdfConverter(); PdfDocument pdf_document = converter.Convert(filename); pdf_document.Save(pdf_filename); pdf_document.Close(true); return(true); }
public static void Main(string[] args) { var memoryUsage = 1200000000; var step1 = 10000000; var step2 = 100000; Marshal.AllocHGlobal(memoryUsage); // warm up the application while (memoryUsage <= 1700000000) { memoryUsage += step1; try { Marshal.AllocHGlobal(step1); Console.WriteLine("Current Memory:" + memoryUsage); } catch (Exception e) { Console.WriteLine("Limit hit" + e.Message); } } while (true) { memoryUsage += step2; try { Marshal.AllocHGlobal(step2); Console.WriteLine("Current Memory:" + memoryUsage); var bytesOfXPS = File.ReadAllBytes(Directory.GetCurrentDirectory() + @"/Files/ToBeConverted.xps"); var converter = new XPSToPdfConverter(); var document = converter.Convert(bytesOfXPS); using (var stream = new MemoryStream()) { document.Save(stream); stream.Position = 0; document.Close(true); if (!Directory.Exists("PDF")) { Directory.CreateDirectory("PDF"); } File.WriteAllBytes($@"PDF/{Guid.NewGuid().ToString("N")}.pdf", stream.ToArray()); } } catch (Exception e) { Console.WriteLine("Limit hit" + e.Message); } } }
private void OnExport(object parameter) { var Extension = "BMP File (*.bmp)|*.bmp|WDP File (*.wdp)|*.wdp|JPG File(*.jpg)|*.jpg|PNG File(*.png)|*.png|TIF File(*.tif)|*.tif|GIF File(*.gif)|*.gif|XPS File(*.xps)|*.xps|PDF File(*.pdf)|*.pdf"; var saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = Extension; ExportSettings.ExportBackground = new System.Windows.Media.SolidColorBrush(Colors.White); if (saveFileDialog.ShowDialog() == true) { string filenamechanged; var extension = IOPath.GetExtension(saveFileDialog.FileName).TrimStart('.'); var fileName = saveFileDialog.FileName; if (extension != "") { if (extension.ToLower() == "pdf") { filenamechanged = fileName + ".xps"; ExportSettings.IsSaveToXps = true; this.ExportSettings.FileName = filenamechanged; // Method to Export the SfDiagram (this.Info as IGraphInfo).Export(); var converter = new XPSToPdfConverter { }; var document = new PdfDocument(); document = converter.Convert(filenamechanged); document.Save(fileName); document.Close(true); } else { if (extension.ToLower() == "xps") { ExportSettings.IsSaveToXps = true; } ExportType exportType; Enum.TryParse(extension.ToUpper(), out exportType); this.ExportSettings.ExportType = exportType; this.ExportSettings.FileName = fileName; (this.Info as IGraphInfo).Export(); } } } }
private void btnXPS2PDF_Click(object sender, EventArgs e) { if ((string)this.textBox1.Tag != String.Empty) { // Initialize XPS converter. XPSToPdfConverter converter = new XPSToPdfConverter(); // Convert XPS document into PDF document. PdfDocument document = converter.Convert((string)textBox1.Tag); // Save & close the pdf file. document.Save("Sample.pdf"); document.Close(true); //Message box confirmation to view the created document. if (MessageBox.Show("Do you want to view the PDF file?", "File has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo(@"Sample.pdf") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start(@"Sample.pdf"); #endif //Exit this.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } else { this.Close(); } } else { MessageBox.Show("Browse a XPS document and click the button to convert as a PDF."); } }
public ActionResult XPStoPDF(string InsideBrowser) { Stream readFile = new FileStream(ResolveApplicationDataPath(@"XPStoPDF.xps"), FileMode.Open, FileAccess.Read, FileShare.Read); XPSToPdfConverter converter = new XPSToPdfConverter(); //Convert XPS document into PDF document PdfDocument document = converter.Convert(readFile); //Save the pdf file if (InsideBrowser == "Browser") { return(document.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open)); } else { return(document.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)); } }
private async void GeneratePDF_Click(object sender, RoutedEventArgs e) { Stream xpsFile = typeof(XPSToPDF).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.XPStoPDF.xps"); //Create XPS to PDF converter XPSToPdfConverter converter = new XPSToPdfConverter(); //Convert the XPS document to PDF PdfDocument doc = converter.Convert(xpsFile); MemoryStream stream = new MemoryStream(); //Save the PDF document await doc.SaveAsync(stream); //Close the PDF document doc.Close(true); Save(stream, "XPSToPDF.pdf"); }
protected void btnXPStoPDF_Click(object sender, EventArgs e) { if (this.FileUpload1.HasFile) { string ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName).ToLower(); if (ext == ".xps") { //Convert the input XPS document to a PDF file # region Convert XPS to PDF Stream readFile = this.FileUpload1.PostedFile.InputStream; try { XPSToPdfConverter converter = new XPSToPdfConverter(); //Convert XPS document into PDF document PdfDocument document = converter.Convert(readFile); //Save the pdf file if (this.CheckBox1.Checked) { document.Save("XPStoPDF.pdf", Response, HttpReadType.Open); } else { document.Save("XPStoPDF.pdf", Response, HttpReadType.Save); } readFile.Close(); this.label1.Text = ""; document.Close(true); } catch (Exception) { this.label1.Text = "The input document could not be processed, Could you please email the document to [email protected] for troubleshooting?"; } # endregion } else { this.label1.Text = "Please choose a XPS file to convert to PDF"; } }