コード例 #1
0
        //<SnippetCreateAndWriteToXpsDocument>
        // ---------------------------- Create() ------------------------------
        /// <summary>
        ///   Creates an XpsDocument using the Xps.Packaging APIs.</summary>
        /// <param name="xpsDocument">
        ///   The XpsDocument to create.</param>
        /// <remarks>
        ///   The Xps.Packaging APIs are used to create the DocumentSequence,
        ///   FixedDocument, and FixedPage "PackageParts" of an XpsDocument.
        ///   The applicationt is responsible for using the XmlWriter to
        ///   serialize the page markup and for supplying the streams for any
        ///   font or image resources.</remarks>
        public void Create(XpsDocument xpsDocument)
        {
            // Create the document sequence
            IXpsFixedDocumentSequenceWriter docSeqWriter =
                xpsDocument.AddFixedDocumentSequence();

            // Create the document
            IXpsFixedDocumentWriter docWriter = docSeqWriter.AddFixedDocument();

            // Create the Page
            IXpsFixedPageWriter pageWriter = docWriter.AddFixedPage();

            // Get the XmlWriter
            XmlWriter xmlWriter = pageWriter.XmlWriter;

            // Write the mark up according the XPS Specifications
            BeginFixedPage(xmlWriter);
            AddGlyphRun(pageWriter, xmlWriter,
                        "This is a photo of the famous Notre Dame in Paris",
                        16, 50, 50, @"C:\Windows\fonts\arial.ttf");

            AddImage(pageWriter, xmlWriter,
                     "ParisNotreDame.jpg", XpsImageType.JpegImageType,
                     100, 100, 600, 1100);

            // End the page.
            EndFixedPage(xmlWriter);

            // Close the page, document, and document sequence.
            pageWriter.Commit();
            docWriter.Commit();
            docSeqWriter.Commit();
            _fontDictionary.Clear();
        }// end:Create()
コード例 #2
0
ファイル: XpsCreate.cs プロジェクト: slozier/dotnet-api-docs
    }// end:Run()

    //<SnippetXpsCreateAddPkgContent>
    // ------------------------- AddPackageContent ----------------------------
    /// <summary>
    ///   Adds a predefined set of content to a given XPS document.</summary>
    /// <param name="xpsDocument">
    ///   The package to add the document content to.</param>
    /// <param name="attachPrintTicket">
    ///   true to include a PrintTicket with the
    ///   document; otherwise, false.</param>
    private void AddPackageContent(
        XpsDocument xpsDocument, bool attachPrintTicket)
    {
        try
        {
            PrintTicket printTicket = GetPrintTicketFromPrinter();
            // PrintTicket is null, there is no need to attach one.
            if (printTicket == null)
            {
                attachPrintTicket = false;
            }

            // Add a FixedDocumentSequence at the Package root
            IXpsFixedDocumentSequenceWriter documentSequenceWriter =
                xpsDocument.AddFixedDocumentSequence();

            // Add the 1st FixedDocument to the FixedDocumentSequence. - - - - -
            IXpsFixedDocumentWriter fixedDocumentWriter =
                documentSequenceWriter.AddFixedDocument();

            // Add content to the 1st document
            AddDocumentContent(fixedDocumentWriter);

            // Commit the 1st Document
            fixedDocumentWriter.Commit();

            // Add a 2nd FixedDocument to the FixedDocumentSequence. - - - - - -
            fixedDocumentWriter = documentSequenceWriter.AddFixedDocument();

            // Add content to the 2nd document.
            AddDocumentContent(fixedDocumentWriter);

            // If attaching PrintTickets, attach one at the FixedDocument level.
            if (attachPrintTicket)
            {
                fixedDocumentWriter.PrintTicket = printTicket;
            }

            // Commit the 2nd document.
            fixedDocumentWriter.Commit();

            // If attaching PrintTickets, attach one at
            // the package FixedDocumentSequence level.
            if (attachPrintTicket)
            {
                documentSequenceWriter.PrintTicket = printTicket;
            }

            // Commit the FixedDocumentSequence
            documentSequenceWriter.Commit();
        }
        catch (XpsPackagingException xpsException)
        {
            throw xpsException;
        }
    }// end:AddPackageContent()
コード例 #3
0
        /// <summary>
        ///   Adds a predefined set of content to a given XPS document.
        /// </summary>
        /// <param name="xpsDocument">
        ///   The package to add the document content to.
        /// </param>
        private static void AddPackageContent(XpsDocument xpsDocument,
                                              XmlNodeList selectedThumbnails)
        {
            // Add a FixedDocumentSequence at the Package root.
            IXpsFixedDocumentSequenceWriter documentSequenceWriter =
                xpsDocument.AddFixedDocumentSequence();

            // Add a FixedDocument to the FixedDocumentSequence.
            IXpsFixedDocumentWriter fixedDocumentWriter =
                documentSequenceWriter.AddFixedDocument();

            // Add content to the document.
            AddDocumentContent(fixedDocumentWriter, selectedThumbnails);

            // Commit the Document.
            fixedDocumentWriter.Commit();

            // Commit the FixedDocumentSequence.
            documentSequenceWriter.Commit();
        }
コード例 #4
0
ファイル: XPSTemplate.cs プロジェクト: ericramses/YPILIS
        public bool CreateNewXPSFromSource(string sourceXpsPath, string destinationXpsPath, ReplaceCallBackType cb)
        {
            this.m_CallBack = cb;
            bool blnResult = false;

            try
            {
                // Delete the old file generated by our code.
                if (File.Exists(destinationXpsPath))
                {
                    File.Delete(destinationXpsPath);
                }

                // Creating the output XPS file.
                XpsDocument document = new XpsDocument(destinationXpsPath,
                                                       FileAccess.ReadWrite, System.IO.Packaging.CompressionOption.NotCompressed);

                IXpsFixedDocumentSequenceWriter docSeqWriter = document.AddFixedDocumentSequence();

                // Loading the source xps files to list to read them for edit.
                List <string> sourceFiles = new List <string>();
                sourceFiles.Add(sourceXpsPath);

                // Looping each source files.
                foreach (string sourceFile in sourceFiles)
                {
                    XpsDocument docToRead = new XpsDocument(sourceFile, FileAccess.ReadWrite);
                    IXpsFixedDocumentSequenceReader docSequenceToRead =
                        docToRead.FixedDocumentSequenceReader;

                    foreach (IXpsFixedDocumentReader fixedDocumentReader in
                             docSequenceToRead.FixedDocuments)
                    {
                        IXpsFixedDocumentWriter fixedDocumentWriter =
                            docSeqWriter.AddFixedDocument();

                        AddStructure(fixedDocumentReader, fixedDocumentWriter);

                        foreach (IXpsFixedPageReader fixedPageReader in
                                 fixedDocumentReader.FixedPages)
                        {
                            IXpsFixedPageWriter pageWriter =
                                fixedDocumentWriter.AddFixedPage();

                            AddImages(fixedPageReader, pageWriter);

                            AddFonts(fixedPageReader, pageWriter);

                            AddContent(fixedPageReader, pageWriter);

                            // Commmit all changes.
                            pageWriter.Commit();
                        }
                        fixedDocumentWriter.Commit();
                    }
                    // Close the current source before openeing next one.
                    docToRead.Close();
                }

                docSeqWriter.Commit();

                // Close newly created XPS document.
                document.Close();
                blnResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return(blnResult);
        }
コード例 #5
0
        public bool WriteDocument(List <byte[]> images, string file_out)
        {
            XpsDocument doc = null;
            IXpsFixedDocumentSequenceWriter docSeqWriter = null;
            IXpsFixedDocumentWriter         docWriter    = null;

            if (LogHelper.CanDebug())
            {
                LogHelper.Begin("XpsHelper.WriteDocument");
            }
            try
            {
                // xps doc does not manage overwrite, so delete before if exist
                if (File.Exists(file_out))
                {
                    File.Delete(file_out);
                }

                // create the document
                doc = new XpsDocument(file_out, FileAccess.ReadWrite, CompressionOption.NotCompressed);

                // Create the document sequence
                docSeqWriter = doc.AddFixedDocumentSequence();

                // Create the document
                docWriter = docSeqWriter.AddFixedDocument();

                for (int i = 0; i < images.Count; ++i)
                {
                    // Create the Page
                    IXpsFixedPageWriter pageWriter = docWriter.AddFixedPage();

                    // Get the XmlWriter
                    XmlWriter xmlWriter = pageWriter.XmlWriter;

                    //create the xps image resource
                    XpsImage xpsImage = pageWriter.AddImage(XpsImageType.JpegImageType);
                    using (Stream dstImageStream = xpsImage.GetStream())
                    {
                        dstImageStream.Write(images[i], 0, images[i].Length);
                        xpsImage.Commit();
                    }

                    //this is just to get the real WPF image size as WPF display units and not image pixel size !!
                    using (MemoryStream ms = new MemoryStream(images[i]))
                    {
                        BitmapImage myImage = new BitmapImage();
                        myImage.BeginInit();
                        myImage.CacheOption  = BitmapCacheOption.OnLoad;
                        myImage.StreamSource = ms;
                        myImage.EndInit();

                        //write the page
                        WritePageContent(xmlWriter, xpsImage, myImage.Width, myImage.Height);
                    }

                    //with the first page, create the thumbnail of the xps document
                    if (i == 0)
                    {
                        XpsThumbnail thumb = doc.AddThumbnail(XpsImageType.JpegImageType);

                        using (MemoryStream ms = new MemoryStream(images[i]))
                        {
                            BitmapImage thumbImage = new BitmapImage();
                            thumbImage.BeginInit();
                            thumbImage.DecodePixelWidth = 256;
                            thumbImage.CacheOption      = BitmapCacheOption.OnLoad;
                            thumbImage.StreamSource     = ms;
                            thumbImage.EndInit();

                            using (Stream xpsThumbStream = thumb.GetStream())
                            {
                                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                                encoder.Frames.Add(BitmapFrame.Create(thumbImage));
                                encoder.Save(xpsThumbStream);
                            }
                        }
                        thumb.Commit();
                    }

                    xmlWriter.Flush();

                    // Close the page
                    pageWriter.Commit();
                }

                return(true);
            }
            catch (Exception err)
            {
                LogHelper.Manage("XpsHelper.WriteDocument", err);
                return(false);
            }
            finally
            {
                if (docWriter != null)
                {
                    docWriter.Commit();
                }

                if (docSeqWriter != null)
                {
                    docSeqWriter.Commit();
                }

                if (doc != null)
                {
                    doc.Close();
                }

                LogHelper.End("XpsHelper.WriteDocument");
            }
        }
コード例 #6
0
        /// <summary>
        /// Creates a new XPS Diagram from scratch
        /// </summary>
        /// <param name="fileName">Name of the file. Important: If the file already exists, it will be deleted</param>
        /// <param name="format">Format of the page</param>
        public XpsDocumentHelper(string fileName, PageFormat format)
        {
            // Initialize teh Dictionaries for fonts and images
            _embeddedFonts = new Dictionary<string, Uri>();
            _embeddedImages = new Dictionary<string, ImageInfo>();

            // The file is created from scratch so if it already exists it will be removed.
            if (File.Exists(fileName))
                File.Delete(fileName);

            // Create the new package with all internal things that are required
            _package = Package.Open(fileName);
            _document = new XpsDocument(_package);
            _xpsDocSeqWriter = _document.AddFixedDocumentSequence();
            _xpsDocWriter = _xpsDocSeqWriter.AddFixedDocument();
            _xpsPageWriter = _xpsDocWriter.AddFixedPage();
            _xmlWriter = _xpsPageWriter.XmlWriter;

            // Write the document header, I fixed everything to the the en-US culture here.
            StartPage(format);
        }
コード例 #7
0
        private bool CreateNewXPSFromSource()
        {
            bool blnResult = false;

            try
            {
                // Delete the old file generated by our code.
                if (File.Exists(destinationXpsPath))
                {
                    File.Delete(destinationXpsPath);
                }

                // Creating the output XPS file.
                XpsDocument document = new XpsDocument(destinationXpsPath,
                                                       FileAccess.ReadWrite,
                                                       System.IO.Packaging.CompressionOption.Maximum);
                IXpsFixedDocumentSequenceWriter docSeqWriter
                    = document.AddFixedDocumentSequence();

                // Loading the source xps files to list to read them for edit.
                List <string> sourceFiles = new List <string>();
                sourceFiles.Add(sourceXPS);

                // Looping each source files.
                foreach (string sourceFile in sourceFiles)
                {
                    XpsDocument docToRead = new XpsDocument(sourceFile, FileAccess.ReadWrite);
                    IXpsFixedDocumentSequenceReader docSequenceToRead =
                        docToRead.FixedDocumentSequenceReader;

                    foreach (IXpsFixedDocumentReader fixedDocumentReader in
                             docSequenceToRead.FixedDocuments)
                    {
                        IXpsFixedDocumentWriter fixedDocumentWriter =
                            docSeqWriter.AddFixedDocument();

                        AddStructure(fixedDocumentReader, fixedDocumentWriter);

                        foreach (IXpsFixedPageReader fixedPageReader in
                                 fixedDocumentReader.FixedPages)
                        {
                            IXpsFixedPageWriter pageWriter =
                                fixedDocumentWriter.AddFixedPage();

                            AddImages(fixedPageReader, pageWriter);

                            AddFonts(fixedPageReader, pageWriter);

                            AddContent(fixedPageReader, pageWriter);

                            // Commmit all changes.
                            pageWriter.Commit();
                        }
                        fixedDocumentWriter.Commit();
                    }
                    // Close the current source before openeing next one.
                    docToRead.Close();
                }

                docSeqWriter.Commit();

                // Show the modified content in a DocumentViewer.
                dvModifiedXPS.Document  = document.GetFixedDocumentSequence();
                ModifiedGrid.Visibility = System.Windows.Visibility.Visible;

                // Close newly created XPS document.
                document.Close();
                blnResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return(blnResult);
        }