←Select platform

FontHeight Property

Summary
Font height in PDF units.
Syntax
C#
C++/CLI
Java
Python
public double FontHeight { get; set; } 
public double getFontHeight(); 
public void setFontHeight( 
   double doubleValue 
); 
public: 
property double FontHeight { 
   double get(); 
   void set (    double ); 
} 
FontHeight # get and set (PDFTextProperties) 

Property Value

A System.Double that represents the font height in PDF units (1/72 of an inch and bottom left). The default value is 0.

Remarks

This value contains the height of this PDF text object in PDF units (1/72 of an inch and bottom left). You can use the PDFDocumentPage.ConvertPoint method to convert the value to a pixels or inches and then to points if required. Here is an example:

// Get the font height in PDF units, which 1/72 of an inch, i.e. in points 
double fontHeightPoints = pdfObject.TextProperties.FontHeight; 
// Convert to inches:  
double fontHeightInches1 = fontHeightPoints / 72.0; 
// Or use PDFDocumentPage.ConvertPoint to convert to inches: 
PDFPoint pt = new PDFPoint(0, fontHeightPoints); 
double fontHeightInches2 = pdfDocumentPage.ConvertPoint(PDFCoordinateType.Pdf, PDFCoordinateType.Inch, pt).Y; 
// And pixels: 
double fontHeightPixels = pdfDocumentPage.ConvertPoint(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, pt).Y; 

This value contains the external and internal leading of the text item, to get the bounds of the box that exactly surrounds the text object, use PDFObject.Bounds.

For more information, refer to PDF Coordinate System.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Pdf; 
using Leadtools.WinForms; 
using Leadtools.Drawing; 
 
 
public void PDFObjectExample() 
{ 
   string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, @"Leadtools.pdf"); 
   string pngFileName = Path.Combine(LEAD_VARS.ImagesDir, @"LEAD_png.png"); 
 
   // Create a PDF document for file at 200 DPI 
   using (PDFDocument document = new PDFDocument(pdfFileName)) 
   { 
      document.Resolution = 200; 
 
      // Parse the objects of the first page 
      document.ParseDocumentStructure(PDFParseDocumentStructureOptions.Fonts); 
      document.ParsePages(PDFParsePagesOptions.Fonts | PDFParsePagesOptions.Objects, 1, 1); 
 
      // Get the page 
      PDFDocumentPage page = document.Pages[0]; 
 
      // Get the image of the page so we can use it to get the source image objects 
      using (RasterImage pageImage = document.GetPageImage(null, page.PageNumber)) 
      { 
         // Create the bitmap to draw the objects to 
         using (Bitmap btmp = new Bitmap(page.WidthPixels, page.HeightPixels)) 
         { 
            btmp.SetResolution(document.Resolution, document.Resolution); 
            using (Graphics g = Graphics.FromImage(btmp)) 
            { 
               g.Clear(Color.White); 
 
               // Render the objects 
 
               // Text is line at a time 
               LeadRect textRect = LeadRect.Empty; 
               double textFontHeight = 0; 
               StringBuilder textLine = new StringBuilder(); 
 
               foreach (PDFObject obj in page.Objects) 
               { 
                  switch (obj.ObjectType) 
                  { 
                     case PDFObjectType.Image: 
                        RenderImage(g, pageImage, page, obj); 
                        break; 
 
                     case PDFObjectType.Text: 
                        // Add the text code and rects together 
                        textLine.Append(obj.Code); 
                        PDFRect rc = page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, obj.Bounds); 
                        LeadRect objRect = LeadRect.FromLTRB((int)rc.Left, (int)rc.Top, (int)rc.Right, (int)rc.Bottom); 
                        if (textRect.IsEmpty) 
                        { 
                           textRect = objRect; 
                        } 
                        else 
                        { 
                           textRect = LeadRect.Union(textRect, objRect); 
                        } 
 
                        textFontHeight = Math.Max(textFontHeight, obj.TextProperties.FontHeight); 
 
                        // If this is the last object in a line, render it 
                        if (obj.TextProperties.IsEndOfLine) 
                        { 
                           RenderText(g, document, page, textLine.ToString(), textRect, obj.TextProperties, textFontHeight); 
 
                           textLine = new StringBuilder(); 
                           textRect = LeadRect.Empty; 
                        } 
                        break; 
                  } 
               } 
            } 
 
            btmp.Save(pngFileName, System.Drawing.Imaging.ImageFormat.Png); 
         } 
      } 
   } 
} 
 
private static void RenderImage(Graphics g, RasterImage pageImage, PDFDocumentPage page, PDFObject obj) 
{ 
   LeadRect destRect = new LeadRect(0, 0, page.WidthPixels, page.HeightPixels); 
 
   // Get the object coordinates in pixels 
   PDFRect rc = page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, obj.Bounds); 
   LeadRect destClipRect = LeadRect.FromLTRB((int)rc.Left, (int)rc.Top, (int)rc.Right, (int)rc.Bottom); 
 
   // Draw from the page image to the destination graphics 
   RasterPaintProperties props = RasterPaintProperties.Default; 
   props.PaintEngine = RasterPaintEngine.GdiPlus; 
   RasterImagePainter.Paint( 
      pageImage, 
      g, 
      LeadRect.Empty, 
      LeadRect.Empty, 
      destRect, 
      destClipRect, 
      props); 
} 
 
private static void RenderText(Graphics g, PDFDocument document, PDFDocumentPage page, string text, LeadRect textRect, PDFTextProperties textProperties, double textFontHeight) 
{ 
   // Create the font 
 
   // Find it in the fonts collection 
   string faceName = null; 
 
   if (document.Fonts != null && textProperties.FontIndex < document.Fonts.Count) 
   { 
      PDFFont font = document.Fonts[textProperties.FontIndex]; 
      faceName = font.FaceName; 
   } 
 
   if (string.IsNullOrEmpty(faceName)) 
   { 
      // Could be an embedded font, use Arial 
      faceName = "Arial"; 
   } 
 
   using (Font f = new Font(faceName, (float)textFontHeight * 72 / g.DpiY)) 
   { 
      using (Brush brush = new SolidBrush(RasterColorConverter.ToColor(textProperties.Color))) 
      { 
         Rectangle rect = new Rectangle(textRect.X, textRect.Y, textRect.Width, textRect.Height); 
 
         using (StringFormat sf = new StringFormat()) 
         { 
            sf.Alignment = StringAlignment.Center; 
            sf.LineAlignment = StringAlignment.Center; 
            sf.FormatFlags |= StringFormatFlags.NoClip | StringFormatFlags.NoWrap; 
 
            g.DrawString(text, f, brush, rect, sf); 
         } 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
 
import leadtools.*; 
import leadtools.pdf.*; 
 
 
public void pdfObjectExample() { 
 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String pdfFileName = combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf"); 
 
   // Create a PDF document for file at 200 DPI 
   PDFDocument document = new PDFDocument(pdfFileName); 
   document.setResolution(200); 
 
   // Parse the objects of the first page 
   document.parseDocumentStructure(PDFParseDocumentStructureOptions.FONTS.getValue()); 
   document.parsePages(PDFParsePagesOptions.FONTS.getValue() | PDFParsePagesOptions.OBJECTS.getValue(), 1, 1); 
 
   // Get the page 
   PDFDocumentPage page = document.getPages().get(0); 
 
   // Get the image of the page so we can use it to get the source image objects 
   RasterImage pageImage = document.getPageImage(null, page.getPageNumber()); 
 
   // Create the bitmap to draw the objects to 
   BufferedImage bImg = new BufferedImage(pageImage.getWidth(), pageImage.getHeight(), BufferedImage.TYPE_INT_RGB); 
   Graphics2D g = (Graphics2D) bImg.getGraphics(); 
   g.setBackground(new Color(255, 255, 255)); 
 
   // Text is line at a time 
   LeadRect textRect = LeadRect.getEmpty(); 
   double textFontHeight = 0; 
   StringBuilder textLine = new StringBuilder(); 
 
   for (PDFObject obj : page.getObjects()) { 
 
      switch (obj.getObjectType()) { 
 
         case IMAGE: 
            System.out.println("Image Found"); 
            break; 
 
         case TEXT: 
            // Add the text code and rects together 
            textLine.append(obj.getCode()); 
            PDFRect rc = page.convertRect(PDFCoordinateType.PDF, PDFCoordinateType.PIXEL, obj.getBounds()); 
            LeadRect objRect = LeadRect.fromLTRB((int) rc.getLeft(), (int) rc.getTop(), (int) rc.getRight(), 
                  (int) rc.getBottom()); 
            if (textRect.isEmpty()) { 
               textRect = objRect; 
            } else { 
               textRect = LeadRect.union(textRect, objRect); 
            } 
 
            double max = Math.max(textFontHeight, obj.getTextProperties().getFontHeight()); 
            textFontHeight = max; 
 
            // If this is the last object in a line, render it 
            if (obj.getTextProperties().isEndOfLine()) { 
               renderText(g, document, page, textLine.toString(), textRect, obj.getTextProperties(), textFontHeight); 
               textLine = new StringBuilder(); 
               textRect = LeadRect.getEmpty(); 
            } 
            break; 
         default: 
            break; 
      } 
   } 
   g = null; 
 
   pageImage = null; 
   document = null; 
 
} 
 
private static void renderText(Graphics g, PDFDocument document, PDFDocumentPage page, String text, 
      LeadRect textRect, PDFTextProperties textProperties, double textFontHeight) { 
 
   // Create the font 
 
   // Find it in the fonts collection 
   String faceName = null; 
 
   if (document.getFonts() != null && textProperties.getFontIndex() < document.getFonts().size()) { 
      PDFFont font = document.getFonts().get(textProperties.getFontIndex()); 
      faceName = font.getFaceName(); 
   } 
 
   if (faceName == null || faceName == "") { 
      // Could be an embedded font, use Arial 
      faceName = "Arial"; 
   } 
 
   g.setClip(0, 0, 0, 0); 
   Font f = new Font(faceName, Font.PLAIN, 
         ((int) Math.round(textFontHeight * 72 / (g.getClipBounds()).getHeight()))); 
   g.setFont(f); 
   g.drawString(text, textRect.getX(), textRect.getY()); 
 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.