Skip to content

Commit

Permalink
feat: add test project (not for merge)
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Ammann committed Jun 2, 2022
1 parent 30e816a commit ed8d9fb
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
13 changes: 11 additions & 2 deletions PdfSharpCore.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29911.84
# Visual Studio Version 17
VisualStudioVersion = 17.2.32519.379
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PdfSharpCore", "PdfSharpCore\PdfSharpCore.csproj", "{4005DEBC-75F8-48DA-BBCC-353E17872B3E}"
EndProject
Expand All @@ -18,6 +18,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PdfSharpCore.Test", "PdfSharpCore.Test\PdfSharpCore.Test.csproj", "{A862C0CE-C095-459C-A32B-8FCDD15A93BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignatureTestConsole", "SignatureTestConsole\SignatureTestConsole.csproj", "{272E99C1-3210-497B-8CCE-D561E353FC13}"
ProjectSection(ProjectDependencies) = postProject
{4005DEBC-75F8-48DA-BBCC-353E17872B3E} = {4005DEBC-75F8-48DA-BBCC-353E17872B3E}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -44,6 +49,10 @@ Global
{A862C0CE-C095-459C-A32B-8FCDD15A93BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A862C0CE-C095-459C-A32B-8FCDD15A93BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A862C0CE-C095-459C-A32B-8FCDD15A93BF}.Release|Any CPU.Build.0 = Release|Any CPU
{272E99C1-3210-497B-8CCE-D561E353FC13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{272E99C1-3210-497B-8CCE-D561E353FC13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{272E99C1-3210-497B-8CCE-D561E353FC13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{272E99C1-3210-497B-8CCE-D561E353FC13}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
83 changes: 83 additions & 0 deletions SignatureTestConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using System.Security.Cryptography.X509Certificates;
using PdfSharpCore.Pdf.IO;
using PdfSharpCore.Drawing.Layout;
using PdfSharpCore.Pdf.Signatures;

namespace TestConsole
{
class Program
{
public static void Main(string[] args)
{
Program.CreateAndSign();
Program.SignExisting();
}

private static void CreateAndSign()
{
string text = "CreateAndSign.pdf";
XFont font = new XFont("Verdana", 10.0, XFontStyle.Regular);
PdfDocument pdfDocument = new PdfDocument();
PdfPage pdfPage = pdfDocument.AddPage();
XGraphics xGraphics = XGraphics.FromPdfPage(pdfPage);
XRect layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width, pdfPage.Height);
xGraphics.DrawString("Sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter);
PdfSignatureOptions options = new PdfSignatureOptions
{
ContactInfo = "Contact Info",
Location = "Paris",
Reason = "Test signatures",
Rectangle = new XRect(36.0, 700.0, 200.0, 50.0)
};
PdfSignatureHandler pdfSignatureHandler = new PdfSignatureHandler( new DefaultSigner(Program.GetCertificate()), null, options);
pdfSignatureHandler.AttachToDocument(pdfDocument);
pdfDocument.Save(text);
}
private static void SignExisting()
{
string text = string.Format("SignExisting.pdf", new object[0]);
PdfDocument pdfDocument = PdfReader.Open("TestFiles\\doc1.pdf");
PdfSignatureOptions options = new PdfSignatureOptions
{
ContactInfo = "Contact Info",
Location = "Paris",
Reason = "Test signatures",
Rectangle = new XRect(36.0, 735.0, 200.0, 50.0),
AppearanceHandler = new Program.SignAppearenceHandler()
};

PdfSignatureHandler pdfSignatureHandler = new PdfSignatureHandler(new DefaultSigner(Program.GetCertificate()), null, options);
pdfSignatureHandler.AttachToDocument(pdfDocument);
pdfDocument.Save(text);
}

private static X509Certificate2 GetCertificate()
{
// add yours here
return new X509Certificate2("TestFiles\\myself.pfx", "1234", X509KeyStorageFlags.Exportable);
}

private class SignAppearenceHandler : ISignatureAppearanceHandler
{
private XImage Image = XImage.FromFile("TestFiles\\logo.jpg");
public void DrawAppearance(XGraphics gfx, XRect rect)
{
XColor empty = XColor.Empty;
string text = "Signed by Napoleon \nLocation: Paris \nDate: " + DateTime.Now.ToString();
XFont font = new XFont("Verdana", 7.0, XFontStyle.Regular);
XTextFormatter xTextFormatter = new XTextFormatter(gfx);
int num = this.Image.PixelWidth / this.Image.PixelHeight;
XPoint xPoint = new XPoint(0.0, 0.0);
bool flag = this.Image != null;
if (flag)
{
gfx.DrawImage(this.Image, xPoint.X, xPoint.Y, rect.Width / 4.0, rect.Width / 4.0 / (double)num);
xPoint = new XPoint(rect.Width / 4.0, 0.0);
}
xTextFormatter.DrawString(text, font, new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)), new XRect(xPoint.X, xPoint.Y, rect.Width - xPoint.X, rect.Height), XStringFormats.TopLeft);
}
}
}
}
23 changes: 23 additions & 0 deletions SignatureTestConsole/SignatureTestConsole.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\PdfSharpCore\PdfSharpCore.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestFiles\doc1.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestFiles\logo.JPG">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file added SignatureTestConsole/TestFiles/doc1.pdf
Binary file not shown.
Binary file added SignatureTestConsole/TestFiles/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ed8d9fb

Please sign in to comment.