예제 #1
0
 /// <summary>Gets a set of registered font names.</summary>
 /// <returns>a set of registered fonts</returns>
 public static ICollection <String> GetRegisteredFonts()
 {
     return(FontProgramFactory.GetRegisteredFonts());
 }
예제 #2
0
 /// <summary>Registers all the fonts in a directory.</summary>
 /// <param name="dirPath">the directory path to be registered as a font directory path</param>
 /// <returns>the number of fonts registered</returns>
 public static int RegisterDirectory(String dirPath)
 {
     return(FontProgramFactory.RegisterFontDirectory(dirPath));
 }
예제 #3
0
 /// <summary>Register fonts in some probable directories.</summary>
 /// <remarks>
 /// Register fonts in some probable directories. It usually works in Windows,
 /// Linux and Solaris.
 /// </remarks>
 /// <returns>the number of fonts registered</returns>
 public static int RegisterSystemDirectories()
 {
     return(FontProgramFactory.RegisterSystemFontDirectories());
 }
예제 #4
0
 /// <summary>Register a font by giving explicitly the font family and name.</summary>
 /// <param name="familyName">the font family</param>
 /// <param name="fullName">the font name</param>
 /// <param name="path">the font path</param>
 public static void RegisterFamily(String familyName, String fullName, String path)
 {
     FontProgramFactory.RegisterFontFamily(familyName, fullName, path);
 }
예제 #5
0
 /// <summary>Register a font file and use an alias for the font contained in it.</summary>
 /// <param name="path">the path to a font file</param>
 /// <param name="alias">the alias you want to use for the font</param>
 public static void Register(String path, String alias)
 {
     FontProgramFactory.RegisterFont(path, alias);
 }
예제 #6
0
        /// <summary>
        /// Creates a
        /// <see cref="PdfFont"/>
        /// instance from the TrueTypeCollection given by the path to the .ttc file.
        /// </summary>
        /// <param name="ttc">the path of the .ttc file</param>
        /// <param name="ttcIndex">the index of the font in the collection, zero-based</param>
        /// <param name="encoding">
        /// the encoding of the font to be created. See
        /// <see cref="iText.IO.Font.PdfEncodings"/>
        /// </param>
        /// <param name="embedded">indicates whether the font is to be embedded into the target document</param>
        /// <param name="cached">indicates whether the font will be cached</param>
        /// <returns>
        /// created
        /// <see cref="PdfFont"/>
        /// instance
        /// </returns>
        /// <exception cref="System.IO.IOException">
        /// in case the file is not found, contents of the TrueTypeCollection is mal-formed
        /// or an error occurred during reading the font
        /// </exception>
        public static PdfFont CreateTtcFont(String ttc, int ttcIndex, String encoding, bool embedded, bool cached)
        {
            FontProgram fontProgram = FontProgramFactory.CreateFont(ttc, ttcIndex, cached);

            return(CreateFont(fontProgram, encoding, embedded));
        }
예제 #7
0
        /// <summary>
        /// Created a
        /// <see cref="PdfFont"/>
        /// instance by the bytes of the underlying font program.
        /// </summary>
        /// <param name="fontProgram">the bytes of the underlying font program</param>
        /// <param name="encoding">
        /// the encoding of the font to be created. See
        /// <see cref="iText.IO.Font.PdfEncodings"/>
        /// </param>
        /// <param name="embedded">indicates whether the font is to be embedded into the target document</param>
        /// <param name="cached">indicates whether the font will be cached</param>
        /// <returns>
        /// created
        /// <see cref="PdfFont"/>
        /// instance
        /// </returns>
        /// <exception cref="System.IO.IOException">this exception is actually never thrown. Will be removed in 7.1.</exception>
        public static PdfFont CreateFont(byte[] fontProgram, String encoding, bool embedded, bool cached)
        {
            FontProgram fp = FontProgramFactory.CreateFont(null, fontProgram, cached);

            return(CreateFont(fp, encoding, embedded));
        }
 public TestSimpleFont(FontEncoding fontEncoding)
 {
     this.fontEncoding = fontEncoding;
     SetFontProgram(FontProgramFactory.CreateFont(StandardFonts.HELVETICA));
 }
예제 #9
0
        internal PdfType0Font(PdfDictionary fontDictionary)
            : base(fontDictionary)
        {
            newFont = false;
            PdfDictionary cidFont       = fontDictionary.GetAsArray(PdfName.DescendantFonts).GetAsDictionary(0);
            PdfObject     cmap          = fontDictionary.Get(PdfName.Encoding);
            PdfObject     toUnicode     = fontDictionary.Get(PdfName.ToUnicode);
            CMapToUnicode toUnicodeCMap = FontUtil.ProcessToUnicode(toUnicode);

            if (cmap.IsName() && (PdfEncodings.IDENTITY_H.Equals(((PdfName)cmap).GetValue()) || PdfEncodings.IDENTITY_V
                                  .Equals(((PdfName)cmap).GetValue())))
            {
                if (toUnicodeCMap == null)
                {
                    String uniMap = GetUniMapFromOrdering(GetOrdering(cidFont));
                    toUnicodeCMap = FontUtil.GetToUnicodeFromUniMap(uniMap);
                    if (toUnicodeCMap == null)
                    {
                        toUnicodeCMap = FontUtil.GetToUnicodeFromUniMap(PdfEncodings.IDENTITY_H);
                        ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Font.PdfType0Font));
                        logger.Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.UNKNOWN_CMAP, uniMap));
                    }
                }
                fontProgram  = DocTrueTypeFont.CreateFontProgram(cidFont, toUnicodeCMap);
                cmapEncoding = CreateCMap(cmap, null);
                System.Diagnostics.Debug.Assert(fontProgram is IDocFontProgram);
                embedded = ((IDocFontProgram)fontProgram).GetFontFile() != null;
            }
            else
            {
                String cidFontName = cidFont.GetAsName(PdfName.BaseFont).GetValue();
                String uniMap      = GetUniMapFromOrdering(GetOrdering(cidFont));
                if (uniMap != null && uniMap.StartsWith("Uni") && CidFontProperties.IsCidFont(cidFontName, uniMap))
                {
                    try {
                        fontProgram  = FontProgramFactory.CreateFont(cidFontName);
                        cmapEncoding = CreateCMap(cmap, uniMap);
                        embedded     = false;
                    }
                    catch (System.IO.IOException) {
                        fontProgram  = null;
                        cmapEncoding = null;
                    }
                }
                else
                {
                    if (toUnicodeCMap == null)
                    {
                        toUnicodeCMap = FontUtil.GetToUnicodeFromUniMap(uniMap);
                    }
                    if (toUnicodeCMap != null)
                    {
                        fontProgram  = DocTrueTypeFont.CreateFontProgram(cidFont, toUnicodeCMap);
                        cmapEncoding = CreateCMap(cmap, uniMap);
                    }
                }
                if (fontProgram == null)
                {
                    throw new PdfException(MessageFormatUtil.Format(PdfException.CannotRecogniseDocumentFontWithEncoding, cidFontName
                                                                    , cmap));
                }
            }
            // DescendantFonts is a one-element array specifying the CIDFont dictionary that is the descendant of this Type 0 font.
            PdfDictionary cidFontDictionary = fontDictionary.GetAsArray(PdfName.DescendantFonts).GetAsDictionary(0);
            // Required according to the spec
            PdfName subtype = cidFontDictionary.GetAsName(PdfName.Subtype);

            if (PdfName.CIDFontType0.Equals(subtype))
            {
                cidFontType = CID_FONT_TYPE_0;
            }
            else
            {
                if (PdfName.CIDFontType2.Equals(subtype))
                {
                    cidFontType = CID_FONT_TYPE_2;
                }
                else
                {
                    LogManager.GetLogger(GetType()).Error(iText.IO.LogMessageConstant.FAILED_TO_DETERMINE_CID_FONT_SUBTYPE);
                }
            }
            longTag = new SortedSet <int>();
            subset  = false;
        }
예제 #10
0
        /// <summary>Returns the Font.</summary>
        /// <returns>the font</returns>
        public virtual FontProgram GetFont() {
            if (font != null) {
                return font;
            }
            FontProgram ff2 = FontProgramFactory.CreateRegisteredFont(faceName, ((italic != 0) ? FontStyles.ITALIC : 0
                ) | ((bold != 0) ? FontStyles.BOLD : 0));
            encoding = FontEncoding.CreateFontEncoding(PdfEncodings.WINANSI);
            font = ff2;
            if (font != null) {
                return font;
            }
            String fontName;
            if (faceName.Contains("courier") || faceName.Contains("terminal") || faceName.Contains("fixedsys")) {
                fontName = fontNames[MARKER_COURIER + italic + bold];
            }
            else {
                if (faceName.Contains("ms sans serif") || faceName.Contains("arial") || faceName.Contains("system")) {
                    fontName = fontNames[MARKER_HELVETICA + italic + bold];
                }
                else {
                    if (faceName.Contains("arial black")) {
                        fontName = fontNames[MARKER_HELVETICA + italic + MARKER_BOLD];
                    }
                    else {
                        if (faceName.Contains("times") || faceName.Contains("ms serif") || faceName.Contains("roman")) {
                            fontName = fontNames[MARKER_TIMES + italic + bold];
                        }
                        else {
                            if (faceName.Contains("symbol")) {
                                fontName = fontNames[MARKER_SYMBOL];
                            }
                            else {
                                int pitch = pitchAndFamily & 3;
                                int family = (pitchAndFamily >> 4) & 7;
                                switch (family) {
                                    case FF_MODERN: {
                                        fontName = fontNames[MARKER_COURIER + italic + bold];
                                        break;
                                    }

                                    case FF_ROMAN: {
                                        fontName = fontNames[MARKER_TIMES + italic + bold];
                                        break;
                                    }

                                    case FF_SWISS:
                                    case FF_SCRIPT:
                                    case FF_DECORATIVE: {
                                        fontName = fontNames[MARKER_HELVETICA + italic + bold];
                                        break;
                                    }

                                    default: {
                                        switch (pitch) {
                                            case FIXED_PITCH: {
                                                fontName = fontNames[MARKER_COURIER + italic + bold];
                                                break;
                                            }

                                            default: {
                                                fontName = fontNames[MARKER_HELVETICA + italic + bold];
                                                break;
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            try {
                font = FontProgramFactory.CreateFont(fontName);
                encoding = FontEncoding.CreateFontEncoding(PdfEncodings.WINANSI);
            }
            catch (System.IO.IOException e) {
                throw new Exception(e.Message, e);
            }
            return font;
        }
예제 #11
0
 /// <summary>Checks if a certain font is registered.</summary>
 /// <param name="fontName">the name of the font that has to be checked.</param>
 /// <returns><c>true</c> if the font is found, <c>false</c> otherwise</returns>
 public static bool IsRegistered(String fontName)
 {
     return(FontProgramFactory.IsRegisteredFont(fontName));
 }
예제 #12
0
        internal static Type1Font CreateFontProgram(PdfDictionary fontDictionary, FontEncoding fontEncoding, CMapToUnicode
                                                    toUnicode)
        {
            PdfName baseFontName = fontDictionary.GetAsName(PdfName.BaseFont);
            String  baseFont;

            if (baseFontName != null)
            {
                baseFont = baseFontName.GetValue();
            }
            else
            {
                baseFont = FontUtil.CreateRandomFontName();
            }
            if (!fontDictionary.ContainsKey(PdfName.FontDescriptor))
            {
                Type1Font type1StdFont;
                try {
                    //if there are no font modifiers, cached font could be used,
                    //otherwise a new instance should be created.
                    type1StdFont = (Type1Font)FontProgramFactory.CreateFont(baseFont, true);
                }
                catch (Exception) {
                    type1StdFont = null;
                }
                if (type1StdFont != null)
                {
                    return(type1StdFont);
                }
            }
            iText.Kernel.Font.DocType1Font fontProgram = new iText.Kernel.Font.DocType1Font(baseFont);
            PdfDictionary fontDesc = fontDictionary.GetAsDictionary(PdfName.FontDescriptor);

            fontProgram.subtype = fontDesc.GetAsName(PdfName.Subtype);
            FillFontDescriptor(fontProgram, fontDesc);
            PdfNumber firstCharNumber = fontDictionary.GetAsNumber(PdfName.FirstChar);
            int       firstChar       = firstCharNumber != null?Math.Max(firstCharNumber.IntValue(), 0) : 0;

            int[] widths = FontUtil.ConvertSimpleWidthsArray(fontDictionary.GetAsArray(PdfName.Widths), firstChar, fontProgram
                                                             .GetMissingWidth());
            fontProgram.avgWidth = 0;
            int glyphsWithWidths = 0;

            for (int i = 0; i < 256; i++)
            {
                Glyph glyph = new Glyph(i, widths[i], fontEncoding.GetUnicode(i));
                fontProgram.codeToGlyph.Put(i, glyph);
                if (glyph.HasValidUnicode())
                {
                    //FontEncoding.codeToUnicode table has higher priority
                    if (fontEncoding.ConvertToByte(glyph.GetUnicode()) == i)
                    {
                        fontProgram.unicodeToGlyph.Put(glyph.GetUnicode(), glyph);
                    }
                }
                else
                {
                    if (toUnicode != null)
                    {
                        glyph.SetChars(toUnicode.Lookup(i));
                    }
                }
                if (widths[i] > 0)
                {
                    glyphsWithWidths++;
                    fontProgram.avgWidth += widths[i];
                }
            }
            if (glyphsWithWidths != 0)
            {
                fontProgram.avgWidth /= glyphsWithWidths;
            }
            return(fontProgram);
        }
예제 #13
0
        internal PdfType0Font(PdfDictionary fontDictionary)
            : base(fontDictionary)
        {
            CheckFontDictionary(fontDictionary, PdfName.Type0);
            newFont = false;
            PdfDictionary cidFont = fontDictionary.GetAsArray(PdfName.DescendantFonts).GetAsDictionary(0);
            String        cmap    = fontDictionary.GetAsName(PdfName.Encoding).GetValue();

            if (PdfEncodings.IDENTITY_H.Equals(cmap) || PdfEncodings.IDENTITY_V.Equals(cmap))
            {
                PdfObject     toUnicode     = fontDictionary.Get(PdfName.ToUnicode);
                CMapToUnicode toUnicodeCMap = FontUtil.ProcessToUnicode(toUnicode);
                if (toUnicodeCMap == null)
                {
                    String uniMap = GetUniMapFromOrdering(GetOrdering(cidFont));
                    toUnicodeCMap = FontUtil.GetToUnicodeFromUniMap(uniMap);
                    if (toUnicodeCMap == null)
                    {
                        toUnicodeCMap = FontUtil.GetToUnicodeFromUniMap(PdfEncodings.IDENTITY_H);
                        ILogger logger = LoggerFactory.GetLogger(typeof(iText.Kernel.Font.PdfType0Font));
                        logger.Error(String.Format(LogMessageConstant.UNKNOWN_CMAP, uniMap));
                    }
                }
                fontProgram  = DocTrueTypeFont.CreateFontProgram(cidFont, toUnicodeCMap);
                cmapEncoding = new CMapEncoding(cmap);
                System.Diagnostics.Debug.Assert(fontProgram is IDocFontProgram);
                embedded    = ((IDocFontProgram)fontProgram).GetFontFile() != null;
                cidFontType = CID_FONT_TYPE_2;
            }
            else
            {
                String cidFontName = cidFont.GetAsName(PdfName.BaseFont).GetValue();
                String uniMap      = GetUniMapFromOrdering(GetOrdering(cidFont));
                if (uniMap != null && uniMap.StartsWith("Uni") && CidFontProperties.IsCidFont(cidFontName, uniMap))
                {
                    try {
                        fontProgram  = FontProgramFactory.CreateFont(cidFontName);
                        cmapEncoding = new CMapEncoding(cmap, uniMap);
                        embedded     = false;
                    }
                    catch (System.IO.IOException) {
                        fontProgram  = null;
                        cmapEncoding = null;
                    }
                }
                else
                {
                    CMapToUnicode toUnicodeCMap = FontUtil.GetToUnicodeFromUniMap(uniMap);
                    if (toUnicodeCMap != null)
                    {
                        fontProgram  = DocTrueTypeFont.CreateFontProgram(cidFont, toUnicodeCMap);
                        cmapEncoding = new CMapEncoding(cmap, uniMap);
                    }
                }
                if (fontProgram == null)
                {
                    throw new PdfException(String.Format("Cannot recognise document font {0} with {1} encoding", cidFontName,
                                                         cmap));
                }
                cidFontType = CID_FONT_TYPE_0;
            }
            longTag = new LinkedDictionary <int, int[]>();
            subset  = false;
        }
예제 #14
0
        public static void Main(string[] args)
        {
            // string path = @"C:\Users\Intern03.ELIKOSOFT\Desktop\MyProjects\IText7\TextExamples\demo.pdf";
            // string dirPath =@"C:\Users\Intern03.ELIKOSOFT\Desktop\MyProjects\IText7\TextExamples";
            //
            //
            //
            // if(File.Exists(path))
            //     File.Delete(path);
            //
            //
            // var directory= Directory.GetFiles(dirPath);
            // foreach (var file in directory)
            // {
            //     File.Delete(file);
            // }

            // Must have write permissions to the path folder
            PdfWriter   writer = new PdfWriter(@"C:\Users\Intern03.ELIKOSOFT\Desktop\MyProjects\IText7\TextExamples\demo.pdf");
            PdfDocument pdf    = new PdfDocument(writer);

            Document document = new Document(pdf, PageSize.A4, true);


            //var font = PdfFontFactory.CreateFont(FontConstants.TIMES_ROMAN);

            #region Creating Cyrillic Font
            //the path to the physical font file TTF on the drive
            string fontPath =
                $@"C:\Users\Intern03.ELIKOSOFT\Desktop\MyProjects\IText7\TextExamples\MAC_C_Times\MCTIME.TTF";

            FontProgram fontProgram = FontProgramFactory.CreateFont(fontPath);
            PdfFont     font        = PdfFontFactory.CreateFont(
                fontProgram, PdfEncodings.WINANSI, true);

            #endregion

            // var header = new Paragraph("Edine~ni Merki").SetTextAlignment(TextAlignment.CENTER)
            //     .SetFontSize(20).SetFont(font);

            var header = new Paragraph("Magacini").SetTextAlignment(TextAlignment.CENTER)
                         .SetFontSize(20).SetFont(font);

            Image img = new Image(ImageDataFactory
                                  .Create(@"C:\Users\Intern03.ELIKOSOFT\Desktop\MyProjects\IText7\logo.jpg"))
                        .SetTextAlignment(TextAlignment.CENTER);
            img.Scale(4, 4);
            document.Add(img);


            document.Add(header);
            // Table
            Table table = new Table(2, false);

            table.SetWidth(400).SetHorizontalAlignment(HorizontalAlignment.CENTER);


            Cell cell11 = new Cell(1, 1)
                          .SetBackgroundColor(ColorConstants.GRAY)
                          .SetTextAlignment(TextAlignment.CENTER)
                          .Add(new Paragraph("Broj na magacin")).SetFont(font);
            Cell cell12 = new Cell(1, 1)
                          .SetBackgroundColor(ColorConstants.GRAY)
                          .SetTextAlignment(TextAlignment.CENTER)
                          .Add(new Paragraph("Naziv na magacin")).SetFont(font);

            ////Uncomment for Measure Units Example
            // Cell cell11 = new Cell(1, 1)
            //     .SetBackgroundColor(ColorConstants.YELLOW)
            //     .SetTextAlignment(TextAlignment.CENTER)
            //     .Add(new Paragraph("Edine~na merka")).SetFont(font);
            // Cell cell12 = new Cell(1, 1)
            //     .SetBackgroundColor(ColorConstants.YELLOW)
            //     .SetTextAlignment(TextAlignment.CENTER)
            //     .Add(new Paragraph("Naziv na edine~na merka")).SetFont(font);



            Cell cell21 = new Cell(1, 1)
                          .SetTextAlignment(TextAlignment.CENTER)
                          .Add(new Paragraph("New York"));



            Cell cell22 = new Cell(1, 1)
                          .SetTextAlignment(TextAlignment.CENTER)
                          .Add(new Paragraph("Albany"));

            Cell cell31 = new Cell(1, 1)
                          .SetTextAlignment(TextAlignment.CENTER)
                          .Add(new Paragraph("New Jersey"));
            Cell cell32 = new Cell(1, 1)
                          .SetTextAlignment(TextAlignment.CENTER)
                          .Add(new Paragraph("Trenton"));

            Cell cell41 = new Cell(1, 1)
                          .SetTextAlignment(TextAlignment.CENTER)
                          .Add(new Paragraph("California"));
            Cell cell42 = new Cell(1, 1)
                          .SetTextAlignment(TextAlignment.CENTER)
                          .Add(new Paragraph("Sacramento"));


            table.AddCell(cell11);
            table.AddCell(cell12);
            ////Uncomment for Measure Units Example
            // foreach (KeyValuePair<string, string> item in Entities.SampleCollection)
            // {
            //     Cell cellKey = new Cell(1, 1)
            //         .SetTextAlignment(TextAlignment.CENTER)
            //         .Add(new Paragraph(item.Key)).SetFont(font);
            //
            //     Cell cellValue = new Cell(1, 1)
            //         .SetTextAlignment(TextAlignment.CENTER)
            //         .Add(new Paragraph(item.Value)).SetFont(font);
            //
            //     table.AddCell(cellKey);
            //     table.AddCell(cellValue);
            // }

            foreach (KeyValuePair <string, string> item in Entities.ReadyWarehouses())
            {
                Cell cellKey = new Cell(1, 1)
                               .SetTextAlignment(TextAlignment.CENTER)
                               .Add(new Paragraph(item.Key)).SetFont(font);

                Cell cellValue = new Cell(1, 1)
                                 .SetTextAlignment(TextAlignment.CENTER)
                                 .Add(new Paragraph(item.Value)).SetFont(font);

                table.AddCell(cellKey);
                table.AddCell(cellValue);
            }
            // for (int i = 0; i < Coll.words.Count(); i++)
            // {
            //     Cell cell = new Cell(1, 1)
            //         .SetTextAlignment(TextAlignment.CENTER)
            //         .Add(new Paragraph(Coll.words.ElementAt(i)));
            //
            //     table.AddCell(cell);
            // }
            document.Add(table);
            document.Close();
        }