locked
How to add Images like JPG,tiff into XPS Document

    Question

  • Hi ,

        Can any one tell me how images like jpg , tiff can be added into the XPS Document.

       Thanks in advance for earliest reply.

     

    Santhosh

     

     

     

    Tuesday, July 04, 2006 8:04 AM

All replies

  • You can open the images in an imaging application and print using "Microsoft XPS Document Writer" Printer Driver.

    You can also add images to Word Document (Office 2007) and save it as XPS file

    Tuesday, July 04, 2006 4:03 PM
  • Hi Mridu,

         Thank you for the reply. I wanted to add images to XPS Document using WinFX APIs.

    Thanks in advance,

    Santhosh.

     

     

    Wednesday, July 05, 2006 3:11 AM
  • Santhosh,

    You can use the IXpsFixedPageWriter API to programmatically add images and other resources to your XpsDocument.

    There is a good sample that clearly demonstrates this in the January CTP Hands on labs PRSHOL11 - Programming XPS Documents, which can be downloaded here (http://download.microsoft.com/download/e/c/d/ecd81671-eed3-4df5-a01e-383cfd888ea2/hands-on%20labs%20-%20jan%20ctp.msi)

    I have edited the code slightly; let me know if you have any questions.  The code below demonstrates how to add images and fonts to an XPS Document.

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Windows.Xps.Packaging;

    using System.Windows.Xps.Serialization;

    using System.IO;

    using System.Windows.Markup;

    using System.Xml;

     

    namespace AddingResources

    {

        class Program

        {

            static void Main(string[] args)

            {

                String srcPage = "src.xaml";

                String dstContainer = "text.xps";

     

                File.Delete(dstContainer);

     

                XpsDocument document = new XpsDocument(dstContainer, FileAccess.ReadWrite);

     

                IXpsFixedDocumentSequenceWriter docSeqWriter =

                    document.AddFixedDocumentSequence();

     

                IXpsFixedDocumentWriter docWriter = docSeqWriter.AddFixedDocument();

     

                IXpsFixedPageWriter pageWriter = docWriter.AddFixedPage();

     

                String image1Path = Directory.GetCurrentDirectory() + "\\Images\\Amsterdam Canal.jpg";

                String image2Path = Directory.GetCurrentDirectory() + "\\Images\\Paris Notre Dame.jpg";

                String image3Path = Directory.GetCurrentDirectory() + "\\Images\\Tower of London.jpg";

                String font1Path = Directory.GetCurrentDirectory() + "\\Fonts\\CURLZ___.TTF";

     

                String image1Uri = AddImage(pageWriter, image1Path);

                String image2Uri = AddImage(pageWriter, image2Path);

                String image3Uri = AddImage(pageWriter, image3Path);

                String font1Uri = AddFont(pageWriter, font1Path);

     

                //Load Markup file, and replace tokens with actual Xps URIs

                StreamReader srcTextStream = new StreamReader(srcPage);

                string pageContents = srcTextStream.ReadToEnd();

                pageContents = pageContents.Replace("font1", font1Uri);

                pageContents = pageContents.Replace("image1", image1Uri);

                pageContents = pageContents.Replace("image2", image2Uri);

                pageContents = pageContents.Replace("image3", image3Uri);

                XmlWriter xmlWriter = pageWriter.XmlWriter;

                xmlWriter.WriteRaw(pageContents);

     

                //Commit Writers

                pageWriter.Commit();

                docWriter.Commit();

                docSeqWriter.Commit();

     

                //Close Document

                document.Close();

            }

     

            private static string AddImage(IXpsFixedPageWriter pageWriter, String imageSource)

            {

                XpsImage image = pageWriter.AddImage(XpsImageType.JpegImageType);

                string imageUri = image.Uri.ToString();

                Stream dstImageStream = image.GetStream();

     

                Stream srcImageStream = new FileStream(imageSource, FileMode.Open);

     

                CopyStream(srcImageStream, dstImageStream);

     

                image.Commit();

     

                return imageUri;

            }

     

            private static string AddFont(IXpsFixedPageWriter pageWriter, String fontSource)

            {

                XpsFont font = pageWriter.AddFont(false);

                string fontUri = font.Uri.ToString();

                Stream srcFontStream = new FileStream(fontSource, FileMode.Open);

                Stream dstFontStream = font.GetStream();

                CopyStream(srcFontStream, dstFontStream);

                font.Commit();

                return fontUri;

            }

     

            public

            static

            void

            CopyStream(Stream srcStream, Stream dstSteam)

            {

                int arraySz = 1024 * 1024;

                byte[] buffer = new byte[arraySz]; //1MB

                long bytesRemaining = srcStream.Length;

                while (bytesRemaining > 0)

                {

                    long copyLng = (bytesRemaining > arraySz) ? arraySz : bytesRemaining;

                    srcStream.Read(buffer, 0, (int)copyLng);

                    dstSteam.Write(buffer, 0, (int)copyLng);

                    bytesRemaining -= copyLng;

                }

            }

        }

    }

     

     

     

    //Source for src.xaml

     

    <FixedPage xmlns="http://schemas.microsoft.com/xps/2005/06" xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key" xml:lang="en-us" Width="816" Height="1056">

          <FixedPage.Resources>

                <ResourceDictionary>

                      <ImageBrush x:Key="b0" ViewportUnits="Absolute" TileMode="None" ViewboxUnits="Absolute" Viewbox="0,0,1092.26666666667,819.2" Viewport="0,0,150,100" ImageSource="image1"/>

                      <ImageBrush x:Key="b1" ViewportUnits="Absolute" TileMode="None" ViewboxUnits="Absolute" Viewbox="0,0,1092.26666666667,819.2" Viewport="0,0,150,150" ImageSource="image2"/>

                      <ImageBrush x:Key="b2" ViewportUnits="Absolute" TileMode="None" ViewboxUnits="Absolute" Viewbox="0,0,819.2,1092.26666666667" Viewport="0,0,150,150" ImageSource="image3"/>

                </ResourceDictionary>

          </FixedPage.Resources>

          <Path Fill="#FFFFFFFF" Data="F0 M 0, 0 L 816, 0 816, 1056 0, 1056Z"/>

          <Path Fill="#FFFFFFFF" Data="F0 M 0, 0 L 816, 0 816, 1056 0, 1056Z"/>

          <Path Stroke="#FF000000" StrokeThickness="2" StrokeMiterLimit="10" Data="F0 M 1, 1 L 499, 1 499, 799 1, 799Z"/>

          <Path Fill="#FFFFFFE0" Data="F0 M 2, 2 L 498, 2 498, 798 2, 798Z"/>

          <Canvas RenderTransform="1, 0, 0, 1, 2, 2">

                <Glyphs OriginX="140" OriginY="56.9666666666667" FontRenderingEmSize="36" FontUri="font1" UnicodeString="Our Vacation" Fill="#FF000000"/>

                <Path Fill="#FF008000" RenderTransform="0.866025403784439, 0.5, -0.5, 0.866025403784439, 80.3169872981078, 119.816987298108" Data="F0 M 0, 0 L 150, 0 150, 150 0, 150Z"/>

                <Path Fill="#FFFF0000" RenderTransform="1, 0, 0, 1, 210, 380" Data="F0 M 0, 0 L 170, 0 170, 170 0, 170Z"/>

                <Path Fill="#FF800000" RenderTransform="0.984807753012208, 0.17364817766693, -0.17364817766693, 0.984807753012208, 140.094420212327, 589.92077203466" Data="F0 M 0, 0 L 170, 0 170, 170 0, 170Z"/>

                <Path Fill="{StaticResource b0}" RenderTransform="1, 0, 0, 1, 130, 640" Data="F0 M 0, 0 L 150, 0 150, 100 0, 100Z"/>

                <Path Fill="{StaticResource b1}" RenderTransform="1, 0, 0, 1, 40, 140" Data="F0 M 0, 0 L 150, 0 150, 150 0, 150Z"/>

                <Path Fill="{StaticResource b2}" RenderTransform="1, 0, 0, 1, 240, 360" Data="F0 M 0, 0 L 150, 0 150, 150 0, 150Z"/>

          </Canvas>

    </FixedPage>

     

     

    Friday, July 07, 2006 10:05 PM
  • Hi ,

         Can any one please let me know what are the features that are supported in XML Paper Specification viewer -  Beta 2 version.

         I would like to know exactly is there any difference between Beta 1 & Beta 2 for opening the same XPS document in both the viewers.

         I am facing the problem when i was trying to open a jpg embedded image in Beta 2 version , the viewer is getting hangged. But the same xps file is getting opened in the Beta 1 version.

        Any one please let me know what could be the reason at the earliest possible.

    Thanks in advance,

    Santhosh.

     

     

     

    Wednesday, September 20, 2006 2:57 PM
  • Hi ,

       Can any one let me know what is the difference between the Beta 1 & Beta 2 version of the XML Paper Specification. I tried opening the XPS document generated by embedding a jpg image which was openable by Beta 1 version , but Beta 2 version is hangging while opening.

      Can any one help me out in knowing where could be the problem is.

      Thanks in advance,

      Santhosh.

     

    Wednesday, September 20, 2006 3:18 PM