Skip to content

Task-935631-How to draw signature field in the rotated PDF document #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35617.110 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DED69A8E-0D66-4E3A-BD90-F5EE9F2BD5C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7BE57722-9807-4418-9F64-8BCD78D871A0}
EndGlobalSection
EndGlobal
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Drawing_Signature_Fields_in_Rotated_PDF_Documents</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Syncfusion.Pdf;
using System.IO;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Security;

internal class Program
{
static void Main(string[] args)
{
//Open the Word document file stream.
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/TestPDF.pdf"), FileMode.Open, FileAccess.Read))
{

PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(inputStream);

PdfLoadedPage ldPage = pdfLoadedDocument.Pages[3] as PdfLoadedPage;

//Create a certificate instance from a PFX file with a private key.
FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read);
PdfCertificate pdfCert = new PdfCertificate(certificateStream, "password123");

PdfSignature signature = new PdfSignature(pdfLoadedDocument, ldPage, pdfCert, "Signature1");

RectangleF bounds = new RectangleF(new PointF(20, 20), new SizeF(240, 70));


signature.Bounds = GetRelativeBounds(ldPage, bounds);

PdfGraphics graphics = signature.Appearance.Normal.Graphics;

FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/TestImage.png"), FileMode.Open, FileAccess.Read);
//Set an image for signature field.
PdfBitmap signatureImage = new PdfBitmap(imageStream);

RotateSignatureAppearance(signatureImage, signature.Appearance.Normal.Graphics, ldPage.Rotation, signature.Bounds);

using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document to file stream.
pdfLoadedDocument.Save(outputFileStream);
}
//Closes the document
pdfLoadedDocument.Close(true);

}
}
private static RectangleF GetRelativeBounds(PdfLoadedPage page, RectangleF bounds)
{
SizeF pagesize = page.Size;
RectangleF rectangle = bounds;

if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
{
rectangle.X = bounds.Y;
rectangle.Y = pagesize.Height - ((bounds.X + bounds.Width));
rectangle.Width = bounds.Height;
rectangle.Height = bounds.Width;
}
else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
{
rectangle.Y = bounds.X;
rectangle.X = pagesize.Width - (bounds.Y + bounds.Height);
rectangle.Width = bounds.Height;
rectangle.Height = bounds.Width;
}
else if (page.Rotation == PdfPageRotateAngle.RotateAngle180)
{
rectangle.X = pagesize.Width - (bounds.X + bounds.Width);
rectangle.Y = pagesize.Height - (bounds.Y + bounds.Height);
}
return rectangle;
}

private static void RotateSignatureAppearance(PdfImage image, PdfGraphics graphics, PdfPageRotateAngle angle, RectangleF bounds)
{
graphics.Save();

if (angle == PdfPageRotateAngle.RotateAngle90)
{
graphics.TranslateTransform(0, bounds.Height);
graphics.RotateTransform(-90);
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width));
}
else if (angle == PdfPageRotateAngle.RotateAngle180)
{
graphics.TranslateTransform(bounds.Width, bounds.Height);
graphics.RotateTransform(-180);
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height));
}
else if (angle == PdfPageRotateAngle.RotateAngle270)
{
graphics.TranslateTransform(bounds.Width, 0);
graphics.RotateTransform(-270);
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width));
}
else
{
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height));
}
graphics.Restore();
}

}

Loading