Skip to content

Commit 1f887e9

Browse files
author
Michael Chourdakis
committed
Better Visible Signature stuff
1 parent 48eeff1 commit 1f887e9

File tree

4 files changed

+85
-16
lines changed

4 files changed

+85
-16
lines changed

AdES.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ class AdES
8181
struct PDFSIGNVISIBLE
8282
{
8383
std::string t;
84-
int left = 1;
85-
int top = 15;
86-
int fs = 5;
87-
int wi = 70;
84+
int leftp = 1;
85+
int topp = 1;
86+
int wip = 50;
87+
int botp = 5;
8888
};
8989

9090
struct PDFSIGNPARAMETERS

AdES.vcxproj.FileListAbsolute.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
F:\tools\ades\AdES.lib
2+
F:\TOOLS\AdES\AdES.pdb

library.cpp

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,6 +3547,37 @@ HRESULTERROR AdES::PDFSign(LEVEL levx, const char* d, DWORD sz, const std::vecto
35473547
RefObject->content.Serialize(strref);
35483548
strref.resize(strref.size() + 1);
35493549

3550+
// Find media box for visible signature
3551+
float MediaBoxWidth = 0, MediaBoxHeight = 0;
3552+
for (auto& co : RefObject->content.Contents)
3553+
{
3554+
if (co.Name == "MediaBox")
3555+
{
3556+
if (!co.Contents.empty())
3557+
{
3558+
if (co.Contents.begin()->Type == PDF::INXTYPE::TYPE_ARRAY)
3559+
{
3560+
auto spl = split(co.Contents.begin()->Value, ' ');
3561+
if (spl.size() >= 4)
3562+
{
3563+
for (auto& e : spl)
3564+
{
3565+
if (MediaBoxWidth == 0)
3566+
{
3567+
MediaBoxWidth = atof(e.c_str());
3568+
if (MediaBoxWidth > 0)
3569+
continue;
3570+
}
3571+
if (MediaBoxHeight == 0)
3572+
MediaBoxHeight = atof(e.c_str());
3573+
}
3574+
3575+
}
3576+
}
3577+
}
3578+
}
3579+
}
3580+
35503581
// A Test
35513582
/* char* a55 = "<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 4 0 R/FAdESFont 14 0 R>>>>/Contents [15 0 R 5 0 R]/Annots[8 0 R]>>";
35523583
strref.resize(strlen(a55) + 1);
@@ -3559,10 +3590,18 @@ HRESULTERROR AdES::PDFSign(LEVEL levx, const char* d, DWORD sz, const std::vecto
35593590
PDF::AddCh(res, "\n");
35603591
PDF::astring vSignatureDescriptor;
35613592

3593+
int sLeft = MediaBoxWidth * (Params.pdfparams.Visible.leftp / 100.0f);
3594+
int sTop = MediaBoxHeight * ((100 - Params.pdfparams.Visible.topp) / 100.0f);
3595+
int sWi = MediaBoxWidth * (Params.pdfparams.Visible.wip / 100.0f);
3596+
int sHe = MediaBoxHeight * ((100 - Params.pdfparams.Visible.botp) / 100.0f);
35623597

35633598
if (!Params.pdfparams.Visible.t.empty())
35643599
{
3565-
vSignatureDescriptor.Format("%llu 0 obj\n<</F 132/Type/Annot/Subtype/Widget/Rect[%i %i %i %i]/FT/Sig/DR<<>>/T(Signature%llu)/V %llu 0 R/P %llu 0 R/AP<</N %llu 0 R>>>>\nendobj\n", iDescribeSignature, (int)Params.pdfparams.Visible.left, (int)Params.pdfparams.Visible.top - 2, (int)Params.pdfparams.Visible.left + (int)Params.pdfparams.Visible.wi, (int)Params.pdfparams.Visible.top + (int)Params.pdfparams.Visible.fs + 2, CountExistingSignatures + 1, iSignature, iPage, iXOBject);
3600+
vSignatureDescriptor.Format("%llu 0 obj\n<</F 132/Type/Annot/Subtype/Widget/Rect[%i %i %i %i]/FT/Sig/DR<<>>/T(Signature%llu)/V %llu 0 R/P %llu 0 R/AP<</N %llu 0 R>>>>\nendobj\n",
3601+
iDescribeSignature, sLeft,
3602+
sTop,
3603+
sWi,
3604+
sHe, CountExistingSignatures + 1, iSignature, iPage, iXOBject);
35663605
}
35673606
else
35683607
vSignatureDescriptor.Format("%llu 0 obj\n<</F 132/Type/Annot/Subtype/Widget/Rect[0 0 0 0]/FT/Sig/DR<<>>/T(Signature%llu)/V %llu 0 R/P %llu 0 R/AP<</N %llu 0 R>>>>\nendobj\n", iDescribeSignature, CountExistingSignatures + 1, iSignature, iPage, iXOBject);
@@ -3679,22 +3718,42 @@ HRESULTERROR AdES::PDFSign(LEVEL levx, const char* d, DWORD sz, const std::vecto
36793718
// Auto
36803719
if (Params.pdfparams.Visible.t == "auto" && Certificates.size() > 0)
36813720
{
3682-
std::vector<char> di(1000);
3683-
CertGetNameStringA(
3721+
std::vector<wchar_t> di(1000);
3722+
CertGetNameString(
3723+
Certificates[0].cert.cert,
3724+
CERT_NAME_EMAIL_TYPE,
3725+
0,
3726+
NULL,
3727+
di.data(),
3728+
1000);
3729+
std::wstring e1 = di.data();
3730+
CertGetNameString(
36843731
Certificates[0].cert.cert,
36853732
CERT_NAME_SIMPLE_DISPLAY_TYPE,
36863733
0,
36873734
NULL,
36883735
di.data(),
36893736
1000);
3690-
if (!strlen(di.data()))
3691-
Params.pdfparams.Visible.t = "PAdES Signature";
3692-
else
3693-
{
3694-
Params.pdfparams.Visible.t = "PAdES Signature: ";
3695-
Params.pdfparams.Visible.t += di.data();
3696-
}
3697-
3737+
std::wstring e2 = di.data();
3738+
3739+
std::wstring tt;
3740+
if (levx == AdES::LEVEL::CMS)
3741+
tt = L"Digital Signature: RSA/";
3742+
if (levx >= AdES::LEVEL::B)
3743+
tt = L"Digital Signature: PAdES-B/";
3744+
if (levx >= AdES::LEVEL::T)
3745+
tt = L"Digital Signature: PAdES-B-T/";
3746+
if (levx > AdES::LEVEL::XL)
3747+
tt = L"Digital Signature: PAdES-B-LT/";
3748+
3749+
tt += e1;
3750+
tt += L" ";
3751+
tt += e2;
3752+
3753+
std::vector<char> xd(10000);
3754+
WideCharToMultiByte(CP_UTF8, 0, tt.c_str(), tt.length(), xd.data(), 10000, 0, 0);
3755+
3756+
Params.pdfparams.Visible.t = xd.data();
36983757
// Find Width
36993758
}
37003759

@@ -3713,7 +3772,13 @@ HRESULTERROR AdES::PDFSign(LEVEL levx, const char* d, DWORD sz, const std::vecto
37133772
vafter += vVisA3;
37143773

37153774
PDF::astring vv1;
3716-
vv1.Format("BT\n%i %i TD\n/F1 %i Tf\n(%s) Tj\nET\n", Params.pdfparams.Visible.left, Params.pdfparams.Visible.top, Params.pdfparams.Visible.fs,Params.pdfparams.Visible.t.c_str());
3775+
3776+
float SigFS = 5;
3777+
if (MediaBoxWidth > 0 && MediaBoxHeight > 0)
3778+
SigFS *= (MediaBoxHeight / MediaBoxWidth);
3779+
3780+
float hmid = fabs(sTop - sHe)/2.0f;
3781+
vv1.Format("BT\n%i %i TD\n/FAdESFont %i Tf\n(%s) Tj\nET\n", sLeft,(int)(sTop - hmid),(int) SigFS,Params.pdfparams.Visible.t.c_str());
37173782
long long lele = vv1.length();
37183783
vVis1.Format("%llu 0 obj\n<</Length %llu>>stream\n%s\nendstream\nendobj\n", iVis1, lele,vv1.c_str());
37193784
xrefs[iVis1] = vafter.size() + res.size() + 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
F:\TOOLS\AdES\test\test.exe
2+
F:\TOOLS\AdES\test\test.pdb

0 commit comments

Comments
 (0)