Exemplo n.º 1
0
        /** Hyphenates a word and returns the first part of it. To get
         * the second part of the hyphenated word call <CODE>getHyphenatedWordPost()</CODE>.
         * @param word the word to hyphenate
         * @param font the font used by this word
         * @param fontSize the font size used by this word
         * @param remainingWidth the width available to fit this word in
         * @return the first part of the hyphenated word including
         * the hyphen symbol, if any
         */
        virtual public string GetHyphenatedWordPre(string word, BaseFont font, float fontSize, float remainingWidth)
        {
            post = word;
            string hyphen      = this.HyphenSymbol;
            float  hyphenWidth = font.GetWidthPoint(hyphen, fontSize);

            if (hyphenWidth > remainingWidth)
            {
                return("");
            }
            Hyphenation hyphenation = hyphenator.Hyphenate(word);

            if (hyphenation == null)
            {
                return("");
            }
            int len = hyphenation.Length;
            int k;

            for (k = 0; k < len; ++k)
            {
                if (font.GetWidthPoint(hyphenation.GetPreHyphenText(k), fontSize) + hyphenWidth > remainingWidth)
                {
                    break;
                }
            }
            --k;
            if (k < 0)
            {
                return("");
            }
            post = hyphenation.GetPostHyphenText(k);
            return(hyphenation.GetPreHyphenText(k) + hyphen);
        }
Exemplo n.º 2
0
        private void ManipulatePdf(string dest)
        {
            // See hyphenation example of specified word in console
            // For the correct run of sample, please, add an itext.hyph dependency,
            // which could be found on the following web-page: https://mvnrepository.com/artifact/com.itextpdf/hyph
            Hyphenation s = Hyphenator.Hyphenate("de", "DE", "Leistungsscheinziffer", 2, 2);

            Console.Out.WriteLine(s);

            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            doc.SetMargins(0, 0, 0, 0);

            Table table = new Table(UnitValue.CreatePercentArray(1)).SetFixedLayout();

            table.SetWidth(UnitValue.CreatePercentValue(10));

            Text text = new Text("Leistungsscheinziffer");

            text.SetHyphenation(new HyphenationConfig("de", "DE", 2, 2));
            table.AddCell(new Cell().Add(new Paragraph(text)));

            Paragraph paragraph = new Paragraph();

            paragraph.SetHyphenation(new HyphenationConfig("de", "DE", 2, 2));
            paragraph.Add("Leistungsscheinziffer");
            table.AddCell(new Cell().Add(paragraph));

            // soft hyphens
            table.AddCell(new Cell().Add(new Paragraph("Le\u00adistun\u00ADgssch\u00ADeinziffe\u00ADr").SetHyphenation
                                             (new HyphenationConfig(3, 2))));

            doc.Add(table);

            doc.Close();
        }