Click here to Skip to main content
15,893,161 members
Articles / Desktop Programming / WPF

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Aug 2008CPOL3 min read 61.4K   1.3K   19  
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();
            #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)


Written By
Founder md8n
Timor-Leste Timor-Leste
If it ain't broke - that can be arranged.

Comments and Discussions