Skip to content

Commit aa7fa1e

Browse files
Merge pull request #140 from SyncfusionExamples/Task-935631-RotatedPDF
Task-935631-How to draw signature field in the rotated PDF document
2 parents 00d3d48 + db894a2 commit aa7fa1e

File tree

7 files changed

+146
-0
lines changed

7 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35617.110 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Drawing-Signature-Fields-in-Rotated-PDF-Documents", "Drawing-Signature-Fields-in-Rotated-PDF-Documents\Drawing-Signature-Fields-in-Rotated-PDF-Documents.csproj", "{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {7BE57722-9807-4418-9F64-8BCD78D871A0}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Drawing_Signature_Fields_in_Rotated_PDF_Documents</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

Digital Signature/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents/Output/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using Syncfusion.Pdf;
2+
using System.IO;
3+
using Syncfusion.Pdf.Parsing;
4+
using Syncfusion.Drawing;
5+
using Syncfusion.Pdf.Graphics;
6+
using Syncfusion.Pdf.Security;
7+
8+
internal class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
//Open the Word document file stream.
13+
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/TestPDF.pdf"), FileMode.Open, FileAccess.Read))
14+
{
15+
16+
PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(inputStream);
17+
18+
PdfLoadedPage ldPage = pdfLoadedDocument.Pages[3] as PdfLoadedPage;
19+
20+
//Create a certificate instance from a PFX file with a private key.
21+
FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read);
22+
PdfCertificate pdfCert = new PdfCertificate(certificateStream, "password123");
23+
24+
PdfSignature signature = new PdfSignature(pdfLoadedDocument, ldPage, pdfCert, "Signature1");
25+
26+
RectangleF bounds = new RectangleF(new PointF(20, 20), new SizeF(240, 70));
27+
28+
29+
signature.Bounds = GetRelativeBounds(ldPage, bounds);
30+
31+
PdfGraphics graphics = signature.Appearance.Normal.Graphics;
32+
33+
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/TestImage.png"), FileMode.Open, FileAccess.Read);
34+
//Set an image for signature field.
35+
PdfBitmap signatureImage = new PdfBitmap(imageStream);
36+
37+
RotateSignatureAppearance(signatureImage, signature.Appearance.Normal.Graphics, ldPage.Rotation, signature.Bounds);
38+
39+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
40+
{
41+
//Save the PDF document to file stream.
42+
pdfLoadedDocument.Save(outputFileStream);
43+
}
44+
//Closes the document
45+
pdfLoadedDocument.Close(true);
46+
47+
}
48+
}
49+
private static RectangleF GetRelativeBounds(PdfLoadedPage page, RectangleF bounds)
50+
{
51+
SizeF pagesize = page.Size;
52+
RectangleF rectangle = bounds;
53+
54+
if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
55+
{
56+
rectangle.X = bounds.Y;
57+
rectangle.Y = pagesize.Height - ((bounds.X + bounds.Width));
58+
rectangle.Width = bounds.Height;
59+
rectangle.Height = bounds.Width;
60+
}
61+
else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
62+
{
63+
rectangle.Y = bounds.X;
64+
rectangle.X = pagesize.Width - (bounds.Y + bounds.Height);
65+
rectangle.Width = bounds.Height;
66+
rectangle.Height = bounds.Width;
67+
}
68+
else if (page.Rotation == PdfPageRotateAngle.RotateAngle180)
69+
{
70+
rectangle.X = pagesize.Width - (bounds.X + bounds.Width);
71+
rectangle.Y = pagesize.Height - (bounds.Y + bounds.Height);
72+
}
73+
return rectangle;
74+
}
75+
76+
private static void RotateSignatureAppearance(PdfImage image, PdfGraphics graphics, PdfPageRotateAngle angle, RectangleF bounds)
77+
{
78+
graphics.Save();
79+
80+
if (angle == PdfPageRotateAngle.RotateAngle90)
81+
{
82+
graphics.TranslateTransform(0, bounds.Height);
83+
graphics.RotateTransform(-90);
84+
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width));
85+
}
86+
else if (angle == PdfPageRotateAngle.RotateAngle180)
87+
{
88+
graphics.TranslateTransform(bounds.Width, bounds.Height);
89+
graphics.RotateTransform(-180);
90+
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height));
91+
}
92+
else if (angle == PdfPageRotateAngle.RotateAngle270)
93+
{
94+
graphics.TranslateTransform(bounds.Width, 0);
95+
graphics.RotateTransform(-270);
96+
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width));
97+
}
98+
else
99+
{
100+
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height));
101+
}
102+
graphics.Restore();
103+
}
104+
105+
}
106+

0 commit comments

Comments
 (0)