Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Hello XPS World - Part 2 of n (of too many)

, 2 Aug 2008
Well, somebody had to do it.
HelloXPSWorld.zip
HelloXPSWorld
HelloXPSWorld.csproj.user
Properties
using System;
using System.IO;
using System.Text;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;
using System.Xml;

namespace HelloXPSWorld
{
	class Program
	{
		static void Main(string[] args)
        {
            #region The setup - Creating all the objects needed
            // Create the new document
            XpsDocument xd = new XpsDocument("HelloWorld.xps", FileAccess.ReadWrite);

			// Create a new FixedDocumentSequence object in the document
			IXpsFixedDocumentSequenceWriter xdSW = xd.AddFixedDocumentSequence();

			// Ccreate a new FixedDocument object in in the document sequence
			IXpsFixedDocumentWriter xdW = xdSW.AddFixedDocument();

			// Add a new FixedPage to the FixedDocument
			IXpsFixedPageWriter xpW = xdW.AddFixedPage();

            // Add a Font to the FixedPage and get back where it ended up
			string fontURI = AddFontResourceToFixedPage(xpW, "C:\\Windows\\Fonts\\Arial.ttf");

			StringBuilder pageContents = new StringBuilder();
            #endregion

            #region The actual XPS markup
            // Try changing the Width and Height and see what you get
            pageContents.AppendLine("<FixedPage Width=\"793.76\" Height=\"1122.56\" xmlns=\"http://schemas.microsoft.com/xps/2005/06\" xml:lang=\"und\">");
			pageContents.AppendLine("<Glyphs Fill=\"#ff000000\" FontRenderingEmSize=\"16\" StyleSimulations=\"None\" OriginX=\"75.68\" OriginY=\"90.56\"");
            // Add the fontURI
            pageContents.AppendFormat(" FontUri=\"{0}\" ", fontURI);
            // HERE IT IS
			pageContents.AppendLine("  UnicodeString=\"Hello XPS World!\"/>");
			pageContents.AppendLine("</FixedPage>");
            #endregion

            #region The shutdown - Commiting all of the objects
            // Write the XPS markup out to the page
			XmlWriter xmlWriter = xpW.XmlWriter;
			xmlWriter.WriteRaw(pageContents.ToString());
			
            // Commit the page
			xpW.Commit();
            // Close the XML writer
			xmlWriter.Close();

            // Commit the fixed document
            xdW.Commit();
            // Commite the fixed document sequence writer
            xdSW.Commit();
            // Commit the XPS document itself
            xd.Close();
            #endregion
        }

        /// <summary>
        /// Add the Font Resource to the Fixed Page
        /// </summary>
        /// <param name="pageWriter">The IXpsFixedPageWriter object to 'add' the Font Resource too</param>
        /// <param name="fontFileName">Full path the font resource being used</param>
        /// <returns>The fonrURI (as a string) of the Font resource</returns>
		private static string AddFontResourceToFixedPage(IXpsFixedPageWriter pageWriter, String fontFileName)
		{
			XpsFont font = pageWriter.AddFont(false);
			using (Stream dstFontStream = font.GetStream())
			using (Stream srcFontStream = File.OpenRead(fontFileName))
			{
				CopyStream(srcFontStream, dstFontStream);

				// commit font resource to the package file
				font.Commit();
			}

            return font.Uri.ToString();
		}

		private static Int32 CopyStream(Stream srcStream, Stream dstStream)
		{
			const int size = 64 * 1024; // copy using 64K buffers
			byte[] localBuffer = new byte[size];
			int bytesRead;
			Int32 bytesMoved = 0;

			// reset stream pointers
			srcStream.Seek(0, SeekOrigin.Begin);
			dstStream.Seek(0, SeekOrigin.Begin);

			// stream position is advanced automatically by stream object
			while ((bytesRead = srcStream.Read(localBuffer, 0, size)) > 0)
			{
				dstStream.Write(localBuffer, 0, bytesRead);
				bytesMoved += bytesRead;
			}
			return bytesMoved;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Lee Humphries
Founder md8n
Australia Australia
If it ain't broke - that can be arranged.
Follow on   Twitter

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 3 Aug 2008
Article Copyright 2008 by Lee Humphries
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid