Skip to content

Commit 210189d

Browse files
Merge pull request #64 from datalogics/develop
Develop to main
2 parents 5131740 + d4274fd commit 210189d

File tree

12 files changed

+292
-66
lines changed

12 files changed

+292
-66
lines changed

.github/workflows/test-net-framework-samples.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on:
1212

1313
jobs:
1414
test-samples:
15-
runs-on: windows-2019
15+
runs-on: windows-2022
1616
defaults:
1717
run:
1818
shell: bash
@@ -93,7 +93,8 @@ jobs:
9393
'Other/StreamIO/',
9494
'Printing/PrintPDF/',
9595
'Printing/PrintPDFGUI/',
96-
'Security/AddDigitalSignature/',
96+
'Security/AddDigitalSignatureCMS/',
97+
'Security/AddDigitalSignatureRFC3161/',
9798
'Security/AddRegexRedaction/',
9899
'Security/Redactions/',
99100
'Text/AddGlyphs/',

ContentModification/CreateLayer/CreateLayer.cs

Lines changed: 109 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1+
using Datalogics.PDFL;
12
using System;
23
using System.Collections.Generic;
3-
using System.Text;
4-
using Datalogics.PDFL;
54

65
/*
7-
* This sample adds an Optional Content Group (a layer) to a PDF document and
8-
* then adds an image to that layer.
6+
* This sample adds Optional Content Groups (layers) to a PDF document and
7+
* then adds Content to those layers.
98
*
109
* The related ChangeLayerConfiguration program makes layers visible or invisible.
1110
*
12-
* You can toggle back and forth to make the layer (the duck image) visible or invisible
13-
* in the PDF file. Open the output PDF document in a PDF Viewer, click View and select
14-
* Show/Hide. Select Navigation Panes and Layers to display the layers in the PDF file.
15-
* Click on the box next to the name of the layer.
11+
* You can toggle back and forth to make a layer visible or invisible
12+
* in a PDF Viewer.
1613
*
1714
*/
1815
namespace CreateLayer
@@ -38,70 +35,129 @@ static void Main(string[] args)
3835

3936
Console.WriteLine("Input file: " + sInput + ", writing to " + sOutput);
4037

41-
Document doc = new Document(sInput);
42-
43-
Console.WriteLine("Opened a document.");
44-
45-
Page pg = doc.GetPage(0);
46-
Image img = (pg.Content.GetElement(0) as Image);
47-
48-
// Containers, Forms and Annotations can be attached to an
49-
// OptionalContentGroup; other content (like Image) can
50-
// be made optional by placing it inside a Container
51-
Container container = new Container();
52-
container.Content = new Content();
53-
container.Content.AddElement(img);
38+
using (Document doc = new Document(sInput))
39+
{
40+
Console.WriteLine("Opened a document.");
41+
42+
using (Page pg = doc.GetPage(0))
43+
{
44+
Image image = (Image)pg.Content.GetElement(0);
45+
image.Matrix = new Matrix(image.Matrix.A * .5, 0, 0, image.Matrix.D * .5, image.Matrix.H, image.Matrix.V);
46+
47+
Image image2 = new Image(Library.ResourceDirectory + "Sample_Input/Image.png");
48+
49+
Text text = new Text();
50+
Matrix matrix = new Matrix();
51+
Font font = new Font("Helvetica");
52+
GraphicState graphicState = new GraphicState();
53+
TextState textState = new TextState();
54+
55+
matrix.A = 42;
56+
matrix.D = 22;
57+
matrix.H = 72;
58+
matrix.V = 72;
59+
60+
TextRun textRun = new TextRun("sample text", font, graphicState, textState, matrix);
61+
text.AddRun(textRun);
62+
63+
Text text2 = new Text();
64+
65+
matrix.A = 30;
66+
matrix.D = 30;
67+
matrix.H = 72;
68+
matrix.V = 288;
69+
70+
TextRun textRun2 = new TextRun("Text definition provided here", font, graphicState, textState, matrix);
71+
text2.AddRun(textRun2);
72+
73+
// Containers, Forms and Annotations can be attached to an
74+
// OptionalContentGroup; other content (like Image) can
75+
// be made optional by placing it inside a Container
76+
Container imageContainer = new Container();
77+
imageContainer.Content = new Content();
78+
imageContainer.Content.AddElement(image);
79+
80+
Container imageContainer2 = new Container();
81+
imageContainer2.Content = new Content();
82+
imageContainer2.Content.AddElement(image2);
83+
84+
Container textContainer = new Container();
85+
textContainer.Content = new Content();
86+
textContainer.Content.AddElement(text);
87+
88+
Container textContainer2 = new Container();
89+
textContainer2.Content = new Content();
90+
textContainer2.Content.AddElement(text2);
91+
92+
using (Document newDoc = new Document())
93+
{
94+
using (Page newPage = newDoc.CreatePage(Document.BeforeFirstPage, pg.MediaBox))
95+
{
96+
newPage.Content.AddElement(imageContainer);
97+
newPage.Content.AddElement(imageContainer2);
98+
newPage.Content.AddElement(textContainer);
99+
newPage.Content.AddElement(textContainer2);
100+
101+
// We create new OptionalContentGroups and place them in the OptionalContentConfig.Order array
102+
List<OptionalContentGroup> ocgs = CreateNewOptionalContentGroups(newDoc, new List<string> { "Rubber Ducky", "PNG Logo", "Example Text", "Text Definition" });
103+
104+
AssociateOCGWithContainer(newDoc, ocgs[0], imageContainer);
105+
AssociateOCGWithContainer(newDoc, ocgs[1], imageContainer2);
106+
AssociateOCGWithContainer(newDoc, ocgs[2], textContainer);
107+
AssociateOCGWithContainer(newDoc, ocgs[3], textContainer2);
108+
109+
newPage.UpdateContent();
110+
111+
newDoc.Save(SaveFlags.Full, sOutput);
112+
}
113+
}
114+
}
115+
}
116+
}
117+
}
54118

55-
// We replace the Image with the Container
56-
// (which now holds the image)
57-
pg.Content.RemoveElement(0);
58-
pg.UpdateContent();
119+
public static List<OptionalContentGroup> CreateNewOptionalContentGroups(Document doc, List<string> names)
120+
{
121+
List<OptionalContentGroup> ocgs = new List<OptionalContentGroup>();
59122

60-
pg.Content.AddElement(container);
61-
pg.UpdateContent();
123+
OptionalContentGroup ocg = new OptionalContentGroup(doc, names[0]);
124+
OptionalContentGroup ocg2 = new OptionalContentGroup(doc, names[1]);
125+
OptionalContentGroup ocg3 = new OptionalContentGroup(doc, names[2]);
126+
OptionalContentGroup ocg4 = new OptionalContentGroup(doc, names[3]);
62127

63-
// We create a new OptionalContentGroup and place it in the
64-
// OptionalContentConfig.Order array
65-
OptionalContentGroup ocg = CreateNewOptionalContentGroup(doc, "Rubber Ducky");
128+
ocgs.Add(ocg);
129+
ocgs.Add(ocg2);
130+
ocgs.Add(ocg3);
131+
ocgs.Add(ocg4);
66132

67-
// Now we associate the Container with the OptionalContentGroup
68-
// via an OptionalContentMembershipDict. Note that we MUST
69-
// update the Page's content afterwards.
70-
AssociateOCGWithContainer(doc, ocg, container);
71-
pg.UpdateContent();
133+
// Add it to the Order array -- this is required so that it will appear in the 'Layers' panel in a PDF Viewer.
134+
OptionalContentOrderArray order_list = doc.DefaultOptionalContentConfig.Order;
72135

73-
doc.Save(SaveFlags.Full, sOutput);
74-
}
75-
}
136+
OptionalContentOrderArray grouping = new OptionalContentOrderArray(doc, "Image Grouping");
137+
grouping.Add(new OptionalContentOrderLeaf(ocg));
138+
grouping.Add(new OptionalContentOrderLeaf(ocg2));
76139

77-
// Create an OptionalContentGroup with a given name, and add it to the
78-
// default OptionalContentConfig's Order array.
79-
public static OptionalContentGroup CreateNewOptionalContentGroup(Document doc, string name)
80-
{
81-
// Create an OptionalContentGroup
82-
OptionalContentGroup ocg = new OptionalContentGroup(doc, name);
140+
OptionalContentOrderArray grouping2 = new OptionalContentOrderArray(doc, "Text Grouping");
141+
grouping2.Add(new OptionalContentOrderLeaf(ocg3));
142+
grouping2.Add(new OptionalContentOrderLeaf(ocg4));
83143

84-
// Add it to the Order array -- this is required so that the OptionalContentGroup
85-
// will appear in the 'Layers' control panel in a PDF Viewer. It will appear in
86-
// the control panel with the name given in the OptionalContentGroup constructor.
87-
OptionalContentOrderArray order_list = doc.DefaultOptionalContentConfig.Order;
88-
order_list.Insert(order_list.Length, new OptionalContentOrderLeaf(ocg));
144+
order_list.Insert(order_list.Length, grouping);
145+
order_list.Insert(order_list.Length, grouping2);
89146

90-
return ocg;
147+
return ocgs;
91148
}
92149

93150
// Associate a Container with an OptionalContentGroup via an OptionalContentMembershipDict.
94-
// This function associates a Containter with a single OptionalContentGroup and uses
151+
// This function associates a Container with a single OptionalContentGroup and uses
95152
// a VisibilityPolicy of AnyOn.
96153
public static void AssociateOCGWithContainer(Document doc, OptionalContentGroup ocg, Container cont)
97154
{
98155
// Create an OptionalContentMembershipDict. The options here are appropriate for a
99156
// 'typical' usage; other options can be used to create an 'inverting' layer
100157
// (i.e. 'Display this content when the layer is turned OFF'), or to make the
101158
// Container's visibility depend on several OptionalContentGroups
102-
OptionalContentMembershipDict ocmd = new OptionalContentMembershipDict(doc, new OptionalContentGroup[] {ocg}, VisibilityPolicy.AnyOn);
159+
OptionalContentMembershipDict ocmd = new OptionalContentMembershipDict(doc, new OptionalContentGroup[] { ocg }, VisibilityPolicy.AnyOn);
103160

104-
// Associate the Container with the OptionalContentMembershipDict
105161
cont.OptionalContentMembershipDict = ocmd;
106162
}
107163
}

Samples.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RegexTextSearch", "Text\Reg
180180
EndProject
181181
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TextExtract", "Text\TextExtract\TextExtract.csproj", "{11C4A647-3DDA-41A5-855C-E14EDF1BFFDE}"
182182
EndProject
183-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddDigitalSignature", "Security\AddDigitalSignature\AddDigitalSignature.csproj", "{AB753937-DF3D-4B06-B475-D3B597D32BC5}"
183+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddDigitalSignatureCMS", "Security\AddDigitalSignatureCMS\AddDigitalSignatureCMS.csproj", "{AB753937-DF3D-4B06-B475-D3B597D32BC5}"
184+
EndProject
185+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddDigitalSignatureRFC3161", "Security\AddDigitalSignatureRFC3161\AddDigitalSignatureRFC3161.csproj", "{74FA984E-AB89-475A-9077-9D612DE12AEC}"
184186
EndProject
185187
Global
186188
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -548,6 +550,10 @@ Global
548550
{AB753937-DF3D-4B06-B475-D3B597D32BC5}.Debug|x64.Build.0 = Debug|x64
549551
{AB753937-DF3D-4B06-B475-D3B597D32BC5}.Release|x64.ActiveCfg = Release|x64
550552
{AB753937-DF3D-4B06-B475-D3B597D32BC5}.Release|x64.Build.0 = Release|x64
553+
{74FA984E-AB89-475A-9077-9D612DE12AEC}.Debug|x64.ActiveCfg = Debug|x64
554+
{74FA984E-AB89-475A-9077-9D612DE12AEC}.Debug|x64.Build.0 = Debug|x64
555+
{74FA984E-AB89-475A-9077-9D612DE12AEC}.Release|x64.ActiveCfg = Release|x64
556+
{74FA984E-AB89-475A-9077-9D612DE12AEC}.Release|x64.Build.0 = Release|x64
551557
EndGlobalSection
552558
GlobalSection(SolutionProperties) = preSolution
553559
HideSolutionNode = FALSE

Security/AddDigitalSignature/AddDigitalSignature.cs renamed to Security/AddDigitalSignatureCMS/AddDigitalSignatureCMS.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55

66
/*
77
*
8-
* This sample program demonstrates the use of AddDigitalSignature.
8+
* This sample program demonstrates the use of AddDigitalSignature for CMS signature type.
99
*
1010
* Copyright (c) 2025, Datalogics, Inc. All rights reserved.
1111
*
1212
*/
13-
namespace AddDigitalSignature
13+
namespace AddDigitalSignatureCMS
1414
{
15-
class AddDigitalSignature
15+
class AddDigitalSignatureCMS
1616
{
1717
static void Main(string[] args)
1818
{
19-
Console.WriteLine("AddDigitalSignature Sample:");
19+
Console.WriteLine("AddDigitalSignatureCMS Sample:");
2020

2121
using (new Library())
2222
{
2323
Console.WriteLine("Initialized the library.");
2424

2525
String sInput = Library.ResourceDirectory + "Sample_Input/SixPages.pdf";
2626
String sLogo = Library.ResourceDirectory + "Sample_Input/ducky_alpha.tif";
27-
String sOutput = "DigSig-out.pdf";
27+
String sOutput = "DigSigCMS-out.pdf";
2828

2929
String sDERCert = Library.ResourceDirectory + "Sample_Input/Credentials/DER/RSA_certificate.der";
3030
String sDERKey = Library.ResourceDirectory + "Sample_Input/Credentials/DER/RSA_privKey.der";
@@ -55,6 +55,10 @@ static void Main(string[] args)
5555
sigDoc.SetNonPfxSignerCert(sDERCert, 0, CredentialStorageFmt.OnDisk);
5656
sigDoc.SetNonPfxPrivateKey(sDERKey, 0, CredentialStorageFmt.OnDisk);
5757

58+
// Set the signature type to be used.
59+
// The available types are defined in the SignatureType enum. Default CMS.
60+
sigDoc.DocSignType = SignatureType.CMS;
61+
5862
// Setup the signer information
5963
// (Logo image is optional)
6064
sigDoc.SetSignerInfo(sLogo, 0.5F, "John Doe", "Chicago, IL", "Approval", "Datalogics, Inc.",

Security/AddDigitalSignature/AddDigitalSignature.csproj renamed to Security/AddDigitalSignatureCMS/AddDigitalSignatureCMS.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<Platform Condition=" '$(Platform)' == '' ">x64</Platform>
77
<ProjectGuid>{AB753937-DF3D-4B06-B475-D3B597D32BC5}</ProjectGuid>
88
<OutputType>Exe</OutputType>
9-
<RootNamespace>AddDigitalSignature</RootNamespace>
10-
<AssemblyName>AddDigitalSignature</AssemblyName>
9+
<RootNamespace>AddDigitalSignatureCMS</RootNamespace>
10+
<AssemblyName>AddDigitalSignatureCMS</AssemblyName>
1111
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
@@ -45,7 +45,7 @@
4545
<Reference Include="System.Xml" />
4646
</ItemGroup>
4747
<ItemGroup>
48-
<Compile Include="AddDigitalSignature.cs" />
48+
<Compile Include="AddDigitalSignatureCMS.cs" />
4949
</ItemGroup>
5050
<ItemGroup>
5151
<None Include="App.config" />

Security/AddDigitalSignature/AddDigitalSignature.sln renamed to Security/AddDigitalSignatureCMS/AddDigitalSignatureCMS.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio Version 17
33
VisualStudioVersion = 17.13.35818.85
44
MinimumVisualStudioVersion = 10.0.40219.1
5-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddDigitalSignature", "AddDigitalSignature.csproj", "{3790CE63-DB43-4F16-8226-BDFEFA25BCDD}"
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddDigitalSignatureCMS", "AddDigitalSignatureCMS.csproj", "{3790CE63-DB43-4F16-8226-BDFEFA25BCDD}"
66
EndProject
77
Global
88
GlobalSection(SolutionConfigurationPlatforms) = preSolution
File renamed without changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Datalogics.PDFL;
5+
6+
/*
7+
*
8+
* This sample program demonstrates the use of AddDigitalSignature for RFC3161 timestamp signature type.
9+
*
10+
* Copyright (c) 2025, Datalogics, Inc. All rights reserved.
11+
*
12+
*/
13+
namespace AddDigitalSignatureRFC3161
14+
{
15+
class AddDigitalSignatureRFC3161
16+
{
17+
static void Main(string[] args)
18+
{
19+
Console.WriteLine("AddDigitalSignatureRFC3161 Sample:");
20+
21+
using (new Library())
22+
{
23+
Console.WriteLine("Initialized the library.");
24+
25+
String sInput = Library.ResourceDirectory + "Sample_Input/CreateAcroForm2h.pdf";
26+
27+
String sOutput = "DigSigRFC3161-out.pdf";
28+
29+
if (args.Length > 0)
30+
sInput = args[0];
31+
32+
if (args.Length > 1)
33+
sOutput = args[1];
34+
35+
Console.WriteLine("Input file: " + sInput);
36+
Console.WriteLine("Writing to output: " + sOutput);
37+
38+
using (Document doc = new Document(sInput))
39+
{
40+
using (Datalogics.PDFL.SignDoc sigDoc = new Datalogics.PDFL.SignDoc())
41+
{
42+
// Setup Sign params
43+
sigDoc.FieldID = SignatureFieldID.SearchForFirstUnsignedField;
44+
45+
// Set signing attributes
46+
sigDoc.DigestCategory = DigestCategory.Sha256;
47+
48+
// Set the signature type to be used, RFC3161/TimeStamp.
49+
// The available types are defined in the SignatureType enum. Default CMS.
50+
sigDoc.DocSignType = SignatureType.RFC3161;
51+
52+
// Setup Save params
53+
sigDoc.OutputPath = sOutput;
54+
55+
// Finally, sign and save the document
56+
sigDoc.AddDigitalSignature(doc);
57+
58+
Console.WriteLine();
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)