public static void SaveStreamAsPdf( object sender, Leadtools.Printer.EmfEventArgs e ) { try { ConsoleMethods.Info( "EMF Event: SaveStreamAsPdf" ); // Write the EMF as file to disk. const string format = "pdf"; string path = Path.Combine( GetOutputRootPath(), format + @"\", GetRandomFileName( format ) ); Directory.CreateDirectory( Path.GetDirectoryName( path ) ); // Create an instance of the LEADTOOLS DocumentWriter var docWriter = new DocumentWriter(); docWriter.BeginDocument( path, DocumentFormat.Pdf ); var page = new DocumentEmfPage() { EmfHandle = new Metafile( new MemoryStream( e.Stream.ToArray() ) ) .GetHenhmetafile() }; docWriter.AddPage( page ); docWriter.EndDocument(); ConsoleMethods.Success( "SaveStreamAsPdf: Complete" ); ConsoleMethods.Verbose( path ); } catch ( Exception ex ) { ConsoleMethods.Error( ex.Message, 4000 ); } }
public static void ExportPdfViaSvg(string filename) { var outputFile = Path.Combine(Leadtools.Demo.Support.Path.GetOutputPath(), Path.GetFileNameWithoutExtension(filename) + "_Svg.pdf"); // Setup a new RasterCodecs object using (var codecs = new RasterCodecs()) { // Check to see if we can use this method // https://www.leadtools.com/help/leadtools/v19/dh/co/leadtools.codecs~leadtools.codecs.rastercodecs~canloadsvg(string).html if (!codecs.CanLoadSvg(filename)) { Console.WriteLine("\nCannot load this file as SVG for conversion to PDF.\nSee: https://www.leadtools.com/help/leadtools/v19/dh/co/leadtools.codecs~leadtools.codecs.rastercodecs~canloadsvg(string).html#remarksSectionHeading"); return; } codecs.Options.RasterizeDocument.Load.Resolution = 300; // Get the number of pages in the input document var pageCount = codecs.GetTotalPages(filename); // Create a new instance of the LEADTOOLS Document Writer var docWriter = new DocumentWriter(); // Set the PDF options // https://www.leadtools.com/help/leadtools/v19/dh/ft/leadtools.forms.documentwriters~leadtools.forms.documentwriters.pdfdocumentoptions.html var pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions; if (pdfOptions != null) { pdfOptions.DocumentType = PdfDocumentType.Pdf; docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions); } // Create a new PDF document docWriter.BeginDocument(outputFile, DocumentFormat.Pdf); var message = ""; // Loop through all the pages for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++) { // Get the page as SVG // https://www.leadtools.com/help/leadtools/v19/dh/co/leadtools.codecs~leadtools.codecs.rastercodecs~loadsvg.html var page = new DocumentSvgPage(); var lastMessageLen = message.Length; message = string.Format("\rConverting page {0} of {1}", pageNumber, pageCount); var diff = lastMessageLen - message.Length; Console.Write("{0}{1}", message, (diff > 0 ? new string( ' ', diff ) : "")); using (page.SvgDocument = codecs.LoadSvg(filename, pageNumber, null)) { // Add the page docWriter.AddPage(page); } } Console.Write("\r{0}\r", new String(' ', message.Length)); // Finally, finish writing the PDF file on disk docWriter.EndDocument(); } }
public Stream PrintSeries(string userName, string[] seriesInstanceUIDs, PrintOptions options, string annotationData) { DocumentWriter documentWriterInstance = null; try { List <SeriesImage> seriesImages = _Exporter.GetSeriesImages(seriesInstanceUIDs); Dictionary <string, List <AnnContainer> > annotations = AddInsUtils.GetAnnotations(annotationData); documentWriterInstance = OpenDocumentWriterInstance(options.PageWidth, options.PageHeight, 300); string fileName = Path.GetTempFileName(); documentWriterInstance.BeginDocument(fileName, DocumentFormat.Pdf); foreach (SeriesImage si in seriesImages) { ProcessInstance(si, userName, options, annotations); { var page = new Leadtools.Document.Writer.DocumentWriterRasterPage(); page.Image = si.Image; documentWriterInstance.AddPage(page); } } documentWriterInstance.EndDocument(); MemoryStream ms = new MemoryStream(File.ReadAllBytes(fileName)); File.Delete(fileName); ms.Position = 0; return(ms); } finally { if (null != documentWriterInstance) { documentWriterInstance.EndDocument(); } } }
static void VirtualPrinter_EmfEvent(object sender, EmfEventArgs e) { string pdfPath = Path.Combine(@"F:\Output\", Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) ) + ".pdf"; Directory.CreateDirectory(Path.GetDirectoryName(pdfPath)); // Create an instance of the LEADTOOLS DocumentWriter Leadtools.Document.Writer.DocumentWriter docWriter = new DocumentWriter(); docWriter.BeginDocument(pdfPath, Leadtools.Document.Writer.DocumentFormat.Pdf); Leadtools.Document.Writer.DocumentWriterEmfPage page = new DocumentWriterEmfPage() { EmfHandle = new Metafile(e.Stream).GetHenhmetafile() }; docWriter.AddPage(page); docWriter.EndDocument(); }
public void Save() { try { _saving = true; if (_tempFiles.Count == 0) { MessageBox.Show("No pages where printed.", FrmMain._strTittle, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } Application.DoEvents(); string strName = _savename; DocumentFormat documentFormat = DocumentFormat.User; DocumentOptions documentOptions = null; PdfDocumentOptions PdfdocumentOptions = new PdfDocumentOptions(); InstallFonts(); string strExt = ""; if (_format.ToLower() == "pdf") { documentFormat = DocumentFormat.Pdf; documentOptions = new PdfDocumentOptions(); (documentOptions as PdfDocumentOptions).DocumentType = PdfDocumentType.Pdf; (documentOptions as PdfDocumentOptions).FontEmbedMode = DocumentFontEmbedMode.Auto; strExt = "pdf"; } if (_format.ToLower() == "doc") { documentFormat = DocumentFormat.Doc; documentOptions = new DocDocumentOptions(); (documentOptions as DocDocumentOptions).TextMode = DocumentTextMode.Framed; strExt = "doc"; } if (_format.ToLower() == "xps") { documentFormat = DocumentFormat.Xps; documentOptions = new XpsDocumentOptions(); strExt = "xps"; } if (_format.ToLower() == "text") { documentFormat = DocumentFormat.Text; documentOptions = new TextDocumentOptions(); strExt = "txt"; } if (!strName.Contains("." + strExt.ToLower())) { strName += "." + strExt.ToLower(); } if (_jobData != null) { strName = Path.GetDirectoryName(strName) + "\\(" + _jobData.IPAddress + ") " + Path.GetFileName(strName); } _fileSaved = strName; DocumentWriter documentWriter = new DocumentWriter(); documentWriter.SetOptions(documentFormat, documentOptions); documentWriter.BeginDocument(strName, documentFormat); foreach (string strFile in _tempFiles) { #if LEADTOOLS_V20_OR_LATER DocumentWriterEmfPage documentPage = new DocumentWriterEmfPage(); #elif LEADTOOLS_V19_OR_LATER DocumentEmfPage documentPage = new DocumentEmfPage(); #else DocumentPage documentPage = DocumentPage.Empty; #endif // #if LEADTOOLS_V19_OR_LATER Metafile metaFile = new Metafile(strFile); documentPage.EmfHandle = metaFile.GetHenhmetafile(); documentWriter.AddPage(documentPage); (new Metafile(documentPage.EmfHandle, true)).Dispose(); metaFile.Dispose(); } documentWriter.EndDocument(); _saved = true; _saving = false; } catch (Exception Ex) { _saving = false; MessageBox.Show(Ex.Message, FrmMain._strTittle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private DialogResult SavePrintedJobsDocument() { System.Windows.Forms.DialogResult result = DialogResult.Cancel; try { SaveFileDialog saveFileDialog = new SaveFileDialog(); string strFilter; #if LTV16_CONFIG strFilter = "(*.PDF Files)|*.pdf|(*.PDFA Files)|*.pdf|(*.Doc Files)|*.doc|(*.RTF Files)|*.rtf|(*.Text Files)|*.txt|(*.HTML Files)|*.html|(*.DOCX Files)|*.docx|(*.XPS Files)|*.xps"; #endif #if LEADTOOLS_V17_OR_LATER strFilter = "(*.PDF Files)|*.pdf|(*.PDFA Files)|*.pdf|(*.Doc Files)|*.doc|(*.RTF Files)|*.rtf|(*.Text Files)|*.txt|(*.HTML Files)|*.html|(*.DOCX Files)|*.docx|(*.XPS Files)|*.xps|(*.XLS Files)|*.xls"; #endif saveFileDialog.Filter = strFilter; result = saveFileDialog.ShowDialog(); if (result == DialogResult.OK) { Application.DoEvents(); Cursor = Cursors.WaitCursor; DocumentFormat documentFormat = DocumentFormat.User; DocumentOptions documentOptions = null; PdfDocumentOptions PdfdocumentOptions = new PdfDocumentOptions(); string fileName = saveFileDialog.FileName; switch (saveFileDialog.FilterIndex) { case 1: documentFormat = DocumentFormat.Pdf; documentOptions = new PdfDocumentOptions(); (documentOptions as PdfDocumentOptions).DocumentType = PdfDocumentType.Pdf; (documentOptions as PdfDocumentOptions).FontEmbedMode = DocumentFontEmbedMode.Auto; break; case 2: documentFormat = DocumentFormat.Pdf; documentOptions = new PdfDocumentOptions(); (documentOptions as PdfDocumentOptions).DocumentType = PdfDocumentType.PdfA; (documentOptions as PdfDocumentOptions).FontEmbedMode = DocumentFontEmbedMode.Auto; (documentOptions as PdfDocumentOptions).ImageOverText = true; break; case 3: documentFormat = DocumentFormat.Doc; documentOptions = new DocDocumentOptions(); (documentOptions as DocDocumentOptions).TextMode = DocumentTextMode.Framed; break; case 4: documentFormat = DocumentFormat.Rtf; documentOptions = new RtfDocumentOptions(); (documentOptions as RtfDocumentOptions).TextMode = DocumentTextMode.Framed; break; case 5: documentFormat = DocumentFormat.Text; documentOptions = new TextDocumentOptions(); (documentOptions as TextDocumentOptions).DocumentType = TextDocumentType.Unicode; (documentOptions as TextDocumentOptions).Formatted = true; break; case 6: documentFormat = DocumentFormat.Html; documentOptions = new HtmlDocumentOptions(); (documentOptions as HtmlDocumentOptions).FontEmbedMode = DocumentFontEmbedMode.Auto; break; case 7: documentFormat = DocumentFormat.Docx; documentOptions = new DocxDocumentOptions(); (documentOptions as DocxDocumentOptions).MaintainAspectRatio = true; (documentOptions as DocxDocumentOptions).PageRestriction = DocumentPageRestriction.Default; (documentOptions as DocxDocumentOptions).TextMode = DocumentTextMode.Framed; break; case 8: documentFormat = DocumentFormat.Xps; documentOptions = new XpsDocumentOptions(); break; #if LTV16_CONFIG case 9: SaveAsEmf(fileName); return(result); #endif #if LEADTOOLS_V17_OR_LATER case 9: documentFormat = DocumentFormat.Xls; documentOptions = new XlsDocumentOptions(); (documentOptions as XlsDocumentOptions).PageRestriction = DocumentPageRestriction.Relaxed; break; case 10: SaveAsEmf(fileName); return(result); #endif } DocumentWriter documentWriter = new DocumentWriter(); documentWriter.SetOptions(documentFormat, documentOptions); documentWriter.BeginDocument(fileName, documentFormat); foreach (IntPtr metaFile in _lstMetaFiles) { #if LEADTOOLS_V20_OR_LATER DocumentWriterEmfPage documentPage = new DocumentWriterEmfPage(); #elif LEADTOOLS_V19_OR_LATER DocumentEmfPage documentPage = new DocumentEmfPage(); #else DocumentPage documentPage = DocumentPage.Empty; #endif // #if LEADTOOLS_V19_OR_LATER int index = _lstMetaFiles.IndexOf(metaFile); documentPage.EmfHandle = metaFile; if (saveFileDialog.FilterIndex == 2) { documentPage.Image = _codec.Load(_tempFiles[index]); } documentWriter.AddPage(documentPage); } documentWriter.EndDocument(); if (ViewOutputFile) { System.Diagnostics.Process.Start(fileName); } } } catch (Exception Ex) { MessageBox.Show(Ex.Message, "LEADTOOLS Printer Demo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } return(result); }
public Stream PrintLayout(string userName, string seriesInstanceUID, Layout layout, PrintOptions options, string annotationData) { DocumentWriter documentWriterInstance = null; // // Check to see if a width was provide. If not we default to 1024; // if (options.LayoutImageWidth == 0) { options.LayoutImageWidth = 1024; } SeriesImage layoutImageWithInfo = null; using (RasterImage layoutImage = AddInsUtils.CreateLayoutImage(options.LayoutImageWidth, options.WhiteBackground)) { try { options.LayoutPatientInfo = true; Dictionary <string, List <AnnContainer> > annotations = AddInsUtils.GetAnnotations(annotationData); foreach (var box in layout.Boxes) { if (null == box.referencedSOPInstanceUID) { continue; } if (0 == box.referencedSOPInstanceUID.Count) { continue; } //only the first instance List <SeriesImage> seriesImages = _Exporter.GetInstanceImages(new string[] { box.referencedSOPInstanceUID[0] }); if (null == seriesImages) { continue; } if (0 == seriesImages.Count) { continue; } // layout patient info check whether to print the patient information on each image or on the whole layout image. if (!options.LayoutPatientInfo) { //only the first image ProcessInstance(seriesImages[0], userName, options, annotations); } else { // take one of those series image to print them at the bottom of the final resultant image. if (layoutImageWithInfo == null) { layoutImageWithInfo = seriesImages[0]; } } AddInsUtils.LayoutImage(layoutImage, seriesImages[0].Image, box); } documentWriterInstance = OpenDocumentWriterInstance(options.PageWidth, options.PageHeight, 300); string fileName = Path.GetTempFileName(); documentWriterInstance.BeginDocument(fileName, DocumentFormat.Pdf); { var page = new Leadtools.Document.Writer.DocumentWriterRasterPage(); var finalImage = layoutImage; if (options.LayoutPatientInfo) { // add text to the bottom of the resultant image. layoutImageWithInfo.Image = layoutImage; ProcessInstance(layoutImageWithInfo, userName, options, null); finalImage = layoutImageWithInfo.Image; } page.Image = finalImage; documentWriterInstance.AddPage(page); } documentWriterInstance.EndDocument(); MemoryStream ms = new MemoryStream(File.ReadAllBytes(fileName)); File.Delete(fileName); ms.Position = 0; return(ms); } finally { if (null != documentWriterInstance) { documentWriterInstance.EndDocument(); } } } return(null); }