PDFRasterizer.NET 4.0 is now available. Supports .NET Core. Renders pixel-perfectly.
- Add Long Term Validation (LTV) data to an existing signature
- How to downscale all images in a PDF
- How to generate and export certificates
- How to downscale all images in a PDF
- Add Stamp to PDF
- How to reduce PDF file size
- Highlight fields in PDF
- Add a note to PDF
- Resize PDF pages
- Extract glyph boxes from PDF
- Use TrueType font collections
- Layout text with MultilineTextShape
- Merge PDF files in C# .NET
- How do I extract page destinations from bookmarks?
- Clip PDF page content in C#
- Fill PDF form
- Extract glyphs and sort by reading order
- Add bookmarks to PDF
- How to scale content of PDF
- Create rectangles with rounded corners
- Create text with decorations
- TIFF to PDF C#
- Crop content on a PDF page
- How to embed files in a PDF document
- Remove graphics from PDF
- Change the color inside a PDF
- Import FDF into PDF
- Flatten PDF form
- Digitally sign a PDF form in C# or VB.NET
- Vector graphics in PDF
- Translate PDF page content
- Extract graphics from PDF
- Determine the content bounding box
- Search text in PDF
- Convert PDF to plain text
- Flatten Markup Annotation
- Add text field to PDF
- Extract embedded files from PDF
- Extract images from PDF
- Add a Diagonal Watermark to PDF in C#
- Fit image to PDF page
- Add simple html text to PDF
- Add multiline text to a PDF document
- Add single-line text to PDF
- Create a new digitally signed PDF document
- PDF Viewer Preferences
- Change page orientation PDF
- Split PDF pages in C# and VB.NET
- Append two or more existing PDF files
- Determine if a PDF only contains images
- Add footer to PDF
- Convert SVG to PDF
- Add hyperlink to PDF
- Rotate a PDF page
- Change the formatting of a numeric field
- How to mirror PDF pages and other shapes
- Fill in a template PDF document
- How to add autosized text to PDF
- Create formfields in PDF documents
- Export FDF from PDF form
- Add a link with an internal destination to PDF
- Remove PDF security settings
- Add a link to PDF with an external destination
- How to sign and verify updates to a PDF document
- Embed TrueType font
- How to create a tiling for shapes in PDF
- EMF to PDF as vector image
- Replace field with image
- Add a rubber stamp annotation with a custom icon
- Create a text annotation in PDF with rich text
- Read and write meta data from PDF
- Create a custom signature handler to sign and verify PDF documents
- Download and convert image to PDF
- Add barcodes to PDF
- Tagged PDF
- PDFKit.NET 5.0 - detailed changes to the API
- Create tagged PDF
- PDFKit.NET 5.0 and .NET Core
- PDFKit.NET 5.0 and Xamarin
- Dynamic XFA
- PDFKit.NET 5.0 .NET Standard API
- .NET Core console app on MacOS
- Add tags to existing PDF
- Read PDF tags
- Merge XDP data with dynamic XFA form
- Fill XFA form and export XDP data
- Fill and save dynamic XFA form
- Use PDFKit.NET 5.0 with a Xamarin.Forms app
- Use multiple licenses
- Licensing and .NET Standard
- Reduce PDF size
- Disable submit button after submitting
- Write Document to HttpResponse
Extract glyph boxes from PDF
This sample demonstrates how to extract glyph boxes.
This sample creates a bitmap for each page and draws boxes for each glyph. It takes into account the orientation of the page, as well as its cropbox and mediabox so that the bitmap resembles the page as shown by a PDF viewer. The main routine here is CreateBoxesBitmap. It takes a page as an argument and returns a Bitmap with drawn boxes.
For the following PDF page:
We get the following result:
C# code sample
static void Main(string[] args)
{
using (FileStream fileIn = new FileStream(@"..\..\..\inputdocuments\R0.pdf", FileMode.Open, FileAccess.Read))
{
//create document
Document document = new Document(fileIn);
foreach (Page page in document.Pages)
{
System.Drawing.Bitmap bitmap = CreateBoxesBitmap(page);
bitmap.Save(@"..\..\out.png", System.Drawing.Imaging.ImageFormat.Png);
}
}
}
private static System.Drawing.Bitmap CreateBoxesBitmap(Page page)
{
// Compute the part of the page that is visible in a viewer.
Rectangle visibleRectangle = GetVisibleRectangle(page);
// determine the size taking the orientation into account
int width = (int)Math.Round(visibleRectangle.Width);
int height = (int)Math.Round(visibleRectangle.Height);
Orientation orientation = page.Orientation;
if (orientation == Orientation.Rotate90 || orientation == Orientation.Rotate270)
{
// swap width and height.
int temp = width;
width = height;
height = temp;
}
// create the resulting bitmap
var bitmap = new System.Drawing.Bitmap(width, height);
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
using (System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red))
{
graphics.Clear(System.Drawing.Color.White);
// retrieve all glyphs on the current page and draw a rectangle for each.
foreach (Glyph glyph in page.Glyphs)
{
// we convert each coordinate into a GDI coordinate
System.Drawing.PointF bottomLeft = PDFPointToGDI(glyph.BottomLeft, visibleRectangle, orientation);
System.Drawing.PointF bottomRight = PDFPointToGDI(glyph.BottomRight, visibleRectangle, orientation);
System.Drawing.PointF topRight = PDFPointToGDI(glyph.TopRight, visibleRectangle, orientation);
System.Drawing.PointF topLeft = PDFPointToGDI(glyph.TopLeft, visibleRectangle, orientation);
System.Drawing.PointF[] points = new[] { bottomLeft, bottomRight, topRight, topLeft };
// draw glyph box
graphics.DrawPolygon(pen, points);
}
}
return bitmap;
}
Private Sub Main(args As String())
Using fileIn As New FileStream("..\..\..\inputdocuments\R0.pdf", FileMode.Open, FileAccess.Read)
'create document
Dim document As New Document(fileIn)
For Each page As Page In document.Pages
Dim bitmap As System.Drawing.Bitmap = CreateBoxesBitmap(page)
bitmap.Save("..\..\out.png", System.Drawing.Imaging.ImageFormat.Png)
Next
End Using
End Sub
Private Function CreateBoxesBitmap(page As Page) As System.Drawing.Bitmap
' Compute the part of the page that is visible in a viewer.
Dim visibleRectangle As Rectangle = GetVisibleRectangle(page)
' determine the size taking the orientation into account
Dim width As Integer = CInt(Math.Round(visibleRectangle.Width))
Dim height As Integer = CInt(Math.Round(visibleRectangle.Height))
Dim orientation__1 As Orientation = page.Orientation
If orientation__1 = Orientation.Rotate90 OrElse orientation__1 = Orientation.Rotate270 Then
' swap width and height.
Dim temp As Integer = width
width = height
height = temp
End If
' create the resulting bitmap
Dim bitmap = New System.Drawing.Bitmap(width, height)
Using graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)
Using pen As New System.Drawing.Pen(System.Drawing.Color.Red)
graphics.Clear(System.Drawing.Color.White)
' retrieve all glyphs on the current page and draw a rectangle for each.
For Each glyph As Glyph In page.Glyphs
' we convert each coordinate into a GDI coordinate
Dim bottomLeft As System.Drawing.PointF = PDFPointToGDI(glyph.BottomLeft, visibleRectangle, orientation__1)
Dim bottomRight As System.Drawing.PointF = PDFPointToGDI(glyph.BottomRight, visibleRectangle, orientation__1)
Dim topRight As System.Drawing.PointF = PDFPointToGDI(glyph.TopRight, visibleRectangle, orientation__1)
Dim topLeft As System.Drawing.PointF = PDFPointToGDI(glyph.TopLeft, visibleRectangle, orientation__1)
Dim points As System.Drawing.PointF() = {bottomLeft, bottomRight, topRight, topLeft}
' draw glyph box
graphics.DrawPolygon(pen, points)
Next
End Using
End Using
Return bitmap
End Function
Note that we need to convert each coordinate into a GDI coordinate, as PDF has its origin at the bottom left of the page, and the page may be rotated as well. Below is the code of the PdfPointToGdi routine.
C# code sample
static Rectangle GetVisibleRectangle(Page page)
{
Rectangle rectangle = new Rectangle(0, 0, page.Width, page.Height);
Rectangle mediaBox = page.MediaBox;
if (mediaBox != null)
{
rectangle = Intersection(rectangle, mediaBox);
}
Rectangle cropBox = page.CropBox;
if (null != cropBox)
{
rectangle = Intersection(rectangle, cropBox);
}
return rectangle;
}
static System.Drawing.PointF PDFPointToGDI(System.Drawing.PointF point, Rectangle rectangle, Orientation orientation)
{
// Adjust for origin of the visible rectangle, which may not be at (0,0).
double x = point.X - rectangle.Left;
double y = point.Y - rectangle.Bottom;
switch (orientation)
{
case Orientation.Rotate0:
// just 'flip' the coordinate over the y axis.
return new System.Drawing.PointF((float)x, (float)(rectangle.Height - y));
case Orientation.Rotate90:
// exchange x and y, and perform appropiate flipping.
return new System.Drawing.PointF((float)(rectangle.Height - y), (float)(rectangle.Width - x));
case Orientation.Rotate180:
// Pointwise mirror of Rotate0.
return new System.Drawing.PointF((float)(rectangle.Width - x), (float)y);
case Orientation.Rotate270:
// Pointwise mirror of Rotate90.
return new System.Drawing.PointF((float)y, (float)x);
default:
return point;
}
}
static Rectangle Intersection(Rectangle rect1, Rectangle rect2)
{
double minX = Math.Max(rect1.Left, rect2.Left); // maximum of left sides.
double maxX = Math.Min(rect1.Left + rect1.Width, rect2.Left + rect2.Width); // minimum of right sides.
double minY = Math.Max(rect1.Bottom, rect2.Bottom); // maximum of bottom sides.
double maxY = Math.Min(rect1.Bottom + rect1.Height, rect2.Bottom + rect2.Height); // minimum of bottom sides.
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
}
Private Function GetVisibleRectangle(page As Page) As Rectangle
Dim rectangle As New Rectangle(0, 0, page.Width, page.Height)
Dim mediaBox As Rectangle = page.MediaBox
If mediaBox IsNot Nothing Then
rectangle = Intersection(rectangle, mediaBox)
End If
Dim cropBox As Rectangle = page.CropBox
If cropBox IsNot Nothing Then
rectangle = Intersection(rectangle, cropBox)
End If
Return rectangle
End Function
Private Function PDFPointToGDI(point As System.Drawing.PointF, rectangle As Rectangle, orientation__1 As Orientation) As System.Drawing.PointF
' Adjust for origin of the visible rectangle, which may not be at (0,0).
Dim x As Double = point.X - rectangle.Left
Dim y As Double = point.Y - rectangle.Bottom
Select Case orientation__1
Case Orientation.Rotate0
' just 'flip' the coordinate over the y axis.
Return New System.Drawing.PointF(CSng(x), CSng(rectangle.Height - y))
Case Orientation.Rotate90
' exchange x and y, and perform appropiate flipping.
Return New System.Drawing.PointF(CSng(rectangle.Height - y), CSng(rectangle.Width - x))
Case Orientation.Rotate180
' Pointwise mirror of Rotate0.
Return New System.Drawing.PointF(CSng(rectangle.Width - x), CSng(y))
Case Orientation.Rotate270
' Pointwise mirror of Rotate90.
Return New System.Drawing.PointF(CSng(y), CSng(x))
Case Else
Return point
End Select
End Function
Private Function Intersection(rect1 As Rectangle, rect2 As Rectangle) As Rectangle
Dim minX As Double = Math.Max(rect1.Left, rect2.Left)
' maximum of left sides.
Dim maxX As Double = Math.Min(rect1.Left + rect1.Width, rect2.Left + rect2.Width)
' minimum of right sides.
Dim minY As Double = Math.Max(rect1.Bottom, rect2.Bottom)
' maximum of bottom sides.
Dim maxY As Double = Math.Min(rect1.Bottom + rect1.Height, rect2.Bottom + rect2.Height)
' minimum of bottom sides.
Return New Rectangle(minX, minY, maxX - minX, maxY - minY)
End Function

Download PDFKit.NET 5.0
We will send you a download link
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download
We have sent an email with a download link.