←Select platform

Code Property

Summary
Character code value of this PDFObject text item.
Syntax
C#
C++/CLI
Java
Python
public char Code { get; set; } 
public char getCode(); 
public void setCode( 
   char charValue 
); 
public: 
property char Code { 
   char get(); 
   void set (    char ); 
} 
Code # get and set (PDFObject) 

Property Value

A System.Char that contain the UNICODE character code of this PDFObject text item. The default value is 0.

Remarks

The Code property is only valid for text items, i.e., when the value of ObjectType is PDFObjectType.Text. The text properties such as font information and color will be stored in the TextProperties property of this object.

White space characters such as a space or a tab are parsed by default and returned as individual objects. You can stop this behavior by OR'ing the PDFParsePagesOptions.IgnoreWhiteSpaces enumeration member with PDFParsePagesOptions.Objects in the options parameter passed to PDFDocument.ParsePages. Note: To re-construct the words and lines of text in the page without white characters by using the PDFTextProperties.IsEndOfWord and PDFTextProperties.IsEndOfLine properties. The PDFTextPropertiesexample demonstrates this.

Note: Currently, LEADTOOLS does not support extracting true UNICODE characters from a PDF file. Only ASCII characters are read and then converted to UNICODE.

Example
C#
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:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

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

Leadtools.Pdf Assembly

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