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); }
private static void AddSection(PdfDocument pdfDoc, Paragraph paragraph, int pageNumber, int sectionNumber) { Document doc = new Document(pdfDoc); ParagraphRenderer renderer = (ParagraphRenderer)paragraph.CreateRendererSubTree(); renderer.SetParent(new DocumentRenderer(doc)); float pageHeight = pdfDoc.GetDefaultPageSize().GetHeight(); float pageWidth = pdfDoc.GetDefaultPageSize().GetWidth(); Rectangle textSectionRectangle = new Rectangle( doc.GetLeftMargin(), doc.GetBottomMargin() + ((pageHeight - doc.GetTopMargin() - doc.GetBottomMargin()) / 3) * sectionNumber, pageWidth - doc.GetLeftMargin() - doc.GetRightMargin(), (pageHeight - doc.GetTopMargin() - doc.GetBottomMargin()) / 3); // Simulate the positioning of the renderer to find out how much space the text section will occupy. LayoutResult layoutResult = renderer .Layout(new LayoutContext(new LayoutArea(pageNumber, textSectionRectangle))); /* Fill the current page section with the content. * If the content isn't fully placed in the current page section, * it will be split and drawn in the next page section. */ while (layoutResult.GetStatus() != LayoutResult.FULL) { if (pdfDoc.GetNumberOfPages() < pageNumber) { pdfDoc.AddNewPage(); } pageNumber++; layoutResult.GetSplitRenderer().Draw(new DrawContext(pdfDoc, new PdfCanvas(pdfDoc.GetPage(pageNumber - 1)), false)); renderer = (ParagraphRenderer)layoutResult.GetOverflowRenderer(); layoutResult = renderer .Layout(new LayoutContext(new LayoutArea(pageNumber, textSectionRectangle))); } if (pdfDoc.GetNumberOfPages() < pageNumber) { pdfDoc.AddNewPage(); } renderer.Draw(new DrawContext(pdfDoc, new PdfCanvas(pdfDoc.GetPage(pageNumber)), false)); }
/// <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); }
protected void ManipulatePdf(string dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); Document doc = new Document(pdfDoc); PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage()); Paragraph p = new Paragraph("This is a long paragraph that doesn't" + "fit the width we defined for the simple column of the" + "ColumnText object, so it will be distributed over several" + "lines (and we don't know in advance how many)."); Rectangle firstRect = new Rectangle(120, 500, 130, 280); new Canvas(canvas, firstRect) .Add(p); canvas.Rectangle(firstRect); canvas.Stroke(); // In the lines below the comment we try to reproduce the iText5 method to achieve the result // However it's much more simple to use the next line // p.SetBorder(new SolidBorder(1)); // Or you can implement your own ParagraphRenderer and change the behaviour of drawBorder(DrawContext) // or draw(DrawContext) Rectangle secRect = new Rectangle(300, 500, 130, 280); ParagraphRenderer renderer = (ParagraphRenderer)p.CreateRendererSubTree().SetParent(doc.GetRenderer()); float height = renderer.Layout(new LayoutContext(new LayoutArea(0, secRect))) .GetOccupiedArea().GetBBox().GetHeight(); new Canvas(canvas, secRect) .Add(p); canvas.Rectangle(secRect.GetX(), secRect.GetY() + secRect.GetHeight() - height, secRect.GetWidth(), height); canvas.Stroke(); doc.Close(); }
public static void ProduceOrphansAndWidowsTestCase(String outPdf, Paragraph testPara) { Document doc = new Document(new PdfDocument(new PdfWriter(outPdf))); PageSize pageSize = new PageSize(PageSize.A4.GetWidth(), PageSize.A5.GetHeight()); doc.GetPdfDocument().SetDefaultPageSize(pageSize); Rectangle[] columns = InitUniformColumns(pageSize, 2); doc.SetRenderer(new ColumnDocumentRenderer(doc, columns)); String paraText = "A one line string\n"; testPara.SetMargin(0).SetBackgroundColor(new DeviceRgb(232, 232, 232)); testPara.Add(paraText); float linesHeight = CalculateHeightForLinesNum(doc, testPara, columns[1].GetWidth(), 1, true); float adjustmentHeight = columns[0].GetHeight() - linesHeight - LINES_SPACE_EPS; String description = "Test orphans and widows case at once. This block height" + " is adjusted in such way that both orphans and widows cases occur.\n " + "The following paragraph contains as many fitting in one line text strings as needed" + " to reproduce the case with both orphans and widows\n" + "Reference example without orphans and widows" + " control can be found on the next page"; doc.Add(new Paragraph(description).SetMargin(0).SetBorder(new SolidBorder(1)).SetHeight(adjustmentHeight)); Paragraph tempPara = new Paragraph().SetMargin(0); for (int i = 0; i < 50; i++) { tempPara.Add(paraText); } ParagraphRenderer renderer = (ParagraphRenderer)tempPara.CreateRendererSubTree().SetParent(doc.GetRenderer ()); LayoutResult layoutRes = renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(columns[1].GetWidth (), columns[1].GetHeight())))); int numberOfLines = ((ParagraphRenderer)layoutRes.GetSplitRenderer()).GetLines().Count; for (int i = 0; i <= numberOfLines; i++) { testPara.Add(paraText); } doc.Add(testPara); doc.Add(new AreaBreak(AreaBreakType.NEXT_PAGE)); doc.Add(new Paragraph("Reference example without orphans and widows control.").SetMargin(0).SetBorder(new SolidBorder(1)).SetHeight(adjustmentHeight)); Paragraph paragraph = new Paragraph(); for (int i = 0; i <= numberOfLines + 1; i++) { paragraph.Add(paraText); } paragraph.SetMargin(0).SetBackgroundColor(new DeviceRgb(232, 232, 232)); doc.Add(paragraph); doc.Add(new Paragraph(paraText).SetMargin(0).SetBackgroundColor(new DeviceRgb(232, 232, 232))); doc.Close(); }
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); } }