Creating and customizing PDFs is a common requirement in many applications, and adding shapes to PDFs can significantly enhance their visual appeal and functionality. Using C# and .NET, you can easily integrate various shapes into your PDF documents. This article will guide you through the process of customizing PDFs with shapes using Sautinsoft.PDF library.
Shapes can be used for various purposes in PDFs, such as:
A PDF shape, also called path, is a geometric content consisting of one or more subpaths of lines and curves. With SautinSoft.Pdf, you can get, create, or edit shapes (paths) in your C# or VB.NET application. It supports commonly used shapes in PDF: Rectangle, Line, Circle, Ellipse, Arc, and Bezier curve. When you draw a shape in PDF, an instance of the PdfPathContent class is created. You can then manipulate the subpaths, formats, and transformations of the created path. In the code snippet example below, you can see how to add different shapes to a PDF page and how to format them.
Step-by-step guide:
Полный код
using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
namespace Sample
{
class Sample
{
/// <summary>
/// Add shapes to PDF files.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/add-shapes-to-pdf.php
/// </remarks>
static void Main(string[] args)
{
// Before starting this example, please get a free 100-day trial key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// PdfDocument.SetLicense("...");
using (var document = new PdfDocument())
{
// Add a page.
var page = document.Pages.Add();
// NOTE: In PDF, location (0, 0) is at the bottom-left corner of the page
// and the positive y axis extends vertically upward.
var pageBounds = page.CropBox;
// Add a thick red line at the top of the page.
var line = page.Content.Elements.AddPath();
line.BeginSubpath(new PdfPoint(100, pageBounds.Top - 100)).
LineTo(new PdfPoint(pageBounds.Right - 100, pageBounds.Top - 200));
var lineFormat = line.Format;
lineFormat.Stroke.IsApplied = true;
lineFormat.Stroke.Width = 5;
lineFormat.Stroke.Color = PdfColor.FromRgb(1, 0, 0);
// Add a filled and stroked rectangle in the middle of the page.
var rectangle = page.Content.Elements.AddPath();
// NOTE: The start point of the rectangle is the bottom left corner of the rectangle.
rectangle.AddRectangle(new PdfPoint(100, pageBounds.Top - 400),
new PdfSize(pageBounds.Width - 200, 100));
var rectangleFormat = rectangle.Format;
rectangleFormat.Fill.IsApplied = true;
rectangleFormat.Fill.Color = PdfColor.FromRgb(0, 1, 0);
rectangleFormat.Stroke.IsApplied = true;
rectangleFormat.Stroke.Width = 10;
rectangleFormat.Stroke.Color = PdfColor.FromRgb(0, 0, 1);
// Add a more complex semi-transparent filled and stroked path at the bottom of the page.
var shape = page.Content.Elements.AddPath();
shape.BeginSubpath(new PdfPoint(100, 100)).
BezierTo(new PdfPoint(100 + pageBounds.Width / 4, 200),
new PdfPoint(pageBounds.Right - 100 - pageBounds.Width / 4, 0),
new PdfPoint(pageBounds.Right - 100, 100)).
LineTo(new PdfPoint(pageBounds.Right - 100, 300)).
BezierTo(new PdfPoint(pageBounds.Right - 100 - pageBounds.Width / 4, 200),
new PdfPoint(100 + pageBounds.Width / 4, 400),
new PdfPoint(100, 300)).
CloseSubpath();
var shapeFormat = shape.Format;
shapeFormat.Fill.IsApplied = true;
shapeFormat.Fill.Color = PdfColor.FromRgb(0, 1, 0);
shapeFormat.Fill.Opacity = 0.5;
shapeFormat.Stroke.IsApplied = true;
shapeFormat.Stroke.Width = 4;
shapeFormat.Stroke.Color = PdfColor.FromRgb(0, 0, 1);
shapeFormat.Stroke.Opacity = 0.5;
shapeFormat.Stroke.DashPattern = PdfLineDashPatterns.DashDot;
// Add a grid to visualize the bounds of each drawn shape.
var grid = page.Content.Elements.AddPath();
grid.AddRectangle(new PdfPoint(100, 100),
new PdfSize(pageBounds.Width - 200, pageBounds.Height - 200));
grid.BeginSubpath(new PdfPoint(100, pageBounds.Top - 200)).
LineTo(new PdfPoint(pageBounds.Right - 100, pageBounds.Top - 200)).
BeginSubpath(new PdfPoint(100, pageBounds.Top - 300)).
LineTo(new PdfPoint(pageBounds.Right - 100, pageBounds.Top - 300)).
BeginSubpath(new PdfPoint(100, pageBounds.Top - 400)).
LineTo(new PdfPoint(pageBounds.Right - 100, pageBounds.Top - 400)).
BeginSubpath(new PdfPoint(100, 300)).
LineTo(new PdfPoint(pageBounds.Right - 100, 300));
grid.Format.Stroke.IsApplied = true;
// A line width of 0 denotes the thinnest line that can be rendered at device resolution: 1 device pixel wide.
grid.Format.Stroke.Width = 0;
document.Save("Paths.pdf");
}
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Paths.pdf") { UseShellExecute = true });
}
}
}
Option Infer On
Imports System
Imports System.IO
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content
Namespace Sample
Friend Class Sample
''' <summary>
''' Add shapes to PDF files.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/add-shapes-to-pdf.php
''' </remarks>
Shared Sub Main(ByVal args() As String)
' Before starting this example, please get a free license:
' https://sautinsoft.com/start-for-free/
' Apply the key here:
' PdfDocument.SetLicense("...");
Using document = New PdfDocument()
' Add a page.
Dim page = document.Pages.Add()
' NOTE: In PDF, location (0, 0) is at the bottom-left corner of the page
' and the positive y axis extends vertically upward.
Dim pageBounds = page.CropBox
' Add a thick red line at the top of the page.
Dim line = page.Content.Elements.AddPath()
line.BeginSubpath(New PdfPoint(100, pageBounds.Top - 100)).LineTo(New PdfPoint(pageBounds.Right - 100, pageBounds.Top - 200))
Dim lineFormat = line.Format
lineFormat.Stroke.IsApplied = True
lineFormat.Stroke.Width = 5
lineFormat.Stroke.Color = PdfColor.FromRgb(1, 0, 0)
' Add a filled and stroked rectangle in the middle of the page.
Dim rectangle = page.Content.Elements.AddPath()
' NOTE: The start point of the rectangle is the bottom left corner of the rectangle.
rectangle.AddRectangle(New PdfPoint(100, pageBounds.Top - 400), New PdfSize(pageBounds.Width - 200, 100))
Dim rectangleFormat = rectangle.Format
rectangleFormat.Fill.IsApplied = True
rectangleFormat.Fill.Color = PdfColor.FromRgb(0, 1, 0)
rectangleFormat.Stroke.IsApplied = True
rectangleFormat.Stroke.Width = 10
rectangleFormat.Stroke.Color = PdfColor.FromRgb(0, 0, 1)
' Add a more complex semi-transparent filled and stroked path at the bottom of the page.
Dim shape = page.Content.Elements.AddPath()
shape.BeginSubpath(New PdfPoint(100, 100)).BezierTo(New PdfPoint(100 + pageBounds.Width \ 4, 200), New PdfPoint(pageBounds.Right - 100 - pageBounds.Width \ 4, 0), New PdfPoint(pageBounds.Right - 100, 100)).LineTo(New PdfPoint(pageBounds.Right - 100, 300)).BezierTo(New PdfPoint(pageBounds.Right - 100 - pageBounds.Width \ 4, 200), New PdfPoint(100 + pageBounds.Width \ 4, 400), New PdfPoint(100, 300)).CloseSubpath()
Dim shapeFormat = shape.Format
shapeFormat.Fill.IsApplied = True
shapeFormat.Fill.Color = PdfColor.FromRgb(0, 1, 0)
shapeFormat.Fill.Opacity = 0.5
shapeFormat.Stroke.IsApplied = True
shapeFormat.Stroke.Width = 4
shapeFormat.Stroke.Color = PdfColor.FromRgb(0, 0, 1)
shapeFormat.Stroke.Opacity = 0.5
shapeFormat.Stroke.DashPattern = PdfLineDashPatterns.DashDot
' Add a grid to visualize the bounds of each drawn shape.
Dim grid = page.Content.Elements.AddPath()
grid.AddRectangle(New PdfPoint(100, 100), New PdfSize(pageBounds.Width - 200, pageBounds.Height - 200))
grid.BeginSubpath(New PdfPoint(100, pageBounds.Top - 200)).LineTo(New PdfPoint(pageBounds.Right - 100, pageBounds.Top - 200)).BeginSubpath(New PdfPoint(100, pageBounds.Top - 300)).LineTo(New PdfPoint(pageBounds.Right - 100, pageBounds.Top - 300)).BeginSubpath(New PdfPoint(100, pageBounds.Top - 400)).LineTo(New PdfPoint(pageBounds.Right - 100, pageBounds.Top - 400)).BeginSubpath(New PdfPoint(100, 300)).LineTo(New PdfPoint(pageBounds.Right - 100, 300))
grid.Format.Stroke.IsApplied = True
' A line width of 0 denotes the thinnest line that can be rendered at device resolution: 1 device pixel wide.
grid.Format.Stroke.Width = 0
document.Save("Paths.pdf")
End Using
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Paths.pdf") With {.UseShellExecute = True})
End Sub
End Class
End Namespace
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: