private static void saveAsXPS(string file, DocumentViewModel viewModel) { // Deselect shapes while saving. List <ShapeViewModelBase> selectedItems = new List <ShapeViewModelBase>(viewModel.vm_CanvasViewModel.SelectedItem.Shapes); viewModel.vm_CanvasViewModel.SelectedItem.Clear(); // Get a rectangle representing the page. Rectangle page = viewModel._CommandUtility.GetDocumentRectangle(); try { using (Package package = Package.Open(file, FileMode.Create)) { using (XpsDocument xpsDocument = new XpsDocument(package)) { // Write the document. System.Windows.Xps.XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument); xpsWriter.Write(page); } } } finally { // Reselect shapes. viewModel.vm_CanvasViewModel.SelectedItem.SelectShapes(selectedItems); } }
public static void SaveToPdf(UIElement ui, string name) { //MigraDoc.DocumentObjectModel.Document doc = new MigraDoc.DocumentObjectModel.Document(); //MigraDoc.Rendering.DocumentRenderer renderer = new DocumentRenderer(doc); //MigraDoc.Rendering.PdfDocumentRenderer pdfRenderer = new MigraDoc.Rendering.PdfDocumentRenderer(); //pdfRenderer.PdfDocument = pDoc; //pdfRenderer.DocumentRenderer = renderer; //using (MemoryStream ms = new MemoryStream()) //{ // pdfRenderer.Save(ms, false); // byte[] buffer = new byte[ms.Length]; // ms.Seek(0, SeekOrigin.Begin); // ms.Flush(); // ms.Read(buffer, 0, (int)ms.Length); //} string path = string.Format(name + "_{0}.pdf", DateTime.Now.ToString("MMddyyyy_hhmmss")); FileOperations.EnsureFileFolderExist(path); MemoryStream lMemoryStream = new MemoryStream(); Package package = Package.Open(lMemoryStream, FileMode.Create); System.Windows.Xps.Packaging.XpsDocument doc = new System.Windows.Xps.Packaging.XpsDocument(package); System.Windows.Xps.XpsDocumentWriter writer = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(doc); writer.Write(ui); doc.Close(); package.Close(); PdfSharp.Xps.XpsModel.XpsDocument pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream); PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, path, 0); }
private void cmdShowAllAnotations_Click(object sender, RoutedEventArgs e) { IList <Annotation> annotations = service.Store.GetAnnotations(); foreach (Annotation annotation in annotations) { // Check for text information. if (annotation.Cargos.Count > 1) { string base64Text = annotation.Cargos[1].Contents[0].InnerText; byte[] decoded = Convert.FromBase64String(base64Text); MemoryStream m = new MemoryStream(); m.Write(decoded, 0, decoded.Length); m.Position = 0; StreamReader r = new StreamReader(m); string annotationXaml = r.ReadToEnd(); MessageBox.Show(annotationXaml); } } PrintDialog dialog = new PrintDialog(); bool? result = dialog.ShowDialog(); if (result != null && result.Value) { System.Windows.Xps.XpsDocumentWriter writer = System.Printing.PrintQueue.CreateXpsDocumentWriter(dialog.PrintQueue); AnnotationDocumentPaginator adp = new AnnotationDocumentPaginator( ((IDocumentPaginatorSource)docReader.Document).DocumentPaginator, service.Store); writer.Write(adp); } }
private void DoThePrint() { FlowDocument copy = mathBox.CloneDocument(); foreach (var mathBox in copy.FindChildren <MathBox>()) { mathBox.BorderThickness = new Thickness(0); } // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, // and allowing the user to select a printer. // get information about the dimensions of the seleted printer+media. System.Printing.PrintDocumentImageableArea ia = null; System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator; // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); Thickness t = new Thickness(72); // copy.PagePadding; copy.PagePadding = new Thickness( Math.Max(ia.OriginWidth, t.Left), Math.Max(ia.OriginHeight, t.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom)); copy.ColumnWidth = double.PositiveInfinity; //copy.PageWidth = 528; // allow the page to be the natural with of the output device // Send content to the printer. docWriter.Write(paginator); } }
private void Imprimer() { DocumentPaginator Doc = MEP.Paginer(_Doc); // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, // and allowing the user to select a printer. // get information about the dimensions of the seleted printer+media. PrintDocumentImageableArea ia = null; System.Windows.Xps.XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { docWriter.Write(Doc); } }
private void DoThePrint(FlowDocument copy) { // Clone the source document's content into a new FlowDocument. // This is because the pagination for the printer needs to be // done differently than the pagination for the displayed page. // We print the copy, rather that the original FlowDocument. //System.IO.MemoryStream s = new System.IO.MemoryStream(); //TextRange source = new TextRange(document.ContentStart, document.ContentEnd); //source.Save(s, DataFormats.Xaml); //FlowDocument copy = new FlowDocument(); //TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd); //dest.Load(s, DataFormats.Xaml); // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, // and allowing the user to select a printer. var theme = ThemeManager.GetAppTheme(Hub.Instance.Settings.Theme); var accent = ThemeManager.GetAccent("VSLight"); ThemeManager.ChangeAppStyle(copy.Resources, accent, theme); // get information about the dimensions of the seleted printer+media. System.Printing.PrintDocumentImageableArea ia = null; System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator; // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); Thickness t = new Thickness(72); // copy.PagePadding; copy.PagePadding = new Thickness( Math.Max(ia.OriginWidth, t.Left), Math.Max(ia.OriginHeight, t.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom)); copy.ColumnWidth = double.PositiveInfinity; //copy.PageWidth = 528; // allow the page to be the natural with of the output device // Send content to the printer. docWriter.Write(paginator); } }
private void btnPrintRTB_Click(object sender, RoutedEventArgs e) { //Print RTB file try { FlowDocument document = rtbEditor.Document; //Make copy of FlowDocument for pritning - witch pictures string copyString = XamlWriter.Save(rtbEditor.Document); FlowDocument kopiaFlowDocument = XamlReader.Parse(copyString) as FlowDocument; MemoryStream memorySrteam = new MemoryStream(); //Przygotowanie się do przechowywania zawartości w pamięci System.Printing.PrintDocumentImageableArea ia = null; System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia); //tu następuje otwarcie okno dialogowego drukowania który zwraca prarametr - referencje: ia //parametr (ia) reprezentuje informacje o obszarze obrazowania i wymiarze nośnika. if (docWriter != null && ia != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)kopiaFlowDocument).DocumentPaginator; ///Dzielenie zawartości na strony. //Change size PageSize and PagePadding of document - for CanvasSize paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); //Ustawia rozmiar stron zgodnie z wymiarem fizycznym kartki papieru. (793/1122) Thickness t = new Thickness(margines + kopiaFlowDocument.PagePadding.Left); //Ustawiam marginesy wydruku w PagePadding. Należy zastosować korektę o starowy margines drukarki . Tu daje tyle co RichTextBox Domyślnie pobiera z drukarki i wynoszą (5,0,5,0) piksela kopiaFlowDocument.PagePadding = new Thickness( //Ustawienie nowego obszaru zadrukowania strony PagePadding Math.Max(ia.OriginWidth, t.Left), Math.Max(ia.OriginHeight, t.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom)); kopiaFlowDocument.ColumnWidth = double.PositiveInfinity; //Ustawienie szerokości kolumny drukowanej. double.PositiveInfinity - reprezentuje nieskończoności dodatniej. To pole jest stałe. // Send content to the printer. docWriter.Write(paginator); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); memorySrteam.Dispose(); } catch (Exception ex) { ErrorMessage(ex); } }
private void DoThePrint(FlowDocument document) { // Clone the source document's content into a new FlowDocument. // This is because the pagination for the printer needs to be // done differently than the pagination for the displayed page. // We print the copy, rather that the original FlowDocument. MemoryStream s = new MemoryStream(); TextRange source = new TextRange(document.ContentStart, document.ContentEnd); source.Save(s, DataFormats.Xaml); FlowDocument doc = GetPrintFlowDocument(); ForceRenderFlowDocument(doc); // get information about the dimensions of the seleted printer+media. System.Printing.PrintDocumentImageableArea ia = null; System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)doc).DocumentPaginator; // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); Thickness t = new Thickness(72); // copy.PagePadding; doc.PagePadding = new Thickness( Math.Max(ia.OriginWidth, t.Left), Math.Max(ia.OriginHeight, t.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom)); doc.ColumnWidth = double.PositiveInfinity; //copy.PageWidth = 528; // allow the page to be the natural with of the output device // Send content to the printer. docWriter.Write(paginator); } }
private void Print() { // create header var cntlHeader = new TechDataHeaderControl(); cntlHeader.SetContents(this.thePackage, this.theDefs, this.theDefaultLang, this.theSubmodel); // create footer var cntlFooter = new TechDataFooterControl(); cntlFooter.SetContents(this.thePackage, this.theDefs, this.theDefaultLang, this.theSubmodel); // render this var bitmapHeader = RenderControl(cntlHeader, 600, 200, false); var bitmapFooter = RenderControl(cntlFooter, 600, 100, false); // create middle part var cntlProps = new TechDataPropertiesControl(); cntlProps.SetContents(this.thePackage, this.theDefs, this.theDefaultLang, this.theSubmodel); var document = cntlProps.CreateFlowDocument( this.thePackage, this.theDefs, this.theDefaultLang, this.theSubmodel); // Clone the source document's content into a new FlowDocument. // This is because the pagination for the printer needs to be // done differently than the pagination for the displayed page. // We print the copy, rather that the original FlowDocument. System.IO.MemoryStream s = new System.IO.MemoryStream(); TextRange source = new TextRange(document.ContentStart, document.ContentEnd); source.Save(s, DataFormats.Xaml); FlowDocument copy = new FlowDocument(); TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd); dest.Load(s, DataFormats.Xaml); // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, // and allowing the user to select a printer. // get information about the dimensions of the seleted printer+media. System.Printing.PrintDocumentImageableArea ia = null; System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { // some more definitions var pdefs = new PimpedPaginator.Definition(); pdefs.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); pdefs.HeaderHeight = 200; pdefs.FooterHeight = 100; pdefs.RepeatTableHeaders = true; // draw header pdefs.Header = (DrawingContext context, Rect bounds, int pageNr) => { context.DrawImage(bitmapHeader, bounds); }; // draw footer pdefs.Footer = (DrawingContext context, Rect bounds, int pageNr) => { context.DrawImage(bitmapFooter, bounds); }; // make suitable paginator var paginator = new PimpedPaginator(copy, pdefs); // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); Thickness t = new Thickness(72); copy.PagePadding = new Thickness( Math.Max(ia.OriginWidth, t.Left), Math.Max(ia.OriginHeight, t.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom)); copy.ColumnWidth = double.PositiveInfinity; // Send content to the printer. docWriter.Write(paginator); } }
public static Boolean Creer(DocumentPaginator document, String Chemin) { if (String.IsNullOrWhiteSpace(Chemin)) { return(false); } try { // Convertion du document en xps MemoryStream strm = new MemoryStream(); Package pkg = Package.Open(strm, FileMode.OpenOrCreate); String pack = "pack://" + Guid.NewGuid().ToString() + ".xps"; PackageStore.AddPackage(new Uri(pack), pkg); XpsDocument xpsDoc = new XpsDocument(pkg, CompressionOption.Maximum, pack); XpsSerializationManager xpsSM = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false); xpsSM.SaveAsXaml(document); // Enregistrement du xps dans un fichier tmp String tmpXpsFileName = Path.Combine(Path.GetTempPath(), "Gestion_" + DateTime.Now.Ticks + ".xps"); XpsDocument xpsd = new XpsDocument(tmpXpsFileName, FileAccess.ReadWrite); System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd); xw.Write(xpsDoc.GetFixedDocumentSequence()); xpsd.Close(); PdfSharp.Xps.XpsConverter.Convert(tmpXpsFileName, Chemin, 0); //// Convertion du xps en pdf //PdfDocument pdfDoc = new PdfDocument(); //pdfDoc.LoadFromXPS(tmpXpsFileName); //Byte[] pdfFile; //using (MemoryStream stream = new MemoryStream()) //{ // pdfDoc.SaveToStream(stream); // pdfFile = stream.ToArray(); //} //// Ecriture du fichier //ByteArrayToFile(Chemin, pdfFile); //Byte[] pdfFile; //using (MemoryStream stream = new MemoryStream()) //{ // PdfSharp.Xps.XpsModel.XpsDocument xpsDocument = PdfSharp.Xps.XpsModel.XpsDocument.Open(tmpXpsFileName); // PdfSharp.Xps.XpsConverter.ConvertToPdfInMemory(xpsDocument, stream, 0); // xpsDocument.Close(); // pdfFile = stream.ToArray(); //} // Ecriture du fichier //ByteArrayToFile(Chemin, pdfFile); // Suppression du fichier tmp File.Delete(tmpXpsFileName); return(true); } catch { } return(false); }
protected MatrixBase() { this.CommandCopyToClipboard = new DelegateCommand( this.CopyToClipboard, () => { return(this.HasData); }); this.CommandPrint = new DelegateCommand( () => { var section = Helpers.FormatHelper.MatrixToFlowDocumentSectionWithTable(this); var flowDocument = new System.Windows.Documents.FlowDocument(section); flowDocument.FontSize = 11d; System.Windows.Controls.PrintDialog pd = new System.Windows.Controls.PrintDialog(); System.Printing.PrintTicket pt = new System.Printing.PrintTicket(); pt.PageOrientation = System.Printing.PageOrientation.Landscape; pd.PrintTicket = pd.PrintQueue.MergeAndValidatePrintTicket(pd.PrintQueue.DefaultPrintTicket, pt).ValidatedPrintTicket; System.Windows.Documents.IDocumentPaginatorSource fdd = flowDocument; flowDocument.PageWidth = pd.PrintableAreaWidth; flowDocument.PageHeight = pd.PrintableAreaHeight; flowDocument.ColumnWidth = pd.PrintableAreaWidth; flowDocument.PagePadding = new Thickness(30.0, 50.0, 20.0, 30.0); flowDocument.IsOptimalParagraphEnabled = true; flowDocument.IsHyphenationEnabled = true; var ms = new System.IO.MemoryStream(); using (var pkg = System.IO.Packaging.Package.Open(ms, System.IO.FileMode.Create)) { using (System.Windows.Xps.Packaging.XpsDocument doc = new System.Windows.Xps.Packaging.XpsDocument(pkg)) { System.Windows.Xps.XpsDocumentWriter writer = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(doc); writer.Write(fdd.DocumentPaginator); } } ms.Position = 0; var pkg2 = System.IO.Packaging.Package.Open(ms); // Read the XPS document into a dynamically generated // preview Window var url = new Uri("memorystream://printstream"); System.IO.Packaging.PackageStore.AddPackage(url, pkg2); try { using (System.Windows.Xps.Packaging.XpsDocument doc = new System.Windows.Xps.Packaging.XpsDocument(pkg2, System.IO.Packaging.CompressionOption.SuperFast, url.AbsoluteUri)) { System.Windows.Documents.FixedDocumentSequence fds = doc.GetFixedDocumentSequence(); Window wnd = new Window(); wnd.Title = string.Format("Предварительный просмотр :: {0}", this.Header); wnd.Owner = Application.Current.MainWindow; System.Windows.Media.TextOptions.SetTextFormattingMode(wnd, System.Windows.Media.TextFormattingMode.Display); wnd.Padding = new Thickness(2); wnd.Content = new System.Windows.Controls.DocumentViewer() { Document = fds, }; wnd.ShowDialog(); } } finally { System.IO.Packaging.PackageStore.RemovePackage(url); } }, () => { return(this.HasData); }); this.CommandRefresh = new DelegateCommand(this.Build, () => this.HasData); }
public FlowDocumentPackage(string xamlFlowDocument) { toDispose = new List <IDisposable>(); TempFolder = new DirectoryInfo(TempPath + "\\XamlToPDF\\" + DateTime.Now.Ticks.ToString()); if (!TempFolder.Exists) { TempFolder.Create(); } /*Stream = File.Create(TempFolder.FullName + "\\a.xps"); * toDispose.Add(Stream); * * Package = Package.Open(Stream, FileMode.Create, FileAccess.ReadWrite); * toDispose.Add(Package); * * pack = "pack://" + DateTime.Now.Ticks + ".xps"; * PackageStore.AddPackage(new Uri(pack), Package); * * * * XpsDoc = new XpsDocument(Package, CompressionOption.NotCompressed, pack);*/ XpsDoc = new XpsDocument(TempFolder.FullName + "\\a.xps", FileAccess.ReadWrite, CompressionOption.NotCompressed); toDispose.Add(new InlineDisposable(() => XpsDoc.Close())); System.Windows.Xps.XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(XpsDoc); xamlFlowDocument = ReplaceImages(writer, xamlFlowDocument); flowDoc = XamlServices.Parse(xamlFlowDocument) as FlowDocument; IDocumentPaginatorSource src = flowDoc as IDocumentPaginatorSource; if (double.IsNaN(flowDoc.PageHeight)) { flowDoc.PageHeight = 792; } if (double.IsNaN(flowDoc.PageWidth)) { flowDoc.PageWidth = 612; } /*XPolicy = new XpsPackagingPolicy(XpsDoc); * toDispose.Add(XPolicy); * * XSrm = new XpsSerializationManager(XPolicy, false); * toDispose.Add(XSrm);*/ DocumentPaginator pgn = src.DocumentPaginator; //XSrm.SaveAsXaml(pgn); writer.Write(pgn); var seq = XpsDoc.GetFixedDocumentSequence(); DocumentReference reff = seq.References.First(); Document = reff.GetDocument(true); }