@@ -3547,6 +3547,37 @@ HRESULTERROR AdES::PDFSign(LEVEL levx, const char* d, DWORD sz, const std::vecto
3547
3547
RefObject->content .Serialize (strref);
3548
3548
strref.resize (strref.size () + 1 );
3549
3549
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
+
3550
3581
// A Test
3551
3582
/* 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]>>";
3552
3583
strref.resize(strlen(a55) + 1);
@@ -3559,10 +3590,18 @@ HRESULTERROR AdES::PDFSign(LEVEL levx, const char* d, DWORD sz, const std::vecto
3559
3590
PDF::AddCh (res, " \n " );
3560
3591
PDF::astring vSignatureDescriptor;
3561
3592
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 );
3562
3597
3563
3598
if (!Params.pdfparams .Visible .t .empty ())
3564
3599
{
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>>>>\n endobj\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>>>>\n endobj\n " ,
3601
+ iDescribeSignature, sLeft ,
3602
+ sTop ,
3603
+ sWi ,
3604
+ sHe , CountExistingSignatures + 1 , iSignature, iPage, iXOBject);
3566
3605
}
3567
3606
else
3568
3607
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>>>>\n endobj\n " , iDescribeSignature, CountExistingSignatures + 1 , iSignature, iPage, iXOBject);
@@ -3679,22 +3718,42 @@ HRESULTERROR AdES::PDFSign(LEVEL levx, const char* d, DWORD sz, const std::vecto
3679
3718
// Auto
3680
3719
if (Params.pdfparams .Visible .t == " auto" && Certificates.size () > 0 )
3681
3720
{
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 (
3684
3731
Certificates[0 ].cert .cert ,
3685
3732
CERT_NAME_SIMPLE_DISPLAY_TYPE,
3686
3733
0 ,
3687
3734
NULL ,
3688
3735
di.data (),
3689
3736
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 ();
3698
3757
// Find Width
3699
3758
}
3700
3759
@@ -3713,7 +3772,13 @@ HRESULTERROR AdES::PDFSign(LEVEL levx, const char* d, DWORD sz, const std::vecto
3713
3772
vafter += vVisA3;
3714
3773
3715
3774
PDF::astring vv1;
3716
- vv1.Format (" BT\n %i %i TD\n /F1 %i Tf\n (%s) Tj\n ET\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\n ET\n " , sLeft ,(int )(sTop - hmid),(int ) SigFS,Params.pdfparams .Visible .t .c_str ());
3717
3782
long long lele = vv1.length ();
3718
3783
vVis1.Format (" %llu 0 obj\n <</Length %llu>>stream\n %s\n endstream\n endobj\n " , iVis1, lele,vv1.c_str ());
3719
3784
xrefs[iVis1] = vafter.size () + res.size () + 1 ;
0 commit comments