Professional Documents
Culture Documents
PDF Merging With Digital Signature and Password Protection
PDF Merging With Digital Signature and Password Protection
PDF Merging With Digital Signature and Password Protection
Password Protection
Gunaprasad Shetty, 7 Jun 2017
1 2 3 4 5
4.26/5 - 10 votes
μ 4.00, σa 1.98 [?]
Introduction
The tip will help you understand how a Word document can be converted to PDF and merge it as
single PDF with a digital signature for the secured transport of PDF.
Background
This tip will give a basic idea to merge the PDFs into a single PDF from a Word doc, which can
then be digitally signed using AsymmetricKey values by using various DLLs in conjunction.
Replacing the word in the Word document and converting into PDF
Merging the list of PDF into Single PDF using Spire.Pdf
Digitally signing the PDF and placement of Signature in the PDF
Password protection of the signed PDF document
1. This code provides the demonstration of how to merge the set of PDF documents into a single
PDF
2. Digitally sign the merged PDF document
3. Password protection of the digitally signed PDF document.
Below is the list of DLLs which are used, please include this essential DLL into the source code
before using the code.
Pkcs12Store pk12;
//First we'll read the certificate file and declare the certificate variable,
//a pfx file will be used and a password will be provided to validate the pfx file.
try
{
pk12 = new Pkcs12Store(new FileStream(txtSignature.Text, FileMode.Open, FileAccess.Read),
txtPassword.Text.ToCharArray());
}
catch (Exception ex)
{
MessageBox.Show("Signature Passsword incorrect" + ex.Message);
return;
}
These parameters would be used by the Pkcs12Store to access the certificate having a pfx
extension. This certificate will be used later for digitally signing the document in the below code.
Part 2 - Loading the Document and Replacing the Values from the
Database
aDoc.Activate();
string text = wordApp.ActiveDocument.Content.Text;
Word.Find fnd = wordApp.ActiveDocument.Content.Find;
fnd.ClearFormatting();
fnd.Replacement.ClearFormatting();
fnd.Forward = true;
fnd.Wrap = Word.WdFindWrap.wdFindContinue;
fnd.Text = "[##FormName]";
fnd.Replacement.Text = formName;
fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);
fnd.Text = "[##COMPANY]";
fnd.Replacement.Text =
((System.Data.DataRowView)(cmbEntity.SelectedItem)).Row.ItemArray[2].ToString();
fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);
fnd.Text = "[##NAME]";
fnd.Replacement.Text = Convert.ToString(dr[0]["RES_DESC"]);
fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);
fnd.Text = "[##EMP_CODE]";
fnd.Replacement.Text = Convert.ToString(row["Emp_Code"]);
fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);
// // Any source code blocks look like this //
fnd.Text = "[##EMP_PAN]";
if (!String.IsNullOrEmpty(Convert.ToString(row["PAN_Number"])))
fnd.Replacement.Text = Convert.ToString(row["PAN_Number"]);
else
fnd.Replacement.Text = "PANNOTAVBL";
fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);
fnd.Text = "[##EMP_DESG]";
fnd.Replacement.Text = Convert.ToString(dr[0]["RES_DESIGNATION"]);
fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);
fnd.Text = "[##FINANCIAL_YEAR]";
fnd.Replacement.Text = Convert.ToString(cmbYear.SelectedItem.ToString());
fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);
fnd.Text = "[##ASSESMENT_YEAR]";
The above code reads the Doc Template, replaces the values from the database and converts it
into the document that would be used by PDF conversion object.
}
finally
{
}
#endregion merging of documents
}
The above code provides a small example of digitally signing and merging PDFs. Developers can
use them accordingly for their use with modification according to the requirement.
<a
href="https://pubads.g.doubleclick.net/gampad/jump?iu=/6839/lqm.codeproject.site/Web-
Development/ASP-
NET/Howto&sz=300x250&c=480156"&am
p;amp;amp;gt;<img
src="https://pubads.g.doubleclick.net/gampad/jump?iu=/6839/lqm.codeproject.site/Web-
Development/ASP-
NET/Howto&sz=300x250&c=480156"
width="300px" height="250px"
target="_blank"/></a>
License
This article, along with any associated source code and files, is licensed under The Code Project
Open License (CPOL)
Share