/// <summary>
        /// Creates a new XPS Diagram from scratch
        /// </summary>
        /// <param name="fileName">Name of the file. Important: If the file already exists, it will be deleted</param>
        /// <param name="format">Format of the page</param>
        public XpsDocumentHelper(string fileName, PageFormat format)
        {
            // Initialize teh Dictionaries for fonts and images
            _embeddedFonts = new Dictionary<string, Uri>();
            _embeddedImages = new Dictionary<string, ImageInfo>();

            // The file is created from scratch so if it already exists it will be removed.
            if (File.Exists(fileName))
                File.Delete(fileName);

            // Create the new package with all internal things that are required
            _package = Package.Open(fileName);
            _document = new XpsDocument(_package);
            _xpsDocSeqWriter = _document.AddFixedDocumentSequence();
            _xpsDocWriter = _xpsDocSeqWriter.AddFixedDocument();
            _xpsPageWriter = _xpsDocWriter.AddFixedPage();
            _xmlWriter = _xpsPageWriter.XmlWriter;

            // Write the document header, I fixed everything to the the en-US culture here.
            StartPage(format);
        }
 /// <summary>
 /// Write the XML Element for a fixed Page
 /// </summary>
 /// <param name="format">Format of the next page.</param>
 private void StartPage(PageFormat format)
 {
     _xmlWriter.WriteStartElement("FixedPage");
     _xmlWriter.WriteAttributeString("Width", format.Width.ToString("0.00", CultureInfo.CreateSpecificCulture("en-US")));
     _xmlWriter.WriteAttributeString("Height", format.Height.ToString("0.00", CultureInfo.CreateSpecificCulture("en-US")));
     _xmlWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/xps/2005/06");
     _xmlWriter.WriteAttributeString("xml:lang", "en-US");
     _pageStarted = true;
 }
        /// <summary>
        /// End current page and start with next page.
        /// </summary>
        /// <param name="format">Format of the next page.</param>
        /// <remarks>
        /// Attention: For each page you have to add the references to fonts and pictures!
        /// </remarks>
        public void NextPage(PageFormat format)
        {
            // Close current page
            _xpsPageWriter.Commit();

            // Create new page.
            _xpsPageWriter = _xpsDocWriter.AddFixedPage();
            _xmlWriter = _xpsPageWriter.XmlWriter;

            // Copy all Fonts
            var part = _package.GetPart(_xpsPageWriter.Uri);
            foreach (var uri in _embeddedFonts.Values)
                part.CreateRelationship(uri, TargetMode.Internal, RequiredResourceRelationship);

            // Copy all Images
            foreach (var img in _embeddedImages.Values)
                part.CreateRelationship(img.Uri, TargetMode.Internal, RequiredResourceRelationship);

            StartPage(format);
        }