Пример #1
0
 public static void SafeSetDocument(this DocumentReference reference, FixedDocument doc)
 {
     if (reference.CheckAccess())
     {
         reference.SetDocument(doc);
     }
     else
     {
         reference.Dispatcher.Invoke(new Action(delegate { reference.SetDocument(doc); }));
     }
 }
Пример #2
0
        public static DocumentReference AddPage(XpsDocument xpsDocument)
        {
            DocumentReference newDocRef = new DocumentReference();
            FixedDocument     newFd     = new FixedDocument();

            FixedDocumentSequence docSeq = xpsDocument.GetFixedDocumentSequence();

            foreach (DocumentReference docRef in docSeq.References)
            {
                FixedDocument fd = docRef.GetDocument(false);

                foreach (PageContent oldPC in fd.Pages)
                {
                    Uri uSource = oldPC.Source;                   //读取源地址
                    Uri uBase   = (oldPC as IUriContext).BaseUri; //读取目标页面地址

                    PageContent newPageContent = new PageContent();
                    newPageContent.GetPageRoot(false);
                    newPageContent.Source = uSource;
                    (newPageContent as IUriContext).BaseUri = uBase;
                    newFd.Pages.Add(newPageContent);//将新文档追加到新的documentRefences中
                }
            }
            newDocRef.SetDocument(newFd);
            xpsDocument.Close();
            return(newDocRef);
        }
Пример #3
0
        public DocumentReference AddPage(string fileName)
        {
            DocumentReference newDocRef = new DocumentReference();
            FixedDocument     newFd     = new FixedDocument();

            XpsDocument           xpsDocument = new XpsDocument(fileName, FileAccess.Read);
            FixedDocumentSequence docSeq      = xpsDocument.GetFixedDocumentSequence();

            foreach (DocumentReference docRef in docSeq.References)
            {
                FixedDocument fd = docRef.GetDocument(false);

                foreach (PageContent oldPC in fd.Pages)
                {
                    Uri         uSource        = oldPC.Source;                   //读取源地址
                    Uri         uBase          = (oldPC as IUriContext).BaseUri; //读取目标页面地址
                    PageContent newPageContent = new PageContent();
                    newPageContent.GetPageRoot(false);                           //这个地方应当是把文档解压成一个包放到内存中我们再去读取
                    newPageContent.Source = uSource;
                    (newPageContent as IUriContext).BaseUri = uBase;
                    newFd.Pages.Add(newPageContent);//将新文档追加到新的documentRefences中
                }
            }
            newDocRef.SetDocument(newFd);
            xpsDocument.Close();
            return(newDocRef);
        }
Пример #4
0
        public DocumentReference AddPage(string fileName)
        {
            DocumentReference     documentReference     = new DocumentReference();
            FixedDocument         fixedDocument         = new FixedDocument();
            XpsDocument           xpsDocument           = new XpsDocument(fileName, FileAccess.Read);
            FixedDocumentSequence fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();

            foreach (DocumentReference current in fixedDocumentSequence.References)
            {
                FixedDocument document = current.GetDocument(false);
                foreach (PageContent current2 in document.Pages)
                {
                    Uri         source      = current2.Source;
                    Uri         baseUri     = ((IUriContext)current2).BaseUri;
                    PageContent pageContent = new PageContent();
                    pageContent.GetPageRoot(false);
                    pageContent.Source = source;
                    ((IUriContext)pageContent).BaseUri = baseUri;
                    fixedDocument.Pages.Add(pageContent);
                }
            }
            documentReference.SetDocument(fixedDocument);
            xpsDocument.Close();
            return(documentReference);
        }
        private FixedDocumentSequence CreateFixedDocumentSequence(IEnumerable <IEnumerable <string> > projects,
                                                                  Size areaExtent,
                                                                  Size areaOrigin,
                                                                  bool fixedStrokeThickness,
                                                                  DiagramTable table)
        {
            var fixedDocumentSeq = new FixedDocumentSequence()
            {
                Name = "diagrams"
            };

            foreach (var diagrams in projects)
            {
                var fixedDocument = CreateFixedDocument(diagrams,
                                                        areaExtent,
                                                        areaOrigin,
                                                        fixedStrokeThickness,
                                                        table);

                var documentRef = new DocumentReference();
                documentRef.BeginInit();
                documentRef.SetDocument(fixedDocument);
                documentRef.EndInit();

                (fixedDocumentSeq as IAddChild).AddChild(documentRef);
            }

            return(fixedDocumentSeq);
        }
Пример #6
0
        /// <summary>
        /// Attaches a FixedDocument to a FixedDocumentSequence.
        /// </summary>
        /// <param name="document">The document to attach.</param>
        /// <param name="fds">The document sequence to attach to.</param>
        private static void AttachDocumentToSequence(FixedDocument document, FixedDocumentSequence fds)
        {
            DocumentReference dr = new DocumentReference();

            dr.BeginInit();
            dr.SetDocument(document);
            dr.EndInit();

            ((IAddChild)fds).AddChild(dr);
        }
        private void CreateASimpleDocument()
        {
            var fSeq = new FixedDocumentSequence();
            var fDoc = new FixedDocument();

            // Add DocumentReference with FixedDoc to FixedDocSeq
            var docRef = new DocumentReference();

            docRef.SetDocument(fDoc);
            fSeq.References.Add(docRef);

            // Create Fixed Page
            var fPage = new FixedPage
            {
                Width  = 816,
                Height = 1056
            };

            var txt = new TextBlock
            {
                Text = "Hello XPS"
            };

            fPage.Children.Add(txt);

            // Add FixedPage to PageContent
            var pageContent = new PageContent();

            ((IAddChild)pageContent).AddChild(fPage);

            // Add PageContent to FixedDoc
            fDoc.Pages.Add(pageContent);

            // Save as XPS
            if (File.Exists(_fileName))
            {
                File.Delete(_fileName);
            }

            XpsDocument doc = new XpsDocument(_fileName, FileAccess.Write);

            try
            {
                XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
                writer.Write(fSeq);
            }
            finally
            {
                doc.Close();
            }
        }
Пример #8
0
        public FixedDocumentSequence GetResult()
        {
            // wrap the FixedDocument object in a DocumentReference object
            DocumentReference docRef = new DocumentReference();

            docRef.BeginInit();
            docRef.SetDocument(_document);
            docRef.EndInit();
            // add the DocumentReference object above to a FixedDocumentSequence object
            FixedDocumentSequence docSeq = new FixedDocumentSequence();

            (docSeq as IAddChild).AddChild(docRef);
            // return the FixedDocumentSequence object
            return(docSeq);
        }
Пример #9
0
        }// end:CreateFixedDocumentWithPages()

        // --------------- CreatePopulatedFixedDocumentSequence ---------------
        /// <summary>
        ///   Creates a FixedDocumentSequence with content.</summary>
        /// <param name="pq">
        ///   The print queue to print to.</param>
        /// <returns>
        ///   A FixedDocumentSequence with content.</returns>
        public FixedDocumentSequence CreatePopulatedFixedDocumentSequence(
            PrintQueue pq)
        {
            // Create FixedDocumentSequence
            FixedDocumentSequence fixedDocumentSequence =
                new FixedDocumentSequence();

            // Add Documents to a Fixed Document Sequence
            DocumentReference documentRef = new DocumentReference();

            documentRef.SetDocument(CreateFixedDocumentWithPages(pq));
            fixedDocumentSequence.References.Add(documentRef);

            return(fixedDocumentSequence);
        }// end:CreatePopulatedFixedDocumentSequence()
Пример #10
0
 private void FillDocumentReference(DocumentReference documentReference, RollUpFixedDocument document)
 {
     if (document.BaseUri != null)
     {
         documentReference.Source = document.Source;
         (documentReference as IUriContext).BaseUri = document.BaseUri;
     }
     else if (document.FixedDocument != null)
     {
         documentReference.SetDocument(document.FixedDocument);
     }
     else
     {
         AddPages(documentReference, document);
     }
 }
Пример #11
0
        public static void ConvertToXps(FixedDocument fixedDocument, Stream outputStream)
        {
            Package               package           = Package.Open(outputStream, FileMode.Create);
            XpsDocument           xpsDocument       = new XpsDocument(package, CompressionOption.Normal);
            XpsDocumentWriter     xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
            FixedDocumentSequence fixedDocSeq       = new FixedDocumentSequence();
            DocumentReference     docRef            = new DocumentReference();

            docRef.BeginInit();
            docRef.SetDocument(fixedDocument);
            docRef.EndInit();
            ((IAddChild)fixedDocSeq).AddChild(docRef);
            xpsDocumentWriter.Write(fixedDocSeq.DocumentPaginator);
            xpsDocument.Close();
            package.Close();
        }
    /// <summary>
    /// encapsulater for a UIElement in an DocumentReference
    /// DocumentReference(FixedDocument(PageContent(FixedPage(UIElement))))
    /// to simplify the print of multiple pages
    /// </summary>
    /// <param name="uiElement">the UIElement which</param>
    /// <returns>creates a DocumentReference</returns>
    private DocumentReference toDocumentReference(UIElement uiElement)
    {
        if (uiElement == null)
        {
            throw new NullReferenceException("the UIElement has to be not null");
        }

        FixedPage         fixedPage   = new FixedPage();
        PageContent       pageContent = new PageContent();
        FixedDocument     fixedDoc    = new FixedDocument();
        DocumentReference docRef      = new DocumentReference();

        #region Step1

        // add the UIElement object the FixedPage
        fixedPage.Children.Add(uiElement);

        #endregion

        #region Step2

        // add the FixedPage to the PageContent collection
        pageContent.BeginInit();
        ((IAddChild)pageContent).AddChild(fixedPage);
        pageContent.EndInit();

        #endregion

        #region Step 3

        //// add the PageContent to the FixedDocument collection
        ((IAddChild)fixedDoc).AddChild(pageContent);

        #endregion

        #region Step 4

        //// add the FixedDocument to the document reference collection
        docRef.BeginInit();
        docRef.SetDocument(fixedDoc);
        docRef.EndInit();

        #endregion

        return(docRef);
    }
Пример #13
0
        public void Print(string printerName)
        {
            string receiptPath = Path.GetDirectoryName(_receiptFile) + "\\" + Path.GetFileNameWithoutExtension(_receiptFile) + ".xps";

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

            using (FileStream outputStream = File.Create(receiptPath))
            {
                var package   = Package.Open(outputStream, FileMode.Create);
                var xpsDoc    = new XpsDocument(package, CompressionOption.Normal);
                var xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);

                var fixedDocSeq = new FixedDocumentSequence();
                var docRef      = new DocumentReference();
                docRef.BeginInit();
                docRef.SetDocument(_template);
                docRef.EndInit();
                ((IAddChild)fixedDocSeq).AddChild(docRef);

                xpsWriter.Write(fixedDocSeq.DocumentPaginator);

                xpsDoc.Close();
                package.Close();
            }

            using (var printServer = new LocalPrintServer())
            {
                EnumeratedPrintQueueTypes[] enumerationFlags = { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections };

                foreach (PrintQueue queue in printServer.GetPrintQueues(enumerationFlags))
                {
                    if (queue.Name == printerName)
                    {
                        queue.AddJob("Photo Kiosk", receiptPath, false);
                        break;
                    }
                }
            }
        }
        public static void ConvertToXps(FixedDocument fixedDoc, Stream outputStream)
        {
            var package = Package.Open(outputStream, FileMode.Create);
            var xpsDoc = new XpsDocument(package, CompressionOption.Normal);
            XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);

            // xps documents are built using fixed document sequences
            var fixedDocSeq = new FixedDocumentSequence();
            var docRef = new DocumentReference();
            docRef.BeginInit();
            docRef.SetDocument(fixedDoc);
            docRef.EndInit();
            ((IAddChild)fixedDocSeq).AddChild(docRef);

            // write out our fixed document to xps
            xpsWriter.Write(fixedDocSeq.DocumentPaginator);

            xpsDoc.Close();
            package.Close();
        }
Пример #15
0
        private void AddPages(DocumentReference documentReference, RollUpFixedDocument document)
        {
            FixedDocument fixedDocument = new FixedDocument();

            documentReference.SetDocument(fixedDocument);
            foreach (RollUpFixedPage page in document.Pages)
            {
                PageContent pageContent = new PageContent();
                if (page.BaseUri == null)
                {
                    (pageContent as IAddChild).AddChild(page.FixedPage);
                }
                else
                {
                    pageContent.Source = page.Source;
                    (pageContent as IUriContext).BaseUri = page.BaseUri;
                }
                pageContent.GetPageRoot(true);
                fixedDocument.Pages.Add(pageContent);
            }
        }
Пример #16
0
        public DocumentReference AddPage(string fileName)
        {
            DocumentReference newDocRef = new DocumentReference();
            FixedDocument     newFd     = new FixedDocument();

            XpsDocument           xpsDocument = new XpsDocument(fileName, FileAccess.Read);
            FixedDocumentSequence docSeq      = xpsDocument.GetFixedDocumentSequence();
            int page = 0;

            foreach (DocumentReference docRef in docSeq.References)
            {
                FixedDocument fd = docRef.GetDocument(false);
                page = fd.Pages.Count;
                foreach (PageContent oldPC in fd.Pages)
                {
                    Uri         uSource        = oldPC.Source;                   //读取源地址
                    Uri         uBase          = (oldPC as IUriContext).BaseUri; //读取目标页面地址
                    PageContent newPageContent = new PageContent();
                    newPageContent.GetPageRoot(false);                           //这个地方应当是把文档解压成一个包放到内存中我们再去读取
                    newPageContent.Source = uSource;
                    (newPageContent as IUriContext).BaseUri = uBase;
                    newFd.Pages.Add(newPageContent);//将新文档追加到新的documentRefences中
                }
            }

            if (page % 2 != 0 && ConfigHelper.GetSetNode("Dprint") == "Y") //双面打印机增加空白页
            {
                FrameworkElement FE = new FrameworkElement();
                FixedPage        FP = new FixedPage();
                FP.Width  = 793.76;
                FP.Height = 1122.56;
                FP.Children.Add(FE);
                PageContent pageContent = new PageContent();
                pageContent.Child = FP;
                newFd.Pages.Add(pageContent);
            }
            newDocRef.SetDocument(newFd);
            xpsDocument.Close();
            return(newDocRef);
        }
Пример #17
0
 private static void Add(string path, FixedDocumentSequence fixedDocumentSequence)
 {
     using (var doc = new XpsDocument(path, FileAccess.Read))
     {
         var sourceSequence = doc.GetFixedDocumentSequence();
         if (sourceSequence != null)
         {
             foreach (var dr in sourceSequence.References)
             {
                 var newDocumentReference = new DocumentReference
                 {
                     Source = dr.Source
                 };
                 var baseUri = ((IUriContext)dr).BaseUri;
                 ((IUriContext)newDocumentReference).BaseUri = baseUri;
                 var fd = newDocumentReference.GetDocument(true);
                 newDocumentReference.SetDocument(fd);
                 fixedDocumentSequence.References.Add(newDocumentReference);
             }
         }
     }
 }
Пример #18
0
        private FixedDocumentSequence CreateManagedXpsDocumentFromPages(IEnumerable <PageContent> pages)
        {
            var newSequence     = new FixedDocumentSequence();
            var newDocReference = new DocumentReference();
            var newDoc          = new FixedDocument();

            newDocReference.SetDocument(newDoc);

            foreach (PageContent page in pages)
            {
                var newPage = new PageContent
                {
                    Source = page.Source
                };
                (newPage as IUriContext).BaseUri = (page as IUriContext)?.BaseUri;
                newPage.GetPageRoot(true);
                newDoc.Pages.Add(newPage);
            }

            newSequence.References.Add(newDocReference);
            return(newSequence);
        }
Пример #19
0
        public DocumentReference AddPage(string fileName)
        {
            DocumentReference     documentReference     = new DocumentReference();
            FixedDocument         fixedDocument         = new FixedDocument();
            XpsDocument           xpsDocument           = new XpsDocument(fileName, FileAccess.Read);
            FixedDocumentSequence fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
            int page = 0;

            foreach (DocumentReference current in fixedDocumentSequence.References)
            {
                FixedDocument document = current.GetDocument(false);
                page = document.Pages.Count;
                foreach (PageContent current2 in document.Pages)
                {
                    Uri         source      = current2.Source;
                    Uri         baseUri     = ((IUriContext)current2).BaseUri;
                    PageContent pageContent = new PageContent();
                    pageContent.GetPageRoot(false);
                    pageContent.Source = source;
                    ((IUriContext)pageContent).BaseUri = baseUri;
                    fixedDocument.Pages.Add(pageContent);
                }
            }
            if (page % 2 != 0 && ConfigHelper.GetSetNode("Dprint") == "Y") //双面打印机增加空白页
            {
                FrameworkElement FE = new FrameworkElement();
                FixedPage        FP = new FixedPage();
                FP.Width  = 793.76;
                FP.Height = 1122.56;
                FP.Children.Add(FE);
                PageContent pageContent = new PageContent();
                pageContent.Child = FP;
                fixedDocument.Pages.Add(pageContent);
            }
            documentReference.SetDocument(fixedDocument);
            xpsDocument.Close();
            return(documentReference);
        }
Пример #20
0
        private void PrintImage(string fileName, Int32Rect crop, int quantity)
        {
            Console.WriteLine("    Print image " + fileName + " in " + quantity.ToString() + " copies.");

            PrintTicket ticket = _printer.UserPrintTicket;
            Size        paper  = new Size(Math.Max(ticket.PageMediaSize.Width.Value, ticket.PageMediaSize.Height.Value), Math.Min(ticket.PageMediaSize.Width.Value, ticket.PageMediaSize.Height.Value));
            Size        format = new Size(Math.Max(_width, _height) * 96 / 25.4, Math.Min(_width, _height) * 96 / 25.4);

            double printableWidth;
            double printableHeight;

            if (format.Width <= paper.Width && format.Height <= paper.Height)
            {
                printableWidth  = format.Width;
                printableHeight = format.Height;
            }
            else if (format.Width <= paper.Height && format.Height <= paper.Width)
            {
                printableWidth  = format.Height;
                printableHeight = format.Width;
            }
            else
            {
                printableWidth  = paper.Width;
                printableHeight = paper.Height;
            }

            // Swap printable width and height if page orientation is portarait
            if (ticket.PageOrientation == PageOrientation.Portrait || ticket.PageOrientation == PageOrientation.ReversePortrait)
            {
                double temp = printableWidth;
                printableWidth  = printableHeight;
                printableHeight = temp;
            }

            fileName = _orderFolder + fileName;

            // Create FixedDocument containing the image
            var fixedDocument = new FixedDocument();
            var pageContent   = new PageContent();
            var fixedPage     = new FixedPage();
            var image         = CreateImage(fileName, printableWidth, printableHeight, crop);

            fixedPage.Width  = printableWidth;
            fixedPage.Height = printableHeight;
            fixedPage.Children.Add(image);
            ((IAddChild)pageContent).AddChild(fixedPage);
            fixedDocument.Pages.Add(pageContent);

            // Save FixedDocument to XPS file
            string xpsFilePath = fileName + _width.ToString() + "x" + _height.ToString() + ".xps";

            using (FileStream outputStream = File.Create(xpsFilePath))
            {
                var package     = Package.Open(outputStream, FileMode.Create);
                var xpsDoc      = new XpsDocument(package, CompressionOption.Normal);
                var xpsWriter   = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                var fixedDocSeq = new FixedDocumentSequence();
                var docRef      = new DocumentReference();
                docRef.BeginInit();
                docRef.SetDocument(fixedDocument);
                docRef.EndInit();
                ((IAddChild)fixedDocSeq).AddChild(docRef);

                xpsWriter.Write(fixedDocSeq.DocumentPaginator);

                xpsDoc.Close();
                package.Close();
            }

            // Add the XPS file to print queue
            for (int i = 0; i < quantity; i++)
            {
                _printer.AddJob("Photo Kiosk", xpsFilePath, false);
                _success = true;
            }
        }