Skip to content

Commit b3e1154

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

File tree

5 files changed

+116
-2
lines changed

5 files changed

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

0 commit comments

Comments
 (0)