PDF: Drawing graphics on PDF page
In This Topic
PdfGraphics class, similar to System.Drawing.Graphics class, is intended for drawing graphics on PDF page, annotation or form.
PdfGraphics class provides the ability to draw graphic primitives (line, rectangle, image, text) on PDF page, annotation or form.
Also
PdfGraphics class provides the ability to draw PDF page, annotation or form on another PDF page, annotation or form.
The process of drawing using
PdfGraphics class consists form the following steps:
Drawing using graphic primitives might take long time, if it is needed to draw lots of graphics.
Because of that the SDK includes an hierarchy of graphic figures, which allows significantly simplify and speed up the drawing process.
IMPORTANT! The coordinates and sizes, which are used while drawing graphics on PDF page, are specified in measurement units
and coordinate system of PDF page. For detailed information about measurement units and coordinate system
of PDF page goto
here.
Draw graphic primitives on PDF page, annotation or form
PdfGraphics class can draw the following primitives:
For drawing a line is used a pen -
PdfPen class. For filling a surface is used a brush -
PdfBrush class.
For defining a line cap is used
PdfLineCap class.
The SDK contains PDF Drawing Demo application, which demonstrates how to perform drawing on PDF page using graphic primitives.
Source codes of the PDF Drawing Demo application are available in C# and VB.NET.
Here is an example that demonstrates how to draw some graphic primitives on PDF page:
public static void TestPDFGraphics(string pdfFileName)
{
// create new PDF document
Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument();
// add new page
document.Pages.Add(new System.Drawing.SizeF(1000, 1000));
// get graphics object associated with page
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics = document.Pages[0].GetGraphics())
{
// draw primitives
Vintasoft.Imaging.Pdf.Drawing.PdfPen pen =
new Vintasoft.Imaging.Pdf.Drawing.PdfPen(System.Drawing.Color.Red);
pen.Width = 3;
Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush =
new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Green);
graphics.DrawLine(pen, 0, 0, 50, 150);
graphics.DrawCurve(pen, 50, 150, 100, 200, 200, 20, 300, 150);
pen.Color = System.Drawing.Color.Blue;
graphics.DrawEllipse(pen, 400, 400, 200, 300);
graphics.FillEllipse(brush, 500, 500, 70, 150);
brush.Color = System.Drawing.Color.FromArgb(127, System.Drawing.Color.Red);
graphics.FillRectangle(brush, 450, 550, 300, 300);
pen.Width = 10;
graphics.DrawRectangle(pen, 0, 0, 1000, 1000);
// draw vector string
brush.Color = System.Drawing.Color.Green;
Vintasoft.Imaging.Drawing.IDrawingFont font = Vintasoft.Imaging.Drawing.DrawingFactory.Default.CreateGenericSansSerifFont(30);
graphics.DrawString("Vector string", font, brush, new System.Drawing.PointF(100, 500));
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont pdfFont = document.FontManager.GetStandardFont(
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman);
// draw text string
graphics.DrawString("Text string", pdfFont, 30, brush, new System.Drawing.PointF(100, 600));
}
// get graphics object associated with page
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics = document.Pages[0].GetGraphics())
{
// draw rendered page image (500x500 pixels) onto page
using (Vintasoft.Imaging.VintasoftImage img = document.Pages[0].Render(500, 500))
graphics.DrawImage(img, new System.Drawing.RectangleF(600, 0, 300, 300));
}
// save document
document.SaveChanges(pdfFileName);
// close document
document.Dispose();
}
Public Shared Sub TestPDFGraphics(pdfFileName As String)
' create new PDF document
Dim document As New Vintasoft.Imaging.Pdf.PdfDocument()
' add new page
document.Pages.Add(New System.Drawing.SizeF(1000, 1000))
' get graphics object associated with page
Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = document.Pages(0).GetGraphics()
' draw primitives
Dim pen As New Vintasoft.Imaging.Pdf.Drawing.PdfPen(System.Drawing.Color.Red)
pen.Width = 3
Dim brush As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Green)
graphics.DrawLine(pen, 0, 0, 50, 150)
graphics.DrawCurve(pen, 50, 150, 100, 200, 200, _
20, 300, 150)
pen.Color = System.Drawing.Color.Blue
graphics.DrawEllipse(pen, 400, 400, 200, 300)
graphics.FillEllipse(brush, 500, 500, 70, 150)
brush.Color = System.Drawing.Color.FromArgb(127, System.Drawing.Color.Red)
graphics.FillRectangle(brush, 450, 550, 300, 300)
pen.Width = 10
graphics.DrawRectangle(pen, 0, 0, 1000, 1000)
' draw vector string
brush.Color = System.Drawing.Color.Green
Dim font As Vintasoft.Imaging.Drawing.IDrawingFont = Vintasoft.Imaging.Drawing.DrawingFactory.[Default].CreateGenericSansSerifFont(30)
graphics.DrawString("Vector string", font, brush, New System.Drawing.PointF(100, 500))
Dim pdfFont As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman)
' draw text string
graphics.DrawString("Text string", pdfFont, 30, brush, New System.Drawing.PointF(100, 600))
End Using
' get graphics object associated with page
Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = document.Pages(0).GetGraphics()
' draw rendered page image (500x500 pixels) onto page
Using img As Vintasoft.Imaging.VintasoftImage = document.Pages(0).Render(500, 500)
graphics.DrawImage(img, New System.Drawing.RectangleF(600, 0, 300, 300))
End Using
End Using
' save document
document.SaveChanges(pdfFileName)
' close document
document.Dispose()
End Sub
Draw graphic figures on PDF page
Graphic figure - is a graphic object, that can be drawn using graphic primitives or other graphic figures.
GraphicsFigure class is the base class for all graphic figures.
Here is a list of graphic figures implemented in the SDK:
The figures implementing
IAutoSizeGraphicsFigure interface can calculate their size automatically when changes
the size of panel in which the figure is located.
Hierarchy of graphic figures is open and allows to create new or customize existing figures.
The SDK contains PDF Drawing Demo application, which demonstrates how to perform drawing on PDF page using graphic figures. Source codes of the PDF Drawing Demo application are available in C# and VB.NET.
Here is an example that demonstrates how to draw some graphic figures on PDF page:
public static void CreatePdfDocument(string pdfFilename, string imageFilename)
{
// create PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14))
{
// open image
using (Vintasoft.Imaging.VintasoftImage image = new Vintasoft.Imaging.VintasoftImage(imageFilename))
{
// add new page to PDF document
Vintasoft.Imaging.Pdf.Tree.PdfPage page = document.Pages.Add(
Vintasoft.Imaging.PaperSizeKind.A4);
// add image resource to PDF document
Vintasoft.Imaging.Pdf.Tree.PdfImageResource imageResource =
new Vintasoft.Imaging.Pdf.Tree.PdfImageResource(
document, image, Vintasoft.Imaging.Pdf.PdfCompression.Auto);
// create layout panel
Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.AlignmentPanel rootLayout =
new Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.AlignmentPanel();
rootLayout.Location = page.MediaBox.Location;
rootLayout.Size = page.MediaBox.Size;
// set layout panel margin (5 mm)
double margin = Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPoints(
5, Vintasoft.Imaging.UnitOfMeasure.Millimeters);
rootLayout.Margin = new Vintasoft.Imaging.Pdf.Drawing.PdfContentPadding(margin);
// create image figure
Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.ImageFigure imageFigure =
new Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.ImageFigure(imageResource);
// maintain aspect ratio of image
imageFigure.MaintainAspectRatio = true;
// add image figure to layout panel
rootLayout.Add(imageFigure);
// draw figures on graphics of PDF page
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics g = page.GetGraphics())
rootLayout.Draw(g);
// save document changes
document.SaveChanges();
}
}
}
Public Shared Sub CreatePdfDocument(pdfFilename As String, imageFilename As String)
' create PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14)
' open image
Using image As New Vintasoft.Imaging.VintasoftImage(imageFilename)
' add new page to PDF document
Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4)
' add image resource to PDF document
Dim imageResource As New Vintasoft.Imaging.Pdf.Tree.PdfImageResource(document, image, Vintasoft.Imaging.Pdf.PdfCompression.Auto)
' create layout panel
Dim rootLayout As New Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.AlignmentPanel()
rootLayout.Location = page.MediaBox.Location
rootLayout.Size = page.MediaBox.Size
' set layout panel margin (5 mm)
Dim margin As Double = Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPoints(5, Vintasoft.Imaging.UnitOfMeasure.Millimeters)
rootLayout.Margin = New Vintasoft.Imaging.Pdf.Drawing.PdfContentPadding(margin)
' create image figure
Dim imageFigure As New Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.ImageFigure(imageResource)
' maintain aspect ratio of image
imageFigure.MaintainAspectRatio = True
' add image figure to layout panel
rootLayout.Add(imageFigure)
' draw figures on graphics of PDF page
Using g As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = page.GetGraphics()
rootLayout.Draw(g)
End Using
' save document changes
document.SaveChanges()
End Using
End Using
End Sub