public static float CalculateHeightForLinesNumKeepTogetherCaseSpecific(Document doc, Paragraph p, float width
                                                                               , float height, float linesNum)
        {
            ParagraphRenderer renderer  = (ParagraphRenderer)p.CreateRendererSubTree().SetParent(doc.GetRenderer());
            LayoutResult      layoutRes = renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(width, 10000)))
                                                          );
            int   allLinesCount = renderer.GetLines().Count;
            float lineHeight    = layoutRes.GetOccupiedArea().GetBBox().GetHeight() / allLinesCount;

            renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(width, height))));
            int linesWithinOnePageCount = renderer.GetLines().Count;

            return((allLinesCount - linesWithinOnePageCount - linesNum) * lineHeight);
        }
Esempio n. 2
0
        /// <summary>Writes a log message reporting that orphans constraint is violated.</summary>
        /// <remarks>
        /// Writes a log message reporting that orphans constraint is violated.
        /// This method is to be overridden if violation scenarios need to be handled in some other way.
        /// </remarks>
        /// <param name="renderer">a renderer processing orphans</param>
        /// <param name="message">
        ///
        /// <see cref="System.String"/>
        /// explaining the reason for violation
        /// </param>
        public virtual void HandleViolatedOrphans(ParagraphRenderer renderer, String message)
        {
            ILog logger = LogManager.GetLogger(typeof(iText.Layout.Properties.ParagraphOrphansControl));

            if (renderer.GetOccupiedArea() != null && renderer.GetLines() != null)
            {
                int    pageNumber = renderer.GetOccupiedArea().GetPageNumber();
                String warnText   = MessageFormatUtil.Format(iText.IO.LogMessageConstant.ORPHANS_CONSTRAINT_VIOLATED, pageNumber
                                                             , minOrphans, renderer.GetLines().Count, message);
                logger.Warn(warnText);
            }
            else
            {
                logger.Warn(iText.IO.LogMessageConstant.PREMATURE_CALL_OF_HANDLE_VIOLATION_METHOD);
            }
        }
Esempio n. 3
0
        /// <summary>Updates the font.</summary>
        /// <param name="renderer">the renderer</param>
        internal virtual void UpdatePdfFont(ParagraphRenderer renderer)
        {
            Object retrievedFont;

            if (renderer != null)
            {
                IList <LineRenderer> lines = renderer.GetLines();
                if (lines != null)
                {
                    foreach (LineRenderer line in lines)
                    {
                        foreach (IRenderer child in line.GetChildRenderers())
                        {
                            retrievedFont = child.GetProperty <Object>(Property.FONT);
                            if (retrievedFont is PdfFont)
                            {
                                font = (PdfFont)retrievedFont;
                                return;
                            }
                        }
                    }
                }
                retrievedFont = renderer.GetProperty <Object>(Property.FONT);
                if (retrievedFont is PdfFont)
                {
                    font = (PdfFont)retrievedFont;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Calculates font size according to given bbox height, width and selected
        /// font.
        /// </summary>
        /// <param name="document">
        /// PDF document as a
        /// <see cref="iText.Layout.Document"/>
        /// object
        /// </param>
        /// <param name="line">text line</param>
        /// <param name="fontFamily">default font family</param>
        /// <param name="bboxHeightPt">height of bbox calculated by OCR Reader</param>
        /// <param name="bboxWidthPt">width of bbox calculated by OCR Reader</param>
        /// <returns>font size</returns>
        internal static float CalculateFontSize(Document document, String line, String fontFamily, float bboxHeightPt
                                                , float bboxWidthPt)
        {
            Rectangle bbox = new Rectangle(bboxWidthPt * 1.5f, bboxHeightPt * 1.5f);
            // setting minimum and maximum (approx.) values for font size
            float fontSize    = 1;
            float maxFontSize = bbox.GetHeight();

            try {
                Paragraph paragraph = new Paragraph(line);
                paragraph.SetWidth(bbox.GetWidth());
                paragraph.SetFontFamily(fontFamily);
                while (Math.Abs(fontSize - maxFontSize) > 1e-1)
                {
                    float curFontSize = (fontSize + maxFontSize) / 2;
                    paragraph.SetFontSize(curFontSize);
                    ParagraphRenderer renderer = (ParagraphRenderer)paragraph.CreateRendererSubTree().SetParent(document.GetRenderer
                                                                                                                    ());
                    LayoutContext context = new LayoutContext(new LayoutArea(1, bbox));
                    if (renderer.Layout(context).GetStatus() == LayoutResult.FULL && renderer.GetLines().Count == 1)
                    {
                        fontSize = curFontSize;
                    }
                    else
                    {
                        maxFontSize = curFontSize;
                    }
                }
            }
            catch (InvalidOperationException e) {
                LOGGER.Error(PdfOcrLogMessageConstant.PROVIDED_FONT_PROVIDER_IS_INVALID);
                throw new OcrException(OcrException.CANNOT_RESOLVE_PROVIDED_FONTS, e);
            }
            return(fontSize);
        }
        public static float CalculateHeightForLinesNum(Document doc, Paragraph p, float width, float linesNum, bool
                                                       orphans)
        {
            ParagraphRenderer renderer  = (ParagraphRenderer)p.CreateRendererSubTree().SetParent(doc.GetRenderer());
            LayoutResult      layoutRes = renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(width, 100000))
                                                                            ));
            float lineHeight = layoutRes.GetOccupiedArea().GetBBox().GetHeight() / renderer.GetLines().Count;
            float height     = lineHeight * linesNum;

            if (orphans)
            {
                return(height);
            }
            else
            {
                return(layoutRes.GetOccupiedArea().GetBBox().GetHeight() - height);
            }
        }