AddPageResources(IXpsFixedPageWriter fixedPageWriter) { // Collection of all resources for this page. // Key: "XpsImage", "XpsFont" // Value: List of XpsImage or XpsFont Dictionary <string, List <XpsResource> > resources = new Dictionary <string, List <XpsResource> >(); // Collections of images and fonts used in the current page. List <XpsResource> xpsImages = new List <XpsResource>(); List <XpsResource> xpsFonts = new List <XpsResource>(); try { XpsImage xpsImage; XpsFont xpsFont; // Add, Write, and Commit image1 to the current page. xpsImage = fixedPageWriter.AddImage(XpsImageType.JpegImageType); WriteToStream(xpsImage.GetStream(), image1); xpsImage.Commit(); xpsImages.Add(xpsImage); // Add image1 as a required resource. // Add, Write, and Commit font 1 to the current page. xpsFont = fixedPageWriter.AddFont(); WriteObfuscatedStream( xpsFont.Uri.ToString(), xpsFont.GetStream(), font1); xpsFont.Commit(); xpsFonts.Add(xpsFont); // Add font1 as a required resource. // Add, Write, and Commit image2 to the current page. xpsImage = fixedPageWriter.AddImage(XpsImageType.TiffImageType); WriteToStream(xpsImage.GetStream(), image2); xpsImage.Commit(); xpsImages.Add(xpsImage); // Add image2 as a required resource. // Add, Write, and Commit font2 to the current page. xpsFont = fixedPageWriter.AddFont(false); WriteToStream(xpsFont.GetStream(), font2); xpsFont.Commit(); xpsFonts.Add(xpsFont); // Add font2 as a required resource. // Return the image and font resources in a combined collection. resources.Add("XpsImage", xpsImages); resources.Add("XpsFont", xpsFonts); return(resources); } catch (XpsPackagingException xpsException) { throw xpsException; } }// end:AddPageResources()
private void AddFonts(IXpsFixedPageReader fixedPageReader, IXpsFixedPageWriter pageWriter) { // Adding font from source to resources. foreach (XpsFont font in fixedPageReader.Fonts) { if (!CheckIfFontAddedAlready(font.Uri.ToString())) { XpsFont newFont = pageWriter.AddFont(false); using (Stream dstFontStream = newFont.GetStream()) { using (Stream srcFontStream = font.GetStream()) { if (font.IsObfuscated) { WriteObfuscatedStream(font.Uri.ToString(), dstFontStream, srcFontStream); } else { WriteToStream(dstFontStream, srcFontStream); } newFont.Commit(); XpsDetails xpsFont = new XpsDetails(); xpsFont.resource = newFont; xpsFont.sourceURI = font.Uri; xpsFont.destURI = newFont.Uri; xpsFonts.Add(xpsFont); } } } } }
}// end:AddGlyphRun() // --------------------------- GetXpsFont() --------------------------- /// <summary> /// Returns the relative path to a font part in the package.</summary> /// <param name="pageWriter"> /// The page to associate the font with.</param> /// <param name="fontUri"> /// The URI for the source font file.</param> /// <returns> /// The relative path to font part in the package.</returns> /// <remarks> /// If the font has not been added previously, GetXpsFont creates a /// new font part and copies the font data from the specified /// source font URI.</remarks> private string GetXpsFont(IXpsFixedPageWriter pageWriter, string fontUri) { string packageFontUri; if (_fontDictionary.ContainsKey(fontUri)) { packageFontUri = _fontDictionary[fontUri]; } else { XpsFont xpsFont = pageWriter.AddFont(false); Stream dstStream = xpsFont.GetStream(); Stream srcStream = new FileStream(fontUri, FileMode.Open, FileAccess.Read); Byte[] streamBytes = new Byte[srcStream.Length]; srcStream.Read(streamBytes, 0, (int)srcStream.Length); dstStream.Write(streamBytes, 0, (int)srcStream.Length); _fontDictionary[fontUri] = xpsFont.Uri.ToString(); packageFontUri = xpsFont.Uri.ToString(); xpsFont.Commit(); } return(packageFontUri); }// end:GetXpsFont()