public void Print_WPF_Preview(FrameworkElement wpf_Element)
        {
            string sPrintFilename = "report.xps";

            if (File.Exists(sPrintFilename) == true)
            {
                File.Delete(sPrintFilename);
            }

            XpsDocument doc = new XpsDocument(sPrintFilename, FileAccess.ReadWrite);

            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

            SerializerWriterCollator outputDocument = writer.CreateVisualsCollator();

            outputDocument.BeginBatchWrite();
            outputDocument.Write(wpf_Element);
            outputDocument.EndBatchWrite();

            FixedDocumentSequence preview = doc.GetFixedDocumentSequence();

            //prozor koji trea da otvorimo
            //trea ga prikazati

            PrintWindow printWindow = new PrintWindow(preview);

            printWindow.Show();

            doc.Close();
            writer         = null;
            outputDocument = null;
            doc            = null;
        }
        public static string SaveXPS2(Visual page, Visual page1, bool isSaved, string strType)
        {
            string containerName = GetXPSFromDialog(isSaved, strType);

            if (containerName != null)
            {
                try
                {
                    File.Delete(containerName);
                }
                catch (Exception e)
                {
                    //  MessageWindow3.Show(e.Message);
                }

                XpsDocument          _xpsDocument = new XpsDocument(containerName, FileAccess.Write);
                XpsDocumentWriter    xpsdw        = XpsDocument.CreateXpsDocumentWriter(_xpsDocument);
                VisualsToXpsDocument xpsdw1       = (VisualsToXpsDocument)xpsdw.CreateVisualsCollator();

                xpsdw1.Write(page);
                xpsdw1.Write(page1);//写入XPS文件
                //xpsdw1.Write(page);
                xpsdw1.EndBatchWrite();
                //xpsdw.Write(((IDocumentPaginatorSource)page).DocumentPaginator);
                _xpsDocument.Close();
            }
            return(containerName);
            //}
            //else
            //{
            //    return false;
            //}
            //}
        }
Exemple #3
0
        public static void PrintWpfPreview(FrameworkElement element)
        {
            string printFileName = "print_preview.xps";

            if (File.Exists(printFileName))
            {
                File.Delete(printFileName);
            }

            //System.Printing.PrintDocumentImageableArea ia = null;
            using XpsDocument doc = new XpsDocument(printFileName, FileAccess.ReadWrite);

            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

            SerializerWriterCollator outputDocument = writer.CreateVisualsCollator();

            outputDocument.BeginBatchWrite();
            outputDocument.Write(element);
            outputDocument.EndBatchWrite();

            FixedDocumentSequence preview   = doc.GetFixedDocumentSequence();
            DocumentPaginator     paginator = preview.DocumentPaginator;

            preview.DocumentPaginator.PageSize = new Size(200, 100);
            var         a           = preview.DocumentPaginator.PageCount;
            PrintWindow printWindow = new PrintWindow(preview);

            printWindow.Show();

            doc.Close();
            writer         = null;
            outputDocument = null;
        }
Exemple #4
0
        private async void MainWindow_ContentRendered(object sender, EventArgs e)
        {
            await Task.Delay(1000);

            var pageSize = new Size(1280 * 8, 720 * 8);
            var document = new FixedDocument();

            document.DocumentPaginator.PageSize = pageSize;

            var fixedPage = new FixedPage
            {
                Width  = pageSize.Width,
                Height = pageSize.Height,
            };

            for (int i = 0; i < 100; i++)
            {
                fixedPage.Children.Add(new Border
                {
                    Width      = pageSize.Width,
                    Height     = pageSize.Height,
                    Background = new VisualBrush
                    {
                        Visual = DemoImage,
                    }
                });
            }
            fixedPage.Measure(pageSize);
            fixedPage.Arrange(new Rect(new Point(), pageSize));
            fixedPage.UpdateLayout();

            // Add page to document
            var pageContent = new PageContent();

            ((IAddChild)pageContent).AddChild(fixedPage);
            document.Pages.Add(pageContent);

            // Send to the printer.
            var pd = new PrintDialog();

            pd.PrintDocument(document.DocumentPaginator, "正在打印……");

            return;

            using var _localPrintServer = new LocalPrintServer();
            var _currentPrintQueue = _localPrintServer.DefaultPrintQueue;

            _xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(_currentPrintQueue);
            //_xpsDocumentWriter.WritingProgressChanged += XpsDocumentWriter_WritingProgressChanged;
            //_xpsDocumentWriter.WritingPrintTicketRequired += XpsDocumentWriterOnWritingPrintTicketRequired;
            //_xpsDocumentWriter.WritingCancelled += XpsDocumentWriterOnWritingCancelled;
            //_xpsDocumentWriter.WritingCompleted += XpsDocumentWriterOnWritingCompleted;
            _vToXspd = (VisualsToXpsDocument)_xpsDocumentWriter.CreateVisualsCollator();
            _vToXspd.BeginBatchWrite();
            for (int i = 0; i < 100; i++)
            {
                _vToXspd?.WriteAsync(RootPanel);
            }
            _vToXspd?.EndBatchWrite();
        }
Exemple #5
0
        private void saveVisuals(XpsDocumentWriter xpsdw, List <Visual> vc)
        {
            VisualsToXpsDocument vtoxps = (VisualsToXpsDocument)xpsdw.CreateVisualsCollator();

            foreach (Visual v in vc)
            {
                vtoxps.Write(v);
            }

            vtoxps.EndBatchWrite();
        }
Exemple #6
0
        /// <summary>
        /// 印刷ダイアログを出さずに既定のプリンタで画面のグリッドを印刷します。
        /// </summary>
        private void PrintXpsDocument2()
        {
            var lps = new LocalPrintServer();
            var q   = lps.DefaultPrintQueue;
            XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(q);

            VisualsToXpsDocument vs = (VisualsToXpsDocument)xps.CreateVisualsCollator();

            vs.Write(this.grid1);
            vs.EndBatchWrite();
        }
Exemple #7
0
        public void Create()
        {
            var uri    = new Uri("pack://application:,,,/ypi_client_order.xps");
            var stream = System.Windows.Application.GetResourceStream(uri).Stream;

            System.IO.Packaging.Package package1 = System.IO.Packaging.Package.Open(stream);
            System.IO.Packaging.PackageStore.AddPackage(uri, package1);
            var xpsDoc = new XpsDocument(package1, System.IO.Packaging.CompressionOption.Maximum, uri.AbsoluteUri);
            var fixedDocumentSequenceO = xpsDoc.GetFixedDocumentSequence();

            MemoryStream xpsStream       = new MemoryStream();
            Package      package2        = Package.Open(xpsStream, FileMode.Create);
            string       memoryStreamUri = "memorystream://printstream";
            Uri          packageUri      = new Uri(memoryStreamUri);

            PackageStore.AddPackage(packageUri, package2);
            XpsDocument       doc    = new XpsDocument(package2, CompressionOption.Fast, memoryStreamUri);
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

            System.Windows.Documents.Serialization.SerializerWriterCollator vxpsd = writer.CreateVisualsCollator();
            int pageno = 1;

            foreach (System.Windows.Documents.DocumentReference r in fixedDocumentSequenceO.References)
            {
                System.Windows.Documents.FixedDocument d = r.GetDocument(false);
                foreach (System.Windows.Documents.PageContent pc in d.Pages)
                {
                    System.Windows.Documents.FixedPage fixedPage = pc.GetPageRoot(false);
                    double width           = fixedPage.Width;
                    double height          = fixedPage.Height;
                    System.Windows.Size sz = new System.Windows.Size(width, height);
                    fixedPage.Measure(sz);
                    fixedPage.Arrange(new System.Windows.Rect(new System.Windows.Point(), sz));
                    fixedPage.UpdateLayout();
                    ContainerVisual newpage = new ContainerVisual();
                    newpage.Children.Add(fixedPage);
                    newpage.Children.Add(CreateWatermark(400, 400, "hello world"));
                    pageno++;
                    vxpsd.Write(newpage);

                    //PackageStore.RemovePackage(packageUri);
                    //doc.Close();
                }
            }

            doc.Close();
            xpsStream.Position = 0;
            Package     finalPackage          = Package.Open(xpsStream, FileMode.Open);
            XpsDocument finalDoc              = new XpsDocument(finalPackage, CompressionOption.Fast, memoryStreamUri);
            var         fixedDocumentSequence = xpsDoc.GetFixedDocumentSequence();
        }
Exemple #8
0
        private static void Print(FrameworkElement kundeInfo, ScrollViewer scrollViewer)
        {
            if (kundeInfo == null && scrollViewer == null)
            {
                return;
            }

            PrintDocumentImageableArea area      = null;
            PageRangeSelection         selection = PageRangeSelection.AllPages;
            PageRange         range  = new PageRange();
            XpsDocumentWriter xpsdw1 = PrintQueue.CreateXpsDocumentWriter("Corinor prisforslag", ref area, ref selection, ref range);

            if (xpsdw1 == null)
            {
                return;
            }

            //TODO: DEBUG
            //if (File.Exists("D:\\test.xps")) File.Delete("D:\\test.xps");
            //XpsDocument _xpsDocument = new XpsDocument("D:\\test.xps", FileAccess.ReadWrite);
            //XpsDocumentWriter xpsdw = XpsDocument.CreateXpsDocumentWriter(_xpsDocument);


            Thickness Margins = new Thickness(96);

            double leftMargin   = Margins.Left - area.OriginWidth;
            double topMargin    = Margins.Top - area.OriginHeight;
            double rightMargin  = Margins.Right - (area.MediaSizeWidth - area.ExtentWidth - area.OriginWidth);
            double bottomMargin = Margins.Bottom - (area.MediaSizeHeight - area.ExtentHeight - area.OriginHeight);
            Size   outputSize   = new Size(
                area.MediaSizeWidth - leftMargin - rightMargin,
                area.MediaSizeHeight - topMargin - bottomMargin);

            SerializerWriterCollator batchPrinter = xpsdw1.CreateVisualsCollator();

            batchPrinter.BeginBatchWrite();

            if (kundeInfo != null)
            {
                printKundeinfo(batchPrinter, kundeInfo, outputSize, leftMargin, topMargin);
            }
            if (scrollViewer != null && scrollViewer.Content != null)
            {
                printScrollViewer(batchPrinter, scrollViewer, outputSize, leftMargin, topMargin);
            }

            batchPrinter.EndBatchWrite();

            //TODO: Debug
            //_xpsDocument.Close();
        }
        //</SnippetWriteToXpsWithVisual>

        //<SnippetCreateAndWriteToVisualsCollator>
        private void SaveVisuals(XpsDocumentWriter xpsdw, List <Visual> vc)
        {
            // Setup for writing multiple visuals
            VisualsToXpsDocument vToXpsD = (VisualsToXpsDocument)xpsdw.CreateVisualsCollator();

            // Iterate through all visuals in the collection
            foreach (Visual v in vc)
            {
                vToXpsD.Write(v);   //Write each visual to single page
            }

            // End writing multiple visuals
            vToXpsD.EndBatchWrite();
        }
        public static void Save(MainViewModel viewModel, string path)
        {
            using (XpsDocument xpsdoc = new XpsDocument(path, FileAccess.Write))
            {
                XpsDocumentWriter    xpsdw   = XpsDocument.CreateXpsDocumentWriter(xpsdoc);
                VisualsToXpsDocument vToXpsD = (VisualsToXpsDocument)xpsdw.CreateVisualsCollator();

                var fixedDoc = new FixedDocument();

                var objReportToPrint = new ToPrint();

                var ReportToPrint = objReportToPrint as UserControl;
                ReportToPrint.DataContext = viewModel;

                vToXpsD.Write(ReportToPrint);
                vToXpsD.EndBatchWrite();
            }
        }
        //</SnippetWriteAsyncToXpsWithVisual>

        private void SaveVisualsAsync(XpsDocumentWriter xpsdw, List <Visual> vc)
        {
            _xpsdwActive = xpsdw;

            xpsdw.WritingCompleted +=
                new WritingCompletedEventHandler(AsyncSaveCompleted);

            xpsdw.WritingProgressChanged +=
                new WritingProgressChangedEventHandler(AsyncSavingProgress);

            // Setup for writing multiple visuals
            VisualsToXpsDocument vToXpsD = (VisualsToXpsDocument)xpsdw.CreateVisualsCollator();

            _batchProgress = 0;
            _activeVtoXPSD = vToXpsD;

            // Iterate through all visuals in the collection
            foreach (Visual v in vc)
            {
                vToXpsD.WriteAsync(v);  // Write each visual to single page.
            }
        }// end:SaveVisualsAsync()
Exemple #12
0
        public static void DoItTTT()
        {
            string filename    = "c:\\tmp\\goof.xps";
            string newFilename = "c:\\tmp\\new_goof.xps";

            XpsDocument xpsOld = new XpsDocument(filename, System.IO.FileAccess.Read);

            System.Windows.Documents.FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();
            System.IO.Packaging.Package container = System.IO.Packaging.Package.Open(newFilename, System.IO.FileMode.Create);
            XpsDocumentWriter           writer    = XpsDocument.CreateXpsDocumentWriter(new XpsDocument(container));

            System.Windows.Documents.Serialization.SerializerWriterCollator vxpsd = writer.CreateVisualsCollator();
            int pageno = 1;

            foreach (System.Windows.Documents.DocumentReference r in seqOld.References)
            {
                System.Windows.Documents.FixedDocument d = r.GetDocument(false);
                foreach (System.Windows.Documents.PageContent pc in d.Pages)
                {
                    System.Windows.Documents.FixedPage fixedPage = pc.GetPageRoot(false);
                    double width           = fixedPage.Width;
                    double height          = fixedPage.Height;
                    System.Windows.Size sz = new System.Windows.Size(width, height);
                    fixedPage.Measure(sz);
                    fixedPage.Arrange(new System.Windows.Rect(new System.Windows.Point(), sz));
                    fixedPage.UpdateLayout();
                    ContainerVisual newpage = new ContainerVisual();
                    newpage.Children.Add(fixedPage);
                    newpage.Children.Add(CreateWatermark(400, 400, "hello world"));
                    pageno++;
                    vxpsd.Write(newpage);
                }
            }
            vxpsd.EndBatchWrite();
            container.Close();
            xpsOld.Close();
        }
Exemple #13
0
        private void btnPrintVisuals_Click(object sender, RoutedEventArgs e)
        {
            //Get hold of the visual you want to print.
            List <Visual> visuals = GetVisuals();

            // Create a Print dialog.
            PrintDialog printDialog = new PrintDialog();

            if (printDialog.ShowDialog() != true)
            {
                return;
            }

            // Get the default print queue
            PrintQueue printQueue = printDialog.PrintQueue;

            // Get an XpsDocumentWriter for the default print queue
            XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(printQueue);

            VisualsToXpsDocument vtxd =
                (VisualsToXpsDocument)xpsdw.CreateVisualsCollator();

            //Indicate we want any writes to be performed in a batch operation.
            vtxd.BeginBatchWrite();

            //Write out each visual.
            visuals.ForEach(delegate(Visual visual)
            {
                //Scale the visual
                Visual scaledVisual = ScaleVisual(visual, printQueue);

                vtxd.Write(scaledVisual);
            });

            //Mark the end of the batch operation.
            vtxd.EndBatchWrite();
        }
Exemple #14
0
        public static void Print_WPF_Preview(FrameworkElement wpf_Element)
        {
            string sPrintFilename = "print_preview.xps";

            if (File.Exists(sPrintFilename) == true)
            {
                File.Delete(sPrintFilename);
            }


            // - <create xps document> -

            XpsDocument       doc    = new XpsDocument(sPrintFilename, FileAccess.ReadWrite);
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

            SerializerWriterCollator preview_Document = writer.CreateVisualsCollator();

            preview_Document.BeginBatchWrite();
            preview_Document.Write(wpf_Element);  // * this or wpf xaml control
            preview_Document.EndBatchWrite();

            // - </ create xps document> -
            FixedDocumentSequence preview = doc.GetFixedDocumentSequence();

            // - </Open> -

            // - </Open preview window> -

            var window = new Window();

            window.Content = new DocumentViewer {
                Document = preview
            };
            window.ShowDialog();
            doc.Close();
        }
Exemple #15
0
        private static string PrintToPDF(FrameworkElement kundeInfo, ScrollViewer scrollViewer, string lagreTilUrl, bool liggende)
        {
            if (kundeInfo == null && scrollViewer == null)
            {
                return("");
            }

            string prisforslagXPSuri = Path.Combine(Hjelpeklasser.GlobaleUrier.standardMappe2(), "Prisforslag.xps");
            string prisforslagPDFuri = Path.Combine(Hjelpeklasser.GlobaleUrier.standardMappe2(), "Prisforslag.pdf");

            if (!string.IsNullOrEmpty(lagreTilUrl))
            {
                prisforslagPDFuri = lagreTilUrl;
            }

            if (File.Exists(prisforslagXPSuri))
            {
                File.Delete(prisforslagXPSuri);
            }
            XpsDocument       _xpsDocument = new XpsDocument(prisforslagXPSuri, FileAccess.ReadWrite);
            XpsDocumentWriter xpsdw        = XpsDocument.CreateXpsDocumentWriter(_xpsDocument);

            Size docSize;

            if (liggende)
            {
                docSize = new Size(1122.24, 793.59874015748028);
            }
            else
            {
                docSize = new Size(793.59874015748028, 1122.24);
            }

            double leftMargin   = 84.5; //Margins.Left - area.OriginWidth;
            double topMargin    = 84.5; ///Margins.Top - area.OriginHeight;
            double rightMargin  = 84.5; //Margins.Right - (docSize.Width - area.ExtentWidth - area.OriginWidth);
            double bottomMargin = 84.5; //Margins.Bottom - (docSize.Height - area.ExtentHeight - area.OriginHeight);
            Size   outputSize   = new Size(
                docSize.Width - leftMargin - rightMargin,
                docSize.Height - topMargin - bottomMargin);


            SerializerWriterCollator batchPrinter = xpsdw.CreateVisualsCollator();

            batchPrinter.BeginBatchWrite();

            if (kundeInfo != null)
            {
                printKundeinfoToPdf(batchPrinter, kundeInfo, outputSize, leftMargin, topMargin);
            }
            if (scrollViewer != null && scrollViewer.Content != null)
            {
                printScrollViewer(batchPrinter, scrollViewer, outputSize, leftMargin, topMargin);
            }

            batchPrinter.EndBatchWrite();

            _xpsDocument.Close();
            PdfSharp.Xps.XpsConverter.Convert(prisforslagXPSuri, prisforslagPDFuri, 0);



            return(prisforslagPDFuri);
        }