The graphic PDF objects in HiQPdf software are the lines, rectangles, circles, ellipses, ellipse arcs, ellipse slices, Bezier curves and polygons. The graphic objects can be laid out in any position in a canvas and they are not paginated.
The graphic PDF objects are all derived from the PdfDrawableObject class which is a specialization of the PdfObject generic class.
In this demo you can learn how to layout graphics in a PDF document with various line styles and fill modes. There are examples for drawing lines, circles, ellipses, elippses arcs and slices, rectangles, Bezier curves and polygons.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using HiQPdfClient;
namespace HiQPdf_Demo.Controllers
{
public class PdfGraphicsController : Controller
{
// GET: PdfGraphics
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult CreatePdf(IFormCollection collection)
{
string serverIP = collection["textBoxServerIP"];
uint serverPort = uint.Parse(collection["textBoxServerPort"]);
string serverPassword = collection["textBoxServerPassword"];
// create a PDF document
PdfDocument document = new PdfDocument(serverIP, serverPort);
// use server password if necessary
if (serverPassword.Length > 0)
document.ServerPassword = serverPassword;
// set a demo serial number
document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// create a page in document
PdfPage page1 = document.AddPage();
// create the true type fonts that can be used in document text
PdfFont pdfFontEmbed = document.CreateFontFromName("Times New Roman", 10, true);
float crtYPos = 20;
float crtXPos = 5;
#region Layout lines
PdfText titleTextLines = new PdfText(crtXPos, crtYPos, "Lines:", pdfFontEmbed);
titleTextLines.ForeColor = PdfColor.Navy;
page1.Layout(titleTextLines);
// advance the Y position in the PDF page
crtYPos += pdfFontEmbed.Size + 10;
// layout a simple line
PdfLine pdfLine = new PdfLine(new PointFloat(crtXPos, crtYPos), new PointFloat(crtXPos + 60, crtYPos));
page1.Layout(pdfLine);
// layout a thick line
PdfLine pdfThickLine = new PdfLine(new PointFloat(crtXPos + 70, crtYPos), new PointFloat(crtXPos + 130, crtYPos));
pdfThickLine.LineStyle.LineWidth = 3;
page1.Layout(pdfThickLine);
// layout a dotted colored line
PdfLine pdfDottedLine = new PdfLine(new PointFloat(crtXPos + 140, crtYPos), new PointFloat(crtXPos + 200, crtYPos));
pdfDottedLine.LineStyle.LineDashPattern = PdfLineDashPattern.Dot;
pdfDottedLine.ForeColor = PdfColor.Green;
page1.Layout(pdfDottedLine);
// layout a dashed colored line
PdfLine pdfDashedLine = new PdfLine(new PointFloat(crtXPos + 210, crtYPos), new PointFloat(crtXPos + 270, crtYPos));
pdfDashedLine.LineStyle.LineDashPattern = PdfLineDashPattern.Dash;
pdfDashedLine.ForeColor = PdfColor.Green;
page1.Layout(pdfDashedLine);
// layout a dash-dot-dot colored line
PdfLine pdfDashDotDotLine = new PdfLine(new PointFloat(crtXPos + 280, crtYPos), new PointFloat(crtXPos + 340, crtYPos));
pdfDashDotDotLine.LineStyle.LineDashPattern = PdfLineDashPattern.DashDotDot;
pdfDashDotDotLine.ForeColor = PdfColor.Green;
page1.Layout(pdfDashDotDotLine);
// layout a thick line with rounded cap style
PdfLine pdfRoundedLine = new PdfLine(new PointFloat(crtXPos + 350, crtYPos), new PointFloat(crtXPos + 410, crtYPos));
pdfRoundedLine.LineStyle.LineWidth = 5;
pdfRoundedLine.LineStyle.LineCapStyle = PdfLineCapStyle.RoundCap;
pdfRoundedLine.ForeColor = PdfColor.Blue;
page1.Layout(pdfRoundedLine);
// advance the Y position in the PDF page
crtYPos += 10;
#endregion
#region Layout circles
PdfText titleTextCircles = new PdfText(crtXPos, crtYPos, "Circles:", pdfFontEmbed);
titleTextCircles.ForeColor = PdfColor.Navy;
page1.Layout(titleTextCircles);
// advance the Y position in the PDF page
crtYPos += pdfFontEmbed.Size + 10;
// draw a simple circle with solid line
PdfCircle pdfCircle = new PdfCircle(new PointFloat(crtXPos + 30, crtYPos + 30), 30);
page1.Layout(pdfCircle);
// draw a simple circle with dotted border line
PdfCircle pdfDottedCircle = new PdfCircle(new PointFloat(crtXPos + 100, crtYPos + 30), 30);
pdfDottedCircle.ForeColor = PdfColor.Green;
pdfDottedCircle.LineStyle.LineDashPattern = PdfLineDashPattern.Dot;
page1.Layout(pdfDottedCircle);
// draw a circle with colored border line and fill color
PdfCircle pdfDisc = new PdfCircle(new PointFloat(crtXPos + 170, crtYPos + 30), 30);
pdfDisc.ForeColor = PdfColor.Navy;
pdfDisc.BackColor = PdfColor.WhiteSmoke;
page1.Layout(pdfDisc);
// draw a circle with thick border line
PdfCircle pdfThickBorderDisc = new PdfCircle(new PointFloat(crtXPos + 240, crtYPos + 30), 30);
pdfThickBorderDisc.LineStyle.LineWidth = 5;
pdfThickBorderDisc.BackColor = PdfColor.LightSalmon;
pdfThickBorderDisc.ForeColor = PdfColor.LightBlue;
page1.Layout(pdfThickBorderDisc);
crtYPos += 70;
#endregion
#region Layout ellipses and arcs
PdfText titleTextEllipses = new PdfText(crtXPos, crtYPos, "Ellipses, arcs and slices:", pdfFontEmbed);
titleTextEllipses.ForeColor = PdfColor.Navy;
page1.Layout(titleTextEllipses);
// advance the Y position in the PDF page
crtYPos += pdfFontEmbed.Size + 10;
// draw a simple ellipse with solid line
PdfEllipse pdfEllipse = new PdfEllipse(new PointFloat(crtXPos + 50, crtYPos + 30), 50, 30);
page1.Layout(pdfEllipse);
// draw an ellipse with fill color and colored border line
PdfEllipse pdfFilledEllipse = new PdfEllipse(new PointFloat(crtXPos + 160, crtYPos + 30), 50, 30);
pdfFilledEllipse.BackColor = PdfColor.WhiteSmoke;
pdfFilledEllipse.ForeColor = PdfColor.Green;
page1.Layout(pdfFilledEllipse);
// draw an ellipse arc with colored border line
PdfEllipseArc pdfEllipseArc1 = new PdfEllipseArc(new RectangleFloat(crtXPos + 220, crtYPos, 100, 60), 0, 90);
pdfEllipseArc1.ForeColor = PdfColor.OrangeRed;
pdfEllipseArc1.LineStyle.LineWidth = 3;
page1.Layout(pdfEllipseArc1);
// draw an ellipse arc with fill color and colored border line
PdfEllipseArc pdfEllipseArc2 = new PdfEllipseArc(new RectangleFloat(crtXPos + 220, crtYPos, 100, 60), 90, 90);
pdfEllipseArc2.ForeColor = PdfColor.Navy;
page1.Layout(pdfEllipseArc2);
// draw an ellipse arc with fill color and colored border line
PdfEllipseArc pdfEllipseArc3 = new PdfEllipseArc(new RectangleFloat(crtXPos + 220, crtYPos, 100, 60), 180, 90);
pdfEllipseArc3.ForeColor = PdfColor.Green;
pdfEllipseArc3.LineStyle.LineWidth = 3;
page1.Layout(pdfEllipseArc3);
// draw an ellipse arc with fill color and colored border line
PdfEllipseArc pdfEllipseArc4 = new PdfEllipseArc(new RectangleFloat(crtXPos + 220, crtYPos, 100, 60), 270, 90);
pdfEllipseArc4.ForeColor = PdfColor.Yellow;
page1.Layout(pdfEllipseArc4);
// draw an ellipse slice
PdfEllipseSlice pdfEllipseSlice1 = new PdfEllipseSlice(new RectangleFloat(crtXPos + 330, crtYPos, 100, 60), 0, 90);
pdfEllipseSlice1.BackColor = PdfColor.Coral;
page1.Layout(pdfEllipseSlice1);
// draw an ellipse slice
PdfEllipseSlice pdfEllipseSlice2 = new PdfEllipseSlice(new RectangleFloat(crtXPos + 330, crtYPos, 100, 60), 90, 90);
pdfEllipseSlice2.BackColor = PdfColor.LightBlue;
page1.Layout(pdfEllipseSlice2);
// draw an ellipse slice
PdfEllipseSlice pdfEllipseSlice3 = new PdfEllipseSlice(new RectangleFloat(crtXPos + 330, crtYPos, 100, 60), 180, 90);
pdfEllipseSlice3.BackColor = PdfColor.LightGreen;
page1.Layout(pdfEllipseSlice3);
// draw an ellipse slice
PdfEllipseSlice pdfEllipseSlice4 = new PdfEllipseSlice(new RectangleFloat(crtXPos + 330, crtYPos, 100, 60), 270, 90);
pdfEllipseSlice4.BackColor = PdfColor.Yellow;
page1.Layout(pdfEllipseSlice4);
crtYPos += 70;
#endregion
#region Layout rectangles
PdfText titleTextRectangles = new PdfText(crtXPos, crtYPos, "Rectangles:", pdfFontEmbed);
titleTextRectangles.ForeColor = PdfColor.Navy;
page1.Layout(titleTextRectangles);
// advance the Y position in the PDF page
crtYPos += pdfFontEmbed.Size + 10;
// draw a simple rectangle with solid line
PdfRectangle pdfRectangle = new PdfRectangle(crtXPos, crtYPos, 100, 60);
page1.Layout(pdfRectangle);
// draw a rectangle with fill color and colored dotted border line
PdfRectangle pdfFilledRectangle = new PdfRectangle(crtXPos + 110, crtYPos, 100, 60);
pdfFilledRectangle.BackColor = PdfColor.WhiteSmoke;
pdfFilledRectangle.ForeColor = PdfColor.Green;
pdfFilledRectangle.LineStyle.LineDashPattern = PdfLineDashPattern.Dot;
page1.Layout(pdfFilledRectangle);
// draw a rectangle with fill color and without border line
PdfRectangle pdfFilledNoBorderRectangle = new PdfRectangle(crtXPos + 220, crtYPos, 100, 60);
pdfFilledNoBorderRectangle.BackColor = PdfColor.LightGreen;
page1.Layout(pdfFilledNoBorderRectangle);
// draw a rectangle filled with a thick border line and reounded corners
PdfRectangle pdfThickBorderRectangle = new PdfRectangle(crtXPos + 330, crtYPos, 100, 60);
pdfThickBorderRectangle.BackColor = PdfColor.LightSalmon;
pdfThickBorderRectangle.ForeColor = PdfColor.LightBlue;
pdfThickBorderRectangle.LineStyle.LineWidth = 5;
pdfThickBorderRectangle.LineStyle.LineJoinStyle = PdfLineJoinStyle.RoundJoin;
page1.Layout(pdfThickBorderRectangle);
crtYPos += 70;
#endregion
#region Layout Bezier curves
PdfText titleTextBezier = new PdfText(crtXPos, crtYPos, "Bezier curves and polygons:", pdfFontEmbed);
titleTextBezier.ForeColor = PdfColor.Navy;
page1.Layout(titleTextBezier);
// advance the Y position in the PDF page
crtYPos += pdfFontEmbed.Size + 10;
// the points controlling the Bezier curve
PointFloat pt1 = new PointFloat(crtXPos, crtYPos + 50);
PointFloat pt2 = new PointFloat(crtXPos + 50, crtYPos);
PointFloat pt3 = new PointFloat(crtXPos + 100, crtYPos + 100);
PointFloat pt4 = new PointFloat(crtXPos + 150, crtYPos + 50);
// draw controlling points
PdfCircle pt1Circle = new PdfCircle(pt1, 2);
pt1Circle.BackColor = PdfColor.LightSalmon;
page1.Layout(pt1Circle);
PdfCircle pt2Circle = new PdfCircle(pt2, 2);
pt2Circle.BackColor = PdfColor.LightSalmon;
page1.Layout(pt2Circle);
PdfCircle pt3Circle = new PdfCircle(pt3, 2);
pt3Circle.BackColor = PdfColor.LightSalmon;
page1.Layout(pt3Circle);
PdfCircle pt4Circle = new PdfCircle(pt4, 2);
pt4Circle.BackColor = PdfColor.LightSalmon;
page1.Layout(pt4Circle);
// draw the Bezier curve
PdfBezierCurve bezierCurve = new PdfBezierCurve(pt1, pt2, pt3, pt4);
bezierCurve.ForeColor = PdfColor.LightBlue;
bezierCurve.LineStyle.LineWidth = 2;
page1.Layout(bezierCurve);
#endregion
#region Layout a polygons
// layout a polygon without fill color
// the polygon points
PointFloat[] polyPoints = new PointFloat[]{
new PointFloat(crtXPos + 200, crtYPos + 50),
new PointFloat(crtXPos + 250, crtYPos),
new PointFloat(crtXPos + 300, crtYPos + 50),
new PointFloat(crtXPos + 250, crtYPos + 100)
};
// draw polygon points
foreach (PointFloat polyPoint in polyPoints)
{
PdfCircle pointCircle = new PdfCircle(polyPoint, 2);
pointCircle.BackColor = PdfColor.LightSalmon;
page1.Layout(pointCircle);
}
// draw the polygon line
PdfPolygon pdfPolygon = new PdfPolygon(polyPoints);
pdfPolygon.ForeColor = PdfColor.LightBlue;
pdfPolygon.LineStyle.LineWidth = 2;
pdfPolygon.LineStyle.LineCapStyle = PdfLineCapStyle.ProjectingSquareCap;
page1.Layout(pdfPolygon);
// layout a polygon with fill color
// the polygon points
PointFloat[] polyFillPoints = new PointFloat[]{
new PointFloat(crtXPos + 330, crtYPos + 50),
new PointFloat(crtXPos + 380, crtYPos),
new PointFloat(crtXPos + 430, crtYPos + 50),
new PointFloat(crtXPos + 380, crtYPos + 100)
};
// draw a polygon with fill color and thick rounded border
PdfPolygon pdfFillPolygon = new PdfPolygon(polyFillPoints);
pdfFillPolygon.ForeColor = PdfColor.LightBlue;
pdfFillPolygon.BackColor = PdfColor.LightSalmon;
pdfFillPolygon.LineStyle.LineWidth = 5;
pdfFillPolygon.LineStyle.LineCapStyle = PdfLineCapStyle.RoundCap;
pdfFillPolygon.LineStyle.LineJoinStyle = PdfLineJoinStyle.RoundJoin;
page1.Layout(pdfFillPolygon);
#endregion
try
{
// write the PDF document to a memory buffer
byte[] pdfBuffer = document.WriteToMemory();
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "PdfGraphics.pdf";
return fileResult;
}
finally
{
document.Close();
}
}
}
}