- Dynamic XFA
- pdfkit5 and Xamarin
- Use multiple licenses
- Replace field with image
- Import FDF into PDF
- Embed TrueType font in PDF
- Determine if a PDF only contains images
- Download and convert image to PDF
- Use TrueType font collections
- Determine the content bounding box
- Highlight fields in PDF
- Add hyperlink to PDF
- Add Stamp to PDF
- How do I extract page destinations from bookmarks?
- Convert SVG to PDF
- Extract glyph boxes from PDF
- Fill in a template PDF document
- Extract graphics from PDF
- Flatten Markup Annotation
- Fill and save dynamic XFA form
- Clip PDF page content in C#
- How to scale content of PDF
- Use pdfkit5 with a Xamarin.Forms app
- Create tagged PDF
- Add single-line text to PDF
- Change the formatting of a numeric field
- Add bookmarks to PDF
- Convert PDF to plain text
- TIFF to PDF C#
- Add a note to PDF
- Add a link with an internal destination to PDF
- Create formfields in PDF documents
- Extract embedded files from PDF
- Remove graphics from PDF
- Fit image to PDF page
- Split PDF pages in C# and VB.NET
- Add tags to existing PDF
- pdfkit5 - detailed changes to the API - Tall Components
- Add a link to PDF with an external destination
- Translate PDF page content
- How to sign and verify updates to a PDF document
- Create a new digitally signed PDF document
- EMF to PDF as vector image
- Layout text with MultilineTextShape
- Add multiline text to a PDF document
- .NET Core console app on MacOS
- How to add autosized text to PDF
- How to generate and export certificates
- Write Document to HttpResponse
- Create rectangles with rounded corners
- Add barcodes to PDF
- Append two or more existing PDF files
- pdfkit5 and .NET Core
- Create a text annotation in PDF with rich text
- Rotate a PDF page
- Add text field to PDF
- Change the color inside a PDF
- How to embed files in a PDF document
- Merge XDP data with dynamic XFA form
- How to mirror PDF pages and other shapes
- Add a Diagonal Watermark to PDF in C# - TallComponents - PDF Library
- Read PDF tags
- How to reduce PDF file size
- Tagged PDF
- Read and write meta data from PDF
- Add footer to PDF
- Create a custom signature handler to sign and verify PDF documents
- Export FDF from PDF form
- Create text with decorations
- Fill XFA form and export XDP data
- Vector graphics in PDF
- Resize PDF pages
- Change page orientation PDF
- Reduce PDF size
- Crop content on a PDF page
- Extract glyphs and sort by reading order
- PDF Viewer Preferences
- Extract images from PDF
- Disable submit button after submitting
- How to downscale all images in a PDF
- Remove PDF security settings
- Merge PDF files in C# .NET
- Flatten PDF form
- Licensing and .NET Standard
- How to downscale all images in a PDF
- Fill PDF form
- pdfkit5 .NET Standard API
- Add a rubber stamp annotation with a custom icon
- Add Long Term Validation (LTV) data to an existing signature
- How to create a tiling for shapes in PDF
- Search text in PDF
- Digitally sign a PDF form in C# or VB.NET
- Add simple html text to PDF
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