Esempio n. 1
0
 /// <summary>Places retrieved text to canvas to a separate layer.</summary>
 /// <param name="imageSize">
 /// size of the image according to the selected
 /// <see cref="ScaleMode"/>
 /// </param>
 /// <param name="pageText">text that was found on this image (or on this page)</param>
 /// <param name="pdfCanvas">canvas to place the text</param>
 /// <param name="multiplier">coefficient to adjust text placing on canvas</param>
 /// <param name="pageMediaBox">page parameters</param>
 private void AddTextToCanvas(Rectangle imageSize, IList <TextInfo> pageText, PdfCanvas pdfCanvas, float multiplier
                              , Rectangle pageMediaBox)
 {
     if (pageText != null && pageText.Count > 0)
     {
         Point imageCoordinates = PdfCreatorUtil.CalculateImageCoordinates(ocrPdfCreatorProperties.GetPageSize(), imageSize
                                                                           );
         foreach (TextInfo item in pageText)
         {
             String        line         = item.GetText();
             IList <float> coordinates  = item.GetBbox();
             float         left         = coordinates[0] * multiplier;
             float         right        = (coordinates[2] + 1) * multiplier - 1;
             float         top          = coordinates[1] * multiplier;
             float         bottom       = (coordinates[3] + 1) * multiplier - 1;
             float         bboxWidthPt  = PdfCreatorUtil.GetPoints(right - left);
             float         bboxHeightPt = PdfCreatorUtil.GetPoints(bottom - top);
             FontProvider  fontProvider = GetOcrPdfCreatorProperties().GetFontProvider();
             String        fontFamily   = GetOcrPdfCreatorProperties().GetDefaultFontFamily();
             if (!String.IsNullOrEmpty(line) && bboxHeightPt > 0 && bboxWidthPt > 0)
             {
                 Document document = new Document(pdfCanvas.GetDocument());
                 document.SetFontProvider(fontProvider);
                 // Scale the text width to fit the OCR bbox
                 float fontSize             = PdfCreatorUtil.CalculateFontSize(document, line, fontFamily, bboxHeightPt, bboxWidthPt);
                 float lineWidth            = PdfCreatorUtil.GetRealLineWidth(document, line, fontFamily, fontSize);
                 float deltaX               = PdfCreatorUtil.GetPoints(left);
                 float deltaY               = imageSize.GetHeight() - PdfCreatorUtil.GetPoints(bottom);
                 iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pageMediaBox);
                 canvas.SetFontProvider(fontProvider);
                 Text      text      = new Text(line).SetHorizontalScaling(bboxWidthPt / lineWidth);
                 Paragraph paragraph = new Paragraph(text).SetMargin(0).SetMultipliedLeading(1.2f);
                 paragraph.SetFontFamily(fontFamily).SetFontSize(fontSize);
                 paragraph.SetWidth(bboxWidthPt * 1.5f);
                 if (ocrPdfCreatorProperties.GetTextColor() != null)
                 {
                     paragraph.SetFontColor(ocrPdfCreatorProperties.GetTextColor());
                 }
                 else
                 {
                     paragraph.SetTextRenderingMode(PdfCanvasConstants.TextRenderingMode.INVISIBLE);
                 }
                 canvas.ShowTextAligned(paragraph, deltaX + (float)imageCoordinates.x, deltaY + (float)imageCoordinates.y,
                                        TextAlignment.LEFT);
                 canvas.Close();
             }
         }
     }
 }
Esempio n. 2
0
 /// <summary>Places given image to canvas to background to a separate layer.</summary>
 /// <param name="imageData">
 /// input image as
 /// <see cref="System.IO.FileInfo"/>
 /// </param>
 /// <param name="imageSize">
 /// size of the image according to the selected
 /// <see cref="ScaleMode"/>
 /// </param>
 /// <param name="pdfCanvas">canvas to place the image</param>
 private void AddImageToCanvas(ImageData imageData, Rectangle imageSize, PdfCanvas pdfCanvas)
 {
     if (imageData != null)
     {
         if (ocrPdfCreatorProperties.GetPageSize() == null)
         {
             pdfCanvas.AddImage(imageData, imageSize, false);
         }
         else
         {
             Point coordinates = PdfCreatorUtil.CalculateImageCoordinates(ocrPdfCreatorProperties.GetPageSize(), imageSize
                                                                          );
             Rectangle rect = new Rectangle((float)coordinates.x, (float)coordinates.y, imageSize.GetWidth(), imageSize
                                            .GetHeight());
             pdfCanvas.AddImage(imageData, rect, false);
         }
     }
 }