/// <summary>
        /// Create a WPF GlyphTypeface and retrieve font data from it.
        /// </summary>
        internal static XFontSource CreateFontSource(string familyName, FontResolvingOptions fontResolvingOptions,
                                                     out WpfFontFamily wpfFontFamily, out WpfTypeface wpfTypeface, out WpfGlyphTypeface wpfGlyphTypeface, string typefaceKey)
        {
            if (string.IsNullOrEmpty(typefaceKey))
            {
                typefaceKey = XGlyphTypeface.ComputeKey(familyName, fontResolvingOptions);
            }
            XFontStyle style = fontResolvingOptions.FontStyle;

#if DEBUG
            if (StringComparer.OrdinalIgnoreCase.Compare(familyName, "Segoe UI Semilight") == 0 &&
                (style & XFontStyle.BoldItalic) == XFontStyle.Italic)
            {
                familyName.GetType();
            }
#endif

            // Use WPF technique to create font data.
            wpfTypeface = XPrivateFontCollection.TryCreateTypeface(familyName, style, out wpfFontFamily);
#if DEBUG__
            if (wpfTypeface != null)
            {
                WpfGlyphTypeface          glyphTypeface;
                ICollection <WpfTypeface> list = wpfFontFamily.GetTypefaces();
                foreach (WpfTypeface tf in list)
                {
                    if (!tf.TryGetGlyphTypeface(out glyphTypeface))
                    {
                        Debug - Break.Break();
                    }
                }

                //if (!WpfTypeface.TryGetGlyphTypeface(out glyphTypeface))
                //    throw new InvalidOperationException(PSSR.CannotGetGlyphTypeface(familyName));
            }
#endif
            if (wpfFontFamily == null)
            {
                wpfFontFamily = new WpfFontFamily(familyName);
            }

            if (wpfTypeface == null)
            {
                wpfTypeface = FontHelper.CreateTypeface(wpfFontFamily, style);
            }

            // Let WPF choose the right glyph typeface.
            if (!wpfTypeface.TryGetGlyphTypeface(out wpfGlyphTypeface))
            {
                throw new InvalidOperationException(PSSR.CannotGetGlyphTypeface(familyName));
            }

            // Get or create the font source and cache it unter the specified typeface key.
            XFontSource fontSource = XFontSource.GetOrCreateFromWpf(typefaceKey, wpfGlyphTypeface);
            return(fontSource);
        }
 protected void LoadPrivateFont(XPrivateFontCollection privateFontCollection, Uri fontUri, string sFontFamilyname)
 {
     //Every font must be added to the global font collection.  There is probably some better way to do this but this was the only method that seemed to work when deploying to any server.
     //If the font has previously been added it will just error out and continue, this does not matter.
     try
     {
         privateFontCollection.Add(fontUri, sFontFamilyname);
     }
     catch
     {
     }
 }
Exemplo n.º 3
0
 //#if
 public TrueTypeDescriptor(XFont font, XPdfFontOptions options, XPrivateFontCollection privateFontCollection)
 {
     try
     {
         this.fontData = new FontData(font, options);
         this.fontName = font.Name;
         Initialize();
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Converts an DOM Font to an XFont.
        /// </summary>
        internal static XFont FontToXFont(Font font, XPrivateFontCollection pfc,
                                          PdfFontEncoding encoding)
        {
            XFont xFont = null;

            // #PFC
            XPdfFontOptions options = null;

            options = new XPdfFontOptions(encoding);
            XFontStyle style = GetXStyle(font);

            if (xFont == null)
            {
                xFont = new XFont(font.Name, font.Size, style, options);
            }
#if DEBUG
            CreateFontCounter++;
#endif
            return(xFont);
        }
        public HttpResponseMessage Print(int businessTripId)
        {
            BusinessTrip trip = this.tasks.BusinessTripsTasks.GetBusinessTrip(businessTripId);

            // First of all initialize the global XPrivateFontCollection.
            XPrivateFontCollection privateFontCollection = XPrivateFontCollection.Global;
            Uri fontUri = new Uri(MappedApplicationPath + "Fonts\\");

            LoadPrivateFont(privateFontCollection, fontUri, "./#Source Sans Pro");             //Example of adding a private font family - the font file should be located in the Uri path above, it will get embedded in the PDF.

            Document document = new Document();

            document.Info.Author = "Saffron";
            Section section = document.AddSection();

            DefineStyles(document);
            AddHeader(document, section, trip);
            AddReportIdentifier(document, section, trip);
            AddDetails(document, section, trip);
            AddSummary(document, section, trip);

            PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);

            renderer.Document = document;
            renderer.RenderDocument();

            using (MemoryStream ms = new MemoryStream())
            {
                renderer.Save(ms, false);

                byte[] res = ms.ToArray();
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new ByteArrayContent(res);
                response.Content.Headers.ContentDisposition          = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = "Raport.pdf";
                return(response);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Converts an DOM Font to an XFont.
        /// </summary>
        internal static XFont FontToXFont(Font font, XPrivateFontCollection pfc,
                                          PdfFontEncoding encoding)
        {
            XFont xFont = null;

#if GDI____  // done in PDFsharp
#if CACHE_FONTS
            string signature = BuildSignature(font, unicode, fontEmbedding);
            xFont = fontCache[signature] as XFont;
            if (xFont == null)
            {
                XPdfFontOptions options = null;
                options = new XPdfFontOptions(fontEmbedding, unicode);
                XFontStyle style = GetXStyle(font);
                xFont = new XFont(font.Name, font.Size, style, options);
                fontCache[signature] = xFont;
            }
#else
            XPdfFontOptions options = null;
            options = new XPdfFontOptions(encoding, fontEmbedding);
            XFontStyle style = GetXStyle(font);
            if (pfc != null && pfc.PrivateFontCollection != null)
            {
                // Is it a private font?
                try
                {
                    foreach (System.Drawing.FontFamily ff in pfc.PrivateFontCollection.Families)
                    {
                        if (String.Compare(ff.Name, font.Name, true) == 0)
                        {
                            xFont = new XFont(ff, font.Size, style, options, pfc);
                            break;
                        }
                    }
                }
                catch
                {
#if DEBUG
                    pfc.GetType();
#endif
                }
            }
#endif
#endif

#if WPF___
            XPdfFontOptions options = null;
            options = new XPdfFontOptions(encoding, fontEmbedding);
            XFontStyle style = GetXStyle(font);
            //if (pfc != null &&
            //  pfc.PrivateFontCollection != null)
            //{
            //  // Is it a private font?
            //  try
            //  {
            //    foreach (System.Drawing.FontFamily ff in pfc.PrivateFontCollection.Families)
            //    {
            //      if (String.Compare(ff.Name, font.Name, true) == 0)
            //      {
            //        xFont = new XFont(ff, font.Size, style, options, pfc);
            //        break;
            //      }
            //    }
            //  }
            //  catch
            //  {
            //  }
            //}
#endif

            // #PFC
            XPdfFontOptions options = null;
            options = new XPdfFontOptions(encoding);
            XFontStyle style = GetXStyle(font);

            if (xFont == null)
            {
                xFont = new XFont(font.Name, font.Size, style, options);
            }
#if DEBUG
            CreateFontCounter++;
#endif
            return(xFont);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create the font image using GDI+ functionality.
        /// </summary>
        void CreateGdiFontImage(XFont font, XPdfFontOptions options, XPrivateFontCollection privateFontCollection)
        {
            System.Drawing.Font   gdiFont = font.RealizeGdiFont();
            NativeMethods.LOGFONT logFont;
#if DEBUG_
            logFont = new NativeMethods.LOGFONT();
            gdiFont.ToLogFont(logFont);
            Debug.WriteLine("FontData: " + logFont.lfFaceName);
#endif
            this.data = null;
            if (privateFontCollection != null)
            {
                //XPrivateFont privateFont = privateFontCollection.FindFont(logFont.lfFaceName, logFont.lfWeight >= 700, logFont.lfItalic > 0);
                XPrivateFont privateFont = privateFontCollection.FindFont(font.Name, font.Bold, font.Italic);
                if (privateFont != null)
                {
                    int size = privateFont.GetFontData(ref this.data, 0);
                    if (size > 0)
                    {
                        this.data = new byte[size];
                        privateFont.GetFontData(ref this.data, size);
                    }
                }
            }
            if (this.data == null)
            {
                int    error;
                IntPtr hfont = gdiFont.ToHfont();
                IntPtr hdc   = NativeMethods.GetDC(IntPtr.Zero);
                error = Marshal.GetLastWin32Error();
                IntPtr oldFont = NativeMethods.SelectObject(hdc, hfont);
                error = Marshal.GetLastWin32Error();
                // size is exactly the size of the font file.
                int size = NativeMethods.GetFontData(hdc, 0, 0, null, 0);
                error = Marshal.GetLastWin32Error();
                if (size > 0)
                {
                    this.data = new byte[size];
                    int effectiveSize = NativeMethods.GetFontData(hdc, 0, 0, this.data, this.data.Length);
                    Debug.Assert(size == effectiveSize);
                    NativeMethods.SelectObject(hdc, oldFont);
                    NativeMethods.ReleaseDC(IntPtr.Zero, hdc);
                    error.GetType();
                }
                else
                {
                    // Sometimes size is -1 (GDI_ERROR), but I cannot determine why. It happens only with the font 'Symbol'.
                    // The issue occurs the first time in early 2005, when I start writing PDFsharp. I could not fix it and after
                    // some code refactoring the problem disappears.
                    // There was never a report from anyone about this issue.
                    // Now I get it again (while debugging QBX 2006). Maybe it is a problem with my PC at my home office.
                    // As a work-around I create a new font handle with a different height value. This works. Maybe the
                    // font file gets locked somewhere. Very very strange.

                    // IF SOMEONE ELSE COMES HERE PLEASE LET ME KNOW!

                    // Clean up old handles
                    NativeMethods.SelectObject(hdc, oldFont);
                    NativeMethods.ReleaseDC(IntPtr.Zero, hdc);

                    // Try again with new font handle
                    logFont = new NativeMethods.LOGFONT();
                    gdiFont.ToLogFont(logFont);
                    logFont.lfHeight += 1; // force new handle
                    IntPtr hfont2 = NativeMethods.CreateFontIndirect(logFont);
                    hdc     = NativeMethods.GetDC(IntPtr.Zero);
                    error   = Marshal.GetLastWin32Error();
                    oldFont = NativeMethods.SelectObject(hdc, hfont2);
                    error   = Marshal.GetLastWin32Error();
                    // size is exactly the size of the font file.
                    size  = NativeMethods.GetFontData(hdc, 0, 0, null, 0);
                    error = Marshal.GetLastWin32Error();
                    if (size > 0)
                    {
                        this.data = new byte[size];
                        int effectiveSize = NativeMethods.GetFontData(hdc, 0, 0, this.data, this.data.Length);
                        Debug.Assert(size == effectiveSize);
                    }
                    NativeMethods.SelectObject(hdc, oldFont);
                    NativeMethods.ReleaseDC(IntPtr.Zero, hdc);
                    NativeMethods.DeleteObject(hfont2);
                    error.GetType();
                }
            }
            if (this.data == null)
            {
                throw new InvalidOperationException("Internal error. Font data could not retrieved.");
            }
        }
Exemplo n.º 8
0
        //#endif

        //#if WPF
        //    public TrueTypeDescriptor(XFont font, XPdfFontOptions options)
        //    {
        //      try
        //      {
        //        this.fontData = new FontData(font, options);
        //        this.fontName = font.Name;
        //        Initialize();
        //      }
        //      catch (Exception ex)
        //      {
        //        throw ex;
        //      }
        //    }
        //#endif

        //internal TrueTypeDescriptor(FontSelector selector)
        //{
        //  throw new NotImplementedException("TrueTypeDescriptor(FontSelector selector)");
        //}

        internal TrueTypeDescriptor(XFont font,
                                    XPrivateFontCollection privateFontCollection)
            : this(font, font.PdfOptions, privateFontCollection)
        {
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            // First of all initialize the global XPrivateFontCollection.
            XPrivateFontCollection globalFontCollection = XPrivateFontCollection.Global;

            // Due to technical limitations of the current implementation of PDFsharp the global font collection
            // must be initialized when your applications starts and belongs implicitely to all PDF documents.


            // Adding private fonts differs between GDI+ and WPF

#if GDI
#endif

#if WPF
            // Without the following line of code the Uri constructor (see below) fails...
            new System.Windows.Application();

            // Add the 3 type faces of 'FrutigerLight' from the resources
            Uri uri = new Uri("pack://application:,,,/");
            //const string name = "./FrutigerFonts/#FrutigerLight";
            //const string name = "./Fonts/#Early Tickertape";
            const string name = "./#Early Tickertape";
            globalFontCollection.Add(uri, name);

            // Add 2 type faces of 'Frutiger' from the resources
            //globalFontCollection.Add(uri, "./FrutigerFonts/#Frutiger");
            //globalFontCollection.Add(uri, "./Fonts/#Oblivious font");
            globalFontCollection.Add(uri, "./#Oblivious font");
#endif

            // Create new document
            PdfDocument document = new PdfDocument();

            // Set font embedding to Always
            XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

            PdfPage   page = document.AddPage();
            XGraphics gfx  = XGraphics.FromPdfPage(page);

            const string familyName1 = "Early Tickertape";

            XFont font1 = new XFont(familyName1, 24, XFontStyle.Regular, options);
            gfx.DrawString("Early Tickertape", font1, XBrushes.Black, 100, 100);

            XFont font2 = new XFont(familyName1, 24, XFontStyle.Bold, options);
            gfx.DrawString("Early Tickertape bold (fontface n.a.)", font2, XBrushes.Black, 100, 150);

            XFont font3 = new XFont(familyName1, 24, XFontStyle.Italic, options);
            gfx.DrawString("Early Tickertape italic (fontface n.a.)", font3, XBrushes.Black, 100, 200);

            XFont font4 = new XFont(familyName1, 24, XFontStyle.BoldItalic, options);
            gfx.DrawString("Early Tickertape bold italic (fontface n.a.)", font4, XBrushes.Black, 100, 250);

            const string familyName2 = "Oblivious font";

            XFont font5 = new XFont(familyName2, 24, XFontStyle.Regular, options);
            gfx.DrawString("Oblivious regular", font5, XBrushes.Black, 100, 400);

            XFont font6 = new XFont(familyName2, 24, XFontStyle.Bold, options);
            gfx.DrawString("Oblivious bold (fontface n.a.)", font6, XBrushes.Black, 100, 450);

            XFont font7 = new XFont(familyName2, 24, XFontStyle.Italic, options);
            gfx.DrawString("Oblivious italic (fontface n.a.)", font7, XBrushes.Black, 100, 500);

            XFont font8 = new XFont(familyName2, 24, XFontStyle.BoldItalic, options);
            gfx.DrawString("Oblivious bold italic (fontface n.a.)", font8, XBrushes.Black, 100, 550);

#if false
            const string familyName1 = "FrutigerLight";

            XFont font1 = new XFont(familyName1, 24, XFontStyle.Regular, options);
            gfx.DrawString("Frutiger Light regular", font1, XBrushes.Black, 100, 100);

            XFont font2 = new XFont(familyName1, 24, XFontStyle.Bold, options);
            gfx.DrawString("Frutiger Light bold", font2, XBrushes.Black, 100, 150);

            XFont font3 = new XFont(familyName1, 24, XFontStyle.Italic, options);
            gfx.DrawString("Frutiger Light italic", font3, XBrushes.Black, 100, 200);

            XFont font4 = new XFont(familyName1, 24, XFontStyle.BoldItalic, options);
            gfx.DrawString("Frutiger Light bold italic (fontface n.a.)", font4, XBrushes.Black, 100, 250);

            const string familyName2 = "Frutiger";

            XFont font5 = new XFont(familyName2, 24, XFontStyle.Regular, options);
            gfx.DrawString("Frutiger regular", font5, XBrushes.Black, 100, 400);

            XFont font6 = new XFont(familyName2, 24, XFontStyle.Bold, options);
            gfx.DrawString("Frutiger bold", font6, XBrushes.Black, 100, 450);

            XFont font7 = new XFont(familyName2, 24, XFontStyle.Italic, options);
            gfx.DrawString("Frutiger italic (fontface n.a.)", font7, XBrushes.Black, 100, 500);

            XFont font8 = new XFont(familyName2, 24, XFontStyle.BoldItalic, options);
            gfx.DrawString("Frutiger bold italic (fontface n.a.)", font8, XBrushes.Black, 100, 550);
#endif

            string filename = Guid.NewGuid().ToString("N") + "_tempfile.pdf";
            // Save the document...
            document.Save(filename);
            // ...and start a viewer.
            Process.Start(filename);
        }