Skip to content

Commit ed8d9fb

Browse files
author
Patrick Ammann
committed
feat: add test project (not for merge)
1 parent 30e816a commit ed8d9fb

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

PdfSharpCore.sln

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29911.84
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32519.379
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PdfSharpCore", "PdfSharpCore\PdfSharpCore.csproj", "{4005DEBC-75F8-48DA-BBCC-353E17872B3E}"
77
EndProject
@@ -18,6 +18,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1818
EndProject
1919
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PdfSharpCore.Test", "PdfSharpCore.Test\PdfSharpCore.Test.csproj", "{A862C0CE-C095-459C-A32B-8FCDD15A93BF}"
2020
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignatureTestConsole", "SignatureTestConsole\SignatureTestConsole.csproj", "{272E99C1-3210-497B-8CCE-D561E353FC13}"
22+
ProjectSection(ProjectDependencies) = postProject
23+
{4005DEBC-75F8-48DA-BBCC-353E17872B3E} = {4005DEBC-75F8-48DA-BBCC-353E17872B3E}
24+
EndProjectSection
25+
EndProject
2126
Global
2227
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2328
Debug|Any CPU = Debug|Any CPU
@@ -44,6 +49,10 @@ Global
4449
{A862C0CE-C095-459C-A32B-8FCDD15A93BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
4550
{A862C0CE-C095-459C-A32B-8FCDD15A93BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
4651
{A862C0CE-C095-459C-A32B-8FCDD15A93BF}.Release|Any CPU.Build.0 = Release|Any CPU
52+
{272E99C1-3210-497B-8CCE-D561E353FC13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
53+
{272E99C1-3210-497B-8CCE-D561E353FC13}.Debug|Any CPU.Build.0 = Debug|Any CPU
54+
{272E99C1-3210-497B-8CCE-D561E353FC13}.Release|Any CPU.ActiveCfg = Release|Any CPU
55+
{272E99C1-3210-497B-8CCE-D561E353FC13}.Release|Any CPU.Build.0 = Release|Any CPU
4756
EndGlobalSection
4857
GlobalSection(SolutionProperties) = preSolution
4958
HideSolutionNode = FALSE

SignatureTestConsole/Program.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using PdfSharpCore.Drawing;
2+
using PdfSharpCore.Pdf;
3+
using System.Security.Cryptography.X509Certificates;
4+
using PdfSharpCore.Pdf.IO;
5+
using PdfSharpCore.Drawing.Layout;
6+
using PdfSharpCore.Pdf.Signatures;
7+
8+
namespace TestConsole
9+
{
10+
class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
Program.CreateAndSign();
15+
Program.SignExisting();
16+
}
17+
18+
private static void CreateAndSign()
19+
{
20+
string text = "CreateAndSign.pdf";
21+
XFont font = new XFont("Verdana", 10.0, XFontStyle.Regular);
22+
PdfDocument pdfDocument = new PdfDocument();
23+
PdfPage pdfPage = pdfDocument.AddPage();
24+
XGraphics xGraphics = XGraphics.FromPdfPage(pdfPage);
25+
XRect layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width, pdfPage.Height);
26+
xGraphics.DrawString("Sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter);
27+
PdfSignatureOptions options = new PdfSignatureOptions
28+
{
29+
ContactInfo = "Contact Info",
30+
Location = "Paris",
31+
Reason = "Test signatures",
32+
Rectangle = new XRect(36.0, 700.0, 200.0, 50.0)
33+
};
34+
PdfSignatureHandler pdfSignatureHandler = new PdfSignatureHandler( new DefaultSigner(Program.GetCertificate()), null, options);
35+
pdfSignatureHandler.AttachToDocument(pdfDocument);
36+
pdfDocument.Save(text);
37+
}
38+
private static void SignExisting()
39+
{
40+
string text = string.Format("SignExisting.pdf", new object[0]);
41+
PdfDocument pdfDocument = PdfReader.Open("TestFiles\\doc1.pdf");
42+
PdfSignatureOptions options = new PdfSignatureOptions
43+
{
44+
ContactInfo = "Contact Info",
45+
Location = "Paris",
46+
Reason = "Test signatures",
47+
Rectangle = new XRect(36.0, 735.0, 200.0, 50.0),
48+
AppearanceHandler = new Program.SignAppearenceHandler()
49+
};
50+
51+
PdfSignatureHandler pdfSignatureHandler = new PdfSignatureHandler(new DefaultSigner(Program.GetCertificate()), null, options);
52+
pdfSignatureHandler.AttachToDocument(pdfDocument);
53+
pdfDocument.Save(text);
54+
}
55+
56+
private static X509Certificate2 GetCertificate()
57+
{
58+
// add yours here
59+
return new X509Certificate2("TestFiles\\myself.pfx", "1234", X509KeyStorageFlags.Exportable);
60+
}
61+
62+
private class SignAppearenceHandler : ISignatureAppearanceHandler
63+
{
64+
private XImage Image = XImage.FromFile("TestFiles\\logo.jpg");
65+
public void DrawAppearance(XGraphics gfx, XRect rect)
66+
{
67+
XColor empty = XColor.Empty;
68+
string text = "Signed by Napoleon \nLocation: Paris \nDate: " + DateTime.Now.ToString();
69+
XFont font = new XFont("Verdana", 7.0, XFontStyle.Regular);
70+
XTextFormatter xTextFormatter = new XTextFormatter(gfx);
71+
int num = this.Image.PixelWidth / this.Image.PixelHeight;
72+
XPoint xPoint = new XPoint(0.0, 0.0);
73+
bool flag = this.Image != null;
74+
if (flag)
75+
{
76+
gfx.DrawImage(this.Image, xPoint.X, xPoint.Y, rect.Width / 4.0, rect.Width / 4.0 / (double)num);
77+
xPoint = new XPoint(rect.Width / 4.0, 0.0);
78+
}
79+
xTextFormatter.DrawString(text, font, new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)), new XRect(xPoint.X, xPoint.Y, rect.Width - xPoint.X, rect.Height), XStringFormats.TopLeft);
80+
}
81+
}
82+
}
83+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\PdfSharpCore\PdfSharpCore.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="TestFiles\doc1.pdf">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="TestFiles\logo.JPG">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>
12.6 KB
Binary file not shown.
65.2 KB
Loading

0 commit comments

Comments
 (0)