←Select platform

PDFObject Structure

Summary

Contains information of a single PDF object.

Syntax

C#
VB
Java
C++
[SerializableAttribute()] 
public struct PDFObject 
<SerializableAttribute()> 
Public Structure PDFObject  
   Inherits System.ValueType 
public class PDFObject 
[SerializableAttribute()] 
public value class PDFObject : public System.ValueType  

Remarks

The PDFObject structure contains information of a single PDF text item (character), rectangle or image. To read objects from a PDF file, you use the PDFDocument.ParsePages method. After this method returns and depending on the value of PDFParsePagesOptions parameter passed to the method, the PDFDocumentPage'spdfdocumentpage.fonts, PDFDocumentPage.Objects, PDFDocumentPage.Annotationsm PDFDocumentPage.FormFields, PDFDocumentPage.Signatures and PDFDocumentPage.Hyperlinks collections will be populated with the items of the PDF page.

Example

This example will read all the objects from a PDF page and then re-draw these objects on an image that closely resembles the original PDF page before saving it to disk. You can use this example as a base for drawing a PDF page as a "vector" drawing.

C#
VB
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.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, 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, PDFDocumentPage page, string text, LeadRect textRect, PDFTextProperties textProperties, double textFontHeight) 
{ 
   // Create the font 
   PDFFont font = page.Fonts[textProperties.FontIndex]; 
   string faceName = font.FaceName; 
   if (string.IsNullOrEmpty(faceName)) 
   { 
      // Could be an embedded font, use Arial 
      faceName = "Arial"; 
   } 
 
   FontStyle fontStyle = FontStyle.Regular; 
 
   if ((font.FontStyle & PDFFontStyle.Bold) == PDFFontStyle.Bold) 
   { 
      fontStyle |= FontStyle.Bold; 
   } 
 
   if ((font.FontStyle & PDFFontStyle.Italic) == PDFFontStyle.Italic) 
   { 
      fontStyle |= FontStyle.Italic; 
   } 
 
   if ((font.FontStyle & PDFFontStyle.Underline) == PDFFontStyle.Underline) 
   { 
      fontStyle |= FontStyle.Underline; 
   } 
 
   using (Font f = new Font(faceName, (float)textFontHeight * 72 / g.DpiY, fontStyle)) 
   { 
      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:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Pdf 
Imports Leadtools.WinForms 
Imports Leadtools.Drawing 
 
Public Sub PDFObjectExample() 
   Dim pdfFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf") 
   Dim pngFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "LEAD_png.png") 
 
   ' Create a PDF document for file at 200 DPI 
   Using document As PDFDocument = New PDFDocument(pdfFileName) 
      document.Resolution = 200 
 
      ' Parse the objects of the first page 
      document.ParsePages(PDFParsePagesOptions.Fonts Or PDFParsePagesOptions.Objects, 1, 1) 
 
      ' Get the page 
      Dim page As PDFDocumentPage = document.Pages(0) 
 
      ' Get the image of the page so we can use it to get the source image objects 
      Using pageImage As RasterImage = document.GetPageImage(Nothing, page.PageNumber) 
         ' Create the bitmap to draw the objects to 
         Using btmp As Bitmap = New Bitmap(page.WidthPixels, page.HeightPixels) 
            btmp.SetResolution(document.Resolution, document.Resolution) 
            Using g As Graphics = Graphics.FromImage(btmp) 
               g.Clear(Color.White) 
 
               ' Render the objects 
 
               ' Text is line at a time 
               Dim textRect As LeadRect = LeadRect.Empty 
               Dim textFontHeight As Double = 0 
               Dim textLine As StringBuilder = New StringBuilder() 
 
               For Each obj As PDFObject In page.Objects 
                  Select Case obj.ObjectType 
                     Case PDFObjectType.Image 
                        RenderImage(g, pageImage, page, obj) 
 
                     Case PDFObjectType.Text 
                        ' Add the text code and rects together 
                        textLine.Append(obj.Code) 
                        Dim rc As PDFRect = page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, obj.Bounds) 
                        Dim objRect As LeadRect = LeadRect.FromLTRB(CInt(rc.Left), CInt(rc.Top), CInt(rc.Right), CInt(rc.Bottom)) 
                        If textRect.IsEmpty Then 
                           textRect = objRect 
                        Else 
                           textRect = LeadRect.Union(textRect, objRect) 
                        End If 
 
                        textFontHeight = Math.Max(textFontHeight, obj.TextProperties.FontHeight) 
 
                        ' If this is the last object in a line, render it 
                        If obj.TextProperties.IsEndOfLine Then 
                           RenderText(g, page, textLine.ToString(), textRect, obj.TextProperties, textFontHeight) 
 
                           textLine = New StringBuilder() 
                           textRect = LeadRect.Empty 
                        End If 
                  End Select 
               Next obj 
            End Using 
 
            btmp.Save(pngFileName, System.Drawing.Imaging.ImageFormat.Png) 
         End Using 
      End Using 
   End Using 
End Sub 
 
Private Shared Sub RenderImage(ByVal g As Graphics, ByVal pageImage As RasterImage, ByVal page As PDFDocumentPage, ByVal obj As PDFObject) 
   Dim destRect As LeadRect = New LeadRect(0, 0, page.WidthPixels, page.HeightPixels) 
 
   ' Get the object coordinates in pixels 
   Dim rc As PDFRect = page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, obj.Bounds) 
   Dim destClipRect As LeadRect = LeadRect.FromLTRB(CInt(rc.Left), CInt(rc.Top), CInt(rc.Right), CInt(rc.Bottom)) 
 
   ' Draw from the page image to the destination graphics 
   Dim props As RasterPaintProperties = RasterPaintProperties.Default 
   props.PaintEngine = RasterPaintEngine.GdiPlus 
   RasterImagePainter.Paint(pageImage, g, LeadRect.Empty, LeadRect.Empty, destRect, destClipRect, props) 
End Sub 
 
Private Shared Sub RenderText(ByVal g As Graphics, ByVal page As PDFDocumentPage, ByVal text As String, ByVal textRect As LeadRect, ByVal textProperties As PDFTextProperties, ByVal textFontHeight As Double) 
   ' Create the font 
   Dim font As PDFFont = page.Fonts(textProperties.FontIndex) 
   Dim faceName As String = font.FaceName 
   If String.IsNullOrEmpty(faceName) Then 
      ' Could be an embedded font, use Arial 
      faceName = "Arial" 
   End If 
 
   Dim fontStyle As FontStyle = fontStyle.Regular 
 
   If (font.FontStyle And PDFFontStyle.Bold) = PDFFontStyle.Bold Then 
      fontStyle = fontStyle Or fontStyle.Bold 
   End If 
 
   If (font.FontStyle And PDFFontStyle.Italic) = PDFFontStyle.Italic Then 
      fontStyle = fontStyle Or fontStyle.Italic 
   End If 
 
   If (font.FontStyle And PDFFontStyle.Underline) = PDFFontStyle.Underline Then 
      fontStyle = fontStyle Or fontStyle.Underline 
   End If 
 
   Using f As Font = New Font(faceName, CSng(textFontHeight) * 72 / g.DpiY, fontStyle) 
      Using brush As Brush = New SolidBrush(RasterColorConverter.ToColor(textProperties.Color)) 
         Dim rect As Rectangle = New Rectangle(textRect.X, textRect.Y, textRect.Width, textRect.Height) 
 
         Using sf As StringFormat = New StringFormat() 
            sf.Alignment = StringAlignment.Center 
            sf.LineAlignment = StringAlignment.Center 
            sf.FormatFlags = sf.FormatFlags Or StringFormatFlags.NoClip Or StringFormatFlags.NoWrap 
 
            g.DrawString(text, f, brush, rect, sf) 
         End Using 
      End Using 
   End Using 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 

Requirements

Target Platforms

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly