Contains information of a single PDF object.
[SerializableAttribute()]public struct PDFObject
<SerializableAttribute()>Public Structure PDFObjectInherits System.ValueType
public class PDFObject [SerializableAttribute()]public value class PDFObject : public System.ValueType
The PDFObject structure contains information of a single PDF text item (character), rectangle or image. To read objects from a PDF file, use the PDFDocument.ParsePages method. After this method returns and depending on the value of PDFParsePagesOptions parameter passed to the method, the PDFDocumentPage.Objects, PDFDocumentPage.Annotations, PDFDocumentPage.FormFields, PDFDocumentPage.Signatures and PDFDocumentPage.Hyperlinks collections will be populated with the items of the PDF page.
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.
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 DPIusing (PDFDocument document = new PDFDocument(pdfFileName)){document.Resolution = 200;// Parse the objects of the first pagedocument.ParseDocumentStructure(PDFParseDocumentStructureOptions.Fonts);document.ParsePages(PDFParsePagesOptions.Fonts | PDFParsePagesOptions.Objects, 1, 1);// Get the pagePDFDocumentPage page = document.Pages[0];// Get the image of the page so we can use it to get the source image objectsusing (RasterImage pageImage = document.GetPageImage(null, page.PageNumber)){// Create the bitmap to draw the objects tousing (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 timeLeadRect 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 togethertextLine.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 itif (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 pixelsPDFRect 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 graphicsRasterPaintProperties 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 collectionstring 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 ArialfaceName = "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:\Users\Public\Documents\LEADTOOLS Images";}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.PdfImports Leadtools.WinFormsImports Leadtools.DrawingPublic 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 DPIUsing document As PDFDocument = New PDFDocument(pdfFileName)document.Resolution = 200' Parse the objects of the first pagedocument.ParseDocumentStructure(PDFParseDocumentStructureOptions.Fonts)document.ParsePages(PDFParsePagesOptions.Fonts Or PDFParsePagesOptions.Objects, 1, 1)' Get the pageDim page As PDFDocumentPage = document.Pages(0)' Get the image of the page so we can use it to get the source image objectsUsing pageImage As RasterImage = document.GetPageImage(Nothing, page.PageNumber)' Create the bitmap to draw the objects toUsing 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 timeDim textRect As LeadRect = LeadRect.EmptyDim textFontHeight As Double = 0Dim textLine As StringBuilder = New StringBuilder()For Each obj As PDFObject In page.ObjectsSelect Case obj.ObjectTypeCase PDFObjectType.ImageRenderImage(g, pageImage, page, obj)Case PDFObjectType.Text' Add the text code and rects togethertextLine.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 ThentextRect = objRectElsetextRect = LeadRect.Union(textRect, objRect)End IftextFontHeight = Math.Max(textFontHeight, obj.TextProperties.FontHeight)' If this is the last object in a line, render itIf obj.TextProperties.IsEndOfLine ThenRenderText(g, document, page, textLine.ToString(), textRect, obj.TextProperties, textFontHeight)textLine = New StringBuilder()textRect = LeadRect.EmptyEnd IfEnd SelectNext objEnd Usingbtmp.Save(pngFileName, System.Drawing.Imaging.ImageFormat.Png)End UsingEnd UsingEnd UsingEnd SubPrivate 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 pixelsDim 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 graphicsDim props As RasterPaintProperties = RasterPaintProperties.Defaultprops.PaintEngine = RasterPaintEngine.GdiPlusRasterImagePainter.Paint(pageImage, g, LeadRect.Empty, LeadRect.Empty, destRect, destClipRect, props)End SubPrivate Shared Sub RenderText(ByVal g As Graphics, ByVal document As PDFDocument, ByVal page As PDFDocumentPage, ByVal text As String, ByVal textRect As LeadRect, ByVal textProperties As PDFTextProperties, ByVal textFontHeight As Double)' Create the font' Find it in the fonts collectionDim faceName As String = "Arial"If Not IsNothing(document.Fonts) AndAlso textProperties.FontIndex < document.Fonts.Count ThenDim font As PDFFont = document.Fonts(textProperties.FontIndex)faceName = font.FaceNameEnd IfIf String.IsNullOrEmpty(faceName) Then' Could be an embedded font, use ArialfaceName = "Arial"End IfUsing f As Font = New Font(faceName, CSng(textFontHeight) * 72 / g.DpiY)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.Centersf.LineAlignment = StringAlignment.Centersf.FormatFlags = sf.FormatFlags Or StringFormatFlags.NoClip Or StringFormatFlags.NoWrapg.DrawString(text, f, brush, rect, sf)End UsingEnd UsingEnd UsingEnd SubPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"End Class
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
