Пример #1
0
        /// <summary>
        ///     Creates a character indexed font from <i>cidFont</i>
        /// </summary>
        /// <remarks>
        ///     The <i>font</i> and <i>cidFont</i> will be different object
        ///     references since the <i>font</i> parameter will most likely
        ///     be a <see cref="ProxyFont"/>.
        /// </remarks>
        /// <param name="pdfFontID">The Pdf font identifier, e.g. F15</param>
        /// <param name="font">Required to access the font descriptor.</param>
        /// <param name="cidFont">The underlying CID font.</param>
        /// <returns></returns>
        private PdfFont CreateCIDFont(
            string pdfFontID, Font font, CIDFont cidFont)
        {
            // The font descriptor is required to access licensing details are
            // obtain the font program itself as a byte array
            IFontDescriptor descriptor = font.Descriptor;

            // A compressed stream that stores the font program
            PdfFontFile fontFile = new PdfFontFile(
                NextObjectId(), descriptor.FontData);

            // Add indirect reference to FontFile object to descriptor
            PdfFontDescriptor pdfDescriptor = MakeFontDescriptor(pdfFontID, cidFont);

            pdfDescriptor.FontFile2 = fontFile;

            PdfCIDSystemInfo pdfCidSystemInfo = new PdfCIDSystemInfo(
                cidFont.Registry, cidFont.Ordering, cidFont.Supplement);

            PdfCIDFont pdfCidFont = new PdfCIDFont(
                NextObjectId(), PdfFontSubTypeEnum.CIDFontType2, font.FontName);

            pdfCidFont.SystemInfo   = pdfCidSystemInfo;
            pdfCidFont.Descriptor   = pdfDescriptor;
            pdfCidFont.DefaultWidth = new PdfNumeric(cidFont.DefaultWidth);
            pdfCidFont.Widths       = cidFont.WArray;

            // Create a ToUnicode CMap that maps characters codes (GIDs) to
            // unicode values.  Very important to ensure searching and copying
            // from a PDF document works correctly.
            PdfCMap pdfCMap = new PdfCMap(NextObjectId());

            pdfCMap.AddFilter(new FlateFilter());
            pdfCMap.SystemInfo = pdfCidSystemInfo;
            pdfCMap.AddBfRanges(cidFont.CMapEntries);

            // Create a PDF object to represent the CID font
            PdfType0Font pdfFont = new PdfType0Font(
                NextObjectId(), pdfFontID, font.FontName);

            pdfFont.Encoding   = new PdfName(cidFont.Encoding);
            pdfFont.Descendant = pdfCidFont;
            pdfFont.ToUnicode  = pdfCMap;

            // Add all the Pdf objects to the document.  MakeFont will add the actual
            // PdfFont object to the document.
            creator.AddObject(pdfDescriptor);
            creator.AddObject(pdfCidFont);
            creator.AddObject(pdfCMap);
            creator.AddObject(fontFile);

            return(pdfFont);
        }
Пример #2
0
        /// <summary>
        ///     Returns a subclass of the PdfFont class that may be one of
        ///     PdfType0Font, PdfType1Font or PdfTrueTypeFont.  The type of
        ///     subclass returned is determined by the type of the <i>font</i>
        ///     parameter.
        /// </summary>
        /// <param name="pdfFontID">The PDF font identifier, e.g. F15</param>
        /// <param name="font">Underlying font object.</param>
        /// <returns></returns>
        public PdfFont MakeFont(string pdfFontID, Font font)
        {
            PdfFont pdfFont = null;

            if (font is Base14Font)
            {
                // One of the standard base 14 fonts
                Base14Font base14 = (Base14Font)font;
                pdfFont = CreateBase14Font(pdfFontID, base14);
            }
            else
            {
                // Will load underlying font if proxy
                IFontMetric realMetrics = GetFontMetrics(font);

                if (realMetrics is Base14Font)
                {
                    // A non-embeddable font that has been defaulted to a base 14 font
                    Base14Font base14 = (Base14Font)realMetrics;
                    pdfFont = CreateBase14Font(pdfFontID, base14);
                }
                else if (realMetrics is TrueTypeFont)
                {
                    // TrueTypeFont restricted to the WinAnsiEncoding scheme
                    // that is linked instead of embedded in the PDF.
                    TrueTypeFont ttf = (TrueTypeFont)realMetrics;
                    pdfFont = CreateTrueTypeFont(pdfFontID, font, ttf);
                }
                else
                {
                    // A character indexed font that may be subsetted.
                    CIDFont cid = (CIDFont)realMetrics;
                    pdfFont = CreateCIDFont(pdfFontID, font, cid);
                }
            }

            // This should never happen, but it's worth checking
            if (pdfFont == null)
            {
                throw new Exception("Unable to create Pdf font object for " + pdfFontID);
            }

            creator.AddObject(pdfFont);

            return(pdfFont);
        }