Click here to Skip to main content
13,201,451 members (74,188 online)
Click here to Skip to main content


20 bookmarked
Posted 2 Aug 2008

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

, 2 Aug 2008
Well, somebody had to do it.
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();

            #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=\"\" 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!\"/>");

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

            // Commit the fixed document
            // Commite the fixed document sequence writer
            // Commit the XPS document itself

        /// <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

            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.


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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 3 Aug 2008
Article Copyright 2008 by Lee Humphries
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid