This example demonstrates how to draw beziers on a PDF document.
To draws the successive Bézier curves to the screen
Set the start, end, and two control points for a first curve and endpoint and two control points for a second curve.
Clicking generate button will launch a pdf with Bézier curves
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;
namespace OfficeComponent.Samples
{
class BeziersExample : ExampleBase
{
public BeziersExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public BeziersExample(string commonDataPath, string outputDir, string xmlFile)
: base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
// Create a new instance of PdfDocument class.
PdfDocument doc = new PdfDocument();
// Add the first page.
PdfPage page = doc.Pages.Add();
float contentWidth = page.Size.Width - doc.PageSettings.Margins.Left - doc.PageSettings.Margins.Right;
float contentHeight = page.Size.Height - doc.PageSettings.Margins.Left - doc.PageSettings.Margins.Right;
//Create Pdf graphics for the page
PdfGraphics g = page.Graphics;
Random rnd = new Random();
// Draw 30 beziers
for (int i = 0; i < 30; i++)
{
const int pointsNum = 4;
PointF[] points = new PointF[pointsNum];
for (int j = 0; j < pointsNum; j++)
{
points[j].X = rnd.Next((int)contentWidth);
points[j].Y = rnd.Next((int)contentHeight);
}
PdfBezierCurve curve = new PdfBezierCurve(points[0], points[1], points[2], points[3]);
curve.Pen = new PdfPen(new PdfColor((byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256)));
curve.Draw(g);
}
// Save and close the document.
var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
doc.Save(outputPath);
doc.Close(true);
return outputPath;
}
}
}
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics
Namespace OfficeComponent.Samples
Friend Class BeziersExample
Inherits ExampleBase
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
' Create a new instance of PdfDocument class.
Dim doc As New PdfDocument()
' Add the first page.
Dim page As PdfPage = doc.Pages.Add()
Dim contentWidth As Single = page.Size.Width - doc.PageSettings.Margins.Left - doc.PageSettings.Margins.Right
Dim contentHeight As Single = page.Size.Height - doc.PageSettings.Margins.Left - doc.PageSettings.Margins.Right
'Create Pdf graphics for the page
Dim g As PdfGraphics = page.Graphics
Dim rnd As New Random()
' Draw 30 beziers
For i As Integer = 0 To 29
Const pointsNum As Integer = 4
Dim points(pointsNum - 1) As PointF
For j As Integer = 0 To pointsNum - 1
points(j).X = rnd.Next(CInt(Fix(contentWidth)))
points(j).Y = rnd.Next(CInt(Fix(contentHeight)))
Next j
Dim curve As New PdfBezierCurve(points(0), points(1), points(2), points(3))
curve.Pen = New PdfPen(New PdfColor(CByte(rnd.Next(256)), CByte(rnd.Next(256)), CByte(rnd.Next(256))))
curve.Draw(g)
Next i
' Save and close the document.
Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
doc.Save(outputPath)
doc.Close(True)
Return outputPath
End Function
End Class
End Namespace