示例#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
    }// end:AddPackageContent()

    //</SnippetXpsCreateAddPkgContent>

    //<SnippetXpsCreateAddDocContent>
    // ------------------------- AddDocumentContent ---------------------------
    /// <summary>
    ///   Adds a predefined set of content to a given document writer.</summary>
    /// <param name="fixedDocumentWriter">
    ///   The document writer to add the content to.</param>
    private void AddDocumentContent(IXpsFixedDocumentWriter fixedDocumentWriter)
    {
        // Collection of image and font resources used on the current page.
        //   Key: "XpsImage", "XpsFont"
        //   Value: List of XpsImage or XpsFont resources
        Dictionary <string, List <XpsResource> > resources;

        try
        {
            // Add Page 1 to current document.
            IXpsFixedPageWriter fixedPageWriter =
                fixedDocumentWriter.AddFixedPage();

            // Add the resources for Page 1 and get the resource collection.
            resources = AddPageResources(fixedPageWriter);

            // Write page content for Page 1.
            WritePageContent(fixedPageWriter.XmlWriter,
                             "Page 1 of " + fixedDocumentWriter.Uri.ToString(), resources);

            // Commit Page 1.
            fixedPageWriter.Commit();

            // Add Page 2 to current document.
            fixedPageWriter = fixedDocumentWriter.AddFixedPage();

            // Add the resources for Page 2 and get the resource collection.
            resources = AddPageResources(fixedPageWriter);

            // Write page content to Page 2.
            WritePageContent(fixedPageWriter.XmlWriter,
                             "Page 2 of " + fixedDocumentWriter.Uri.ToString(), resources);

            // Commit Page 2.
            fixedPageWriter.Commit();
        }
        catch (XpsPackagingException xpsException)
        {
            throw xpsException;
        }
    }// end:AddDocumentContent()
示例#3
0
        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);
        }
示例#4
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");
            }
        }
示例#5
0
        private static void AddDocumentContent(IXpsFixedDocumentWriter fixedDocumentWriter,
                                               XmlNodeList selectedThumbnails)
        {
            Console.WriteLine("Using PackWebRequest and PackWebResponse APIs to retrieve selected photos ...");
            Console.WriteLine("------------------------------------------------------------------------------");

            foreach (XmlNode node in selectedThumbnails)
            {
                Uri    packageUri;
                Uri    partUri;
                string selectedId = node.Attributes["Id"].InnerText;

                try
                {
                    GetSelectedPhotoUri(selectedId, out packageUri, out partUri);
                }
                catch (Exception e)
                {
                    // We catch all exceptions because we don't want any
                    // exception caused by invalid input crash our server.
                    // But we record all exceptions, and try to continue.
                    Console.WriteLine("Hit exception while resolving photo Uri "
                                      + "for selected thumbnail ID " + selectedId);
                    Console.WriteLine("Exception:");
                    Console.WriteLine(e.ToString());
                    continue;
                }

                // Using PackWebRequest to retrieve the photo.
                using (WebResponse response =
                           PackWebRequestForPhotoPart(packageUri, partUri))
                {
                    Stream s = response.GetResponseStream();

                    // Add a fixed page into the fixed document.
                    IXpsFixedPageWriter fixedPageWriter =
                        fixedDocumentWriter.AddFixedPage();

                    // Add the photo image resource to the page.
                    XpsImage xpsImage =
                        fixedPageWriter.AddImage(response.ContentType);

                    SharedLibrary.CopyStream(s, xpsImage.GetStream());

                    xpsImage.Commit();

                    s.Seek(0, SeekOrigin.Begin);

                    // Use BitmapFrame to get the dimension of the photo image.
                    BitmapFrame imgFrame = BitmapFrame.Create(s);

                    // Write the XML description of the fixed page.
                    WritePageContent(fixedPageWriter.XmlWriter, xpsImage.Uri,
                                     imgFrame);

                    fixedPageWriter.Commit();
                }
            }
            Console.WriteLine("------------------------------------------------------------------------------");
            Console.WriteLine("All selected photos retrieved.");
        }
        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);
        }
示例#7
0
        }// end:AddPackageContent()

        // ------------------------- AddDocumentContent ---------------------------
        /// <summary>
        ///   Adds a predefined set of content to a given document writer.</summary>
        /// <param name="fixedDocumentWriter">
        ///   The document writer to add the content to.</param>
        private void AddDocumentContent(IXpsFixedDocumentWriter fixedDocumentWriter)
        {
            // Collection of image and font resources used on the current page.
            //   Key: "XpsImage", "XpsFont"
            //   Value: List of XpsImage or XpsFont resources
            Dictionary <string, List <XpsResource> > resources;

            try
            {
                // Add Page 1 to current document.
                IXpsFixedPageWriter fixedPageWriter =
                    fixedDocumentWriter.AddFixedPage();

                // Add the resources for Page 1 and get the resource collection.
                // resources = AddPageResources(fixedPageWriter);

                // Write page content for Page 1.

                fixedPageWriter.XmlWriter.WriteStartElement("FixedPage");
                fixedPageWriter.XmlWriter.WriteAttributeString("Width", "816");
                fixedPageWriter.XmlWriter.WriteAttributeString("Height", "1056");
                fixedPageWriter.XmlWriter.WriteAttributeString("xmlns",
                                                               "http://schemas.microsoft.com/xps/2005/06");
                fixedPageWriter.XmlWriter.WriteAttributeString("xml:lang", "en-US");


                fixedPageWriter.XmlWriter.WriteStartElement("Table");

                fixedPageWriter.XmlWriter.WriteAttributeString("Margin", "6,16,0,0");

                fixedPageWriter.XmlWriter.WriteStartElement("Table.Columns");

                fixedPageWriter.XmlWriter.WriteStartElement("TableColumn");
                fixedPageWriter.XmlWriter.WriteAttributeString("Width", "63.5");
                fixedPageWriter.XmlWriter.WriteEndElement(); //column

                fixedPageWriter.XmlWriter.WriteStartElement("TableColumn");
                fixedPageWriter.XmlWriter.WriteAttributeString("Width", "3");
                fixedPageWriter.XmlWriter.WriteEndElement(); //column

                fixedPageWriter.XmlWriter.WriteStartElement("TableColumn");
                fixedPageWriter.XmlWriter.WriteAttributeString("Width", "63.5");
                fixedPageWriter.XmlWriter.WriteEndElement(); //column

                fixedPageWriter.XmlWriter.WriteStartElement("TableColumn");
                fixedPageWriter.XmlWriter.WriteAttributeString("Width", "3");
                fixedPageWriter.XmlWriter.WriteEndElement(); //column

                fixedPageWriter.XmlWriter.WriteStartElement("TableColumn");
                fixedPageWriter.XmlWriter.WriteAttributeString("Width", "63.5");
                fixedPageWriter.XmlWriter.WriteEndElement(); //column

                fixedPageWriter.XmlWriter.WriteEndElement(); //columns

                fixedPageWriter.XmlWriter.WriteStartElement("TableRowGroup");


                for (int i = 0; i < 7; i++)
                {
                    fixedPageWriter.XmlWriter.WriteStartElement("TableRow");

                    fixedPageWriter.XmlWriter.WriteStartElement("TableCell");

                    fixedPageWriter.XmlWriter.WriteElementString("Paragraph", "Hello!!!");

                    fixedPageWriter.XmlWriter.WriteEndElement(); //TableCell

                    fixedPageWriter.XmlWriter.WriteEndElement(); //row
                }


                fixedPageWriter.XmlWriter.WriteEndElement(); //TableRowGroup

                fixedPageWriter.XmlWriter.WriteEndElement(); //table

                // Commit Page 1.
                fixedPageWriter.Commit();
            }
            catch (XpsPackagingException xpsException)
            {
                throw xpsException;
            }
        }// end:AddDocumentContent()