/// <summary>
        /// Add images from source to new XPS.
        /// </summary>
        /// <param name="fixedPageReader"></param>
        /// <param name="pageWriter"></param>
        private void AddImages(
            IXpsFixedPageReader fixedPageReader,
            IXpsFixedPageWriter pageWriter)
        {
            // Adding images to resources.
            foreach (XpsImage image in fixedPageReader.Images)
            {
                XpsImage newImage = null;

                // Getting the URI to check the image extension.
                string sourceExt = image.Uri.ToString().ToLower();


                if (sourceExt.Contains(ImageType.PNG))
                {
                    newImage = pageWriter.AddImage(XpsImageType.PngImageType);
                }
                else if (sourceExt.Contains(ImageType.JPG) ||
                         sourceExt.Contains(ImageType.JPEG))
                {
                    newImage = pageWriter.AddImage(XpsImageType.JpegImageType);
                }
                else if (sourceExt.Contains(ImageType.TIF) ||
                         sourceExt.Contains(ImageType.TIFF))
                {
                    newImage = pageWriter.AddImage(XpsImageType.TiffImageType);
                }
                else if (sourceExt.Contains(ImageType.WDP))
                {
                    newImage = pageWriter.AddImage(XpsImageType.WdpImageType);
                }
                else
                {
                    newImage = null;
                }

                if (null != newImage)
                {
                    using (Stream dstImageStream = newImage.GetStream())
                    {
                        using (Stream srcImageStream = image.GetStream())
                        {
                            CopyStream(srcImageStream, dstImageStream);
                            newImage.Commit();
                            XpsDetails xpsImage = new XpsDetails();
                            xpsImage.resource  = newImage;
                            xpsImage.sourceURI = image.Uri;
                            xpsImage.destURI   = newImage.Uri;
                            xpsImages.Add(xpsImage);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        }// end:AddImage()

        // --------------------------- GetXpsImage() --------------------------
        /// <summary>
        ///   Copies a given image to a specified XPS page.</summary>
        /// <param name="pageWriter">
        ///   The FixedPage writer to copy the image to.</param>
        /// <param name="imgPath">
        ///   The source file path for the image.</param>
        /// <param name="imageType">
        ///   The type of the image.</param>
        /// <returns>
        ///   The package URI of the copied image.</returns>
        private string GetXpsImage(
            IXpsFixedPageWriter pageWriter,
            string imgPath,
            XpsImageType imageType)
        {
            XpsImage xpsImage  = pageWriter.AddImage(imageType);
            Stream   srcStream = new FileStream(imgPath, FileMode.Open);
            Stream   dstStream = xpsImage.GetStream();

            Byte[] streamBytes = new Byte[srcStream.Length];
            srcStream.Read(streamBytes, 0, (int)srcStream.Length);
            dstStream.Write(streamBytes, 0, (int)srcStream.Length);
            xpsImage.Commit();
            return(xpsImage.Uri.ToString());
        }
Esempio n. 3
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");
            }
        }
Esempio n. 4
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.");
        }