spire.pdf 6.12.20已发布。该版本支持通过外部服务进行数字签名并且支持设置pdf包中文件的字段并对文件进行排序。此外还增强了pdf转换到pdf/a 和pdf/a -3b的功能,以及修复了在查找文本中出现的问题。详情请阅读以下信息。
新功能:
- 支持通过外部服务进行数字签名。
void sign()
{
pdfdocument doc = new pdfdocument();
doc.loadfromfile(inputpath);
//load the certificate
x509certificate2 cert = new x509certificate2(certpath, passwd);
custompkcs7signatureformatter custompkcs7signatureformatter = new custompkcs7signatureformatter(cert);
pdfsignature signature = new pdfsignature(doc, doc.pages[0], custompkcs7signatureformatter, "signature0");
signature.bounds = new rectanglef(new pointf(90, 550), new sizef(270, 90));
//set the dispay mode of graphics, if not set any, the default one will be applied
signature.graphicsmode = graphicmode.signdetail;
signature.namelabel = "signer:";
signature.name = "gary";
signature.contactinfolabel = "contactinfo:";
signature.datelabel = "date:";
signature.date = datetime.now;
signature.locationinfolabel = "location:";
signature.locationinfo = "chengdu";
signature.reasonlabel = "reason: ";
signature.reason = "the certificate of this document";
signature.distinguishednamelabel = "dn: ";
signature.documentpermissions = pdfcertificationflags.allowformfill | pdfcertificationflags.forbidchanges;
signature.signdetailsfont = new pdffont(pdffontfamily.timesroman, 10f);
signature.signnamefont = new pdffont(pdffontfamily.courier, 15);
signature.signimagelayout = signimagelayout.none;
//save pdf file.
doc.savetofile(outputpath, spire.pdf.fileformat.pdf);
}
class custompkcs7signatureformatter : ipdfsignatureformatter
{
///
/// if encapsulate is true, a copy of the message will be included in the signature.
///
private bool m_encapsulate = true;
///
/// the signing certificate.
///
private x509certificate2 m_certificate = null;
public dictionary m_parameters = new dictionary();
///
/// parameters for the encoding of the signature.
/// 1.key:filter,string
/// required
/// the name of the preferred signature handler to use when validating this signature.
/// 2.subfilter,string
/// required
/// a name that describes the encoding of the signature value.
/// pdf 1.6 defines the following values for public-key cryptographic signatures: adbe.x509.rsa_sha1, adbe.pkcs7.detached, and adbe.pkcs7.sha1
/// 3.cert,x509certificate2
/// required when subfilter is adbe.x509.rsa_sha1
///
public dictionary parameters
{
get
{ return m_parameters; }
}
///
/// construct a new instance.
///
/// the signing certificate.
///
/// if encapsulate is true, a copy of the message will be included in the signature.
///
public custompkcs7signatureformatter(x509certificate2 certificate)
{
if (null == certificate)
{ throw new argumentnullexception("certificate"); }
m_certificate = certificate;
parameters.add("filter", "adobe.ppkms");
parameters.add("subfilter", m_encapsulate ? "adbe.pkcs7.sha1" : "adbe.pkcs7.detached");
}
///
/// sign.
///
/// the data that contains signature content.
/// the signature
public byte[] sign(byte[] content)
{
cmssigner cmssigner = new cmssigner(m_certificate);
sha1 sha1 = sha1.create();
signedcms signedcms = new signedcms(new contentinfo(m_encapsulate ? sha1.computehash(content) : content), !m_encapsulate);
signedcms.computesignature(cmssigner, true);
byte[] result = signedcms.encode();
return result;
}
}
string[] files = directory.getfiles(@"files");
string inputfile = @"input.pdf";
string outputfile = @"result.pdf";
using (pdfdocument doc = new pdfdocument(inputfile))
{
//添加字段
doc.collection.addcustomfield("no", "序号", spire.pdf.collections.customfieldtype.numberfield);
doc.collection.addfilerelatedfield("filename", "文件名称", spire.pdf.collections.filerelatedfieldtype.filename);
doc.collection.addfilerelatedfield("desc", "文件描述", spire.pdf.collections.filerelatedfieldtype.desc);
doc.collection.addfilerelatedfield("createdate", "创建时间", spire.pdf.collections.filerelatedfieldtype.creationdate);
doc.collection.addfilerelatedfield("moddate", "修改时间", spire.pdf.collections.filerelatedfieldtype.moddate);
doc.collection.addfilerelatedfield("filesize", "文件大小", spire.pdf.collections.filerelatedfieldtype.size);
doc.collection.addcustomfield("filetype", "文件类型", spire.pdf.collections.customfieldtype.textfield);
doc.collection.addcustomfield("filedate", "自定义时间", spire.pdf.collections.customfieldtype.datefield);
//根据字段对附件排序
doc.collection.sort(new string[] { "no", "filename", "desc", "createdate", "moddate", "filesize", "filetype", "filedate" }, new bool[] { false, true, true, true, true, true, true, true });
for (int i = 0; i < files.length; i )
{
pdfattachment attachment = new pdfattachment(files[i]);
doc.collection.addattachment(attachment);
}
int n = 1;
//设置字段值
foreach (pdfattachment att in doc.collection.associatedfiles)
{
att.setfieldvalue("no", n);
att.description = n "文件描述";
att.setfieldvalue("filedate", datetime.today);
att.setfieldvalue("filetype", n "文件");
n ;
}
doc.savetofile(outputfile, fileformat.pdf);
doc.dispose();
}
pdfdocument doc = new pdfdocument();
doc.loadfromfile(inputpath);
pdfattachment attachment = new pdfattachment(attachmentpath);
doc.attachments.add(attachment,doc,spire.pdf.general.pdfattachmentrelationship.alternative);
doc.savetofile(outputpath);
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile(inputfile, fileformat.pdf);
rectanglef rctg = new rectanglef(0, 0, 200, 100);
pdftextfindcollection findcollection = pdf.pages[0].findtext(rctg, "deposit", textfindparameter.wholeword);
pdftextfindcollection findcollectionout = pdf.pages[0].findtext(rctg, "agreement", textfindparameter.wholeword);
foreach (pdftextfind find in findcollection.finds)
{ find.applyhighlight(color.green); }
foreach (pdftextfind findout in findcollectionout.finds)
{ findout.applyhighlight(color.yellow); }
pdf.savetofile(outputfile, fileformat.pdf);
问题修复:
- 修复了转换含附件的pdf到pdf/a-3b不符合规范的问题。
- 修复了转换含动作的pdf到pdf/a不符合规范的问题。
- 修复了获取书签时程序挂起的问题。
- 调整了.net standard平台下的pdftruetypefont类的初始化方法。
- 修复了注释无法删除的问题。
- 修复了添加上下标导致字符间距不正确的问题。
- 修复了查找文本抛“nullreferenceexception”的问题。
- 修复了查找文本失败的问题。
获取spire.pdf 6.12.20,请点击: