我正在尝试将签名字段添加到现有的PDF中,但是出现两个错误,这是我的代码
private static bool CreateForm(string inputPDF, float x, float y, float w, float h, float angle, bool allPages)
{
try
{
PdfReader reader = new PdfReader(inputPDF);
using (FileStream os = new FileStream(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar + "Temp" + Path.DirectorySeparatorChar + "t0.pdf", FileMode.Create, FileAccess.Write))
{
PdfStamper st0 = PdfStamper.CreateSignature(reader, os, '\0', Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar + "Temp" + Path.DirectorySeparatorChar + "t1.pdf", true);
PdfFormField field = PdfFormField.CreateSignature(st0.Writer);
field.FieldName = "FIRMA";
field.SetWidget(new Rectangle(x, y, x + w, y + h), PdfAnnotation.HIGHLIGHT_OUTLINE);
field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
field.Rotate = Convert.ToInt32(Math.Round(angle));
if (allPages)
for (int i = 1; i <= reader.NumberOfPages; i++)
st0.AddAnnotation(field, i);
else
st0.AddAnnotation(field, reader.NumberOfPages);
st0.FormFlattening = true;
st0.Writer.CloseStream = true;
//st0.Close();
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
return true;
}
现在,如果我关闭压模st0.Close()
,则会收到错误消息“必须关闭签名”,否则,如果不关闭它,则结果文件为空(0 B);在没有st0.FormFlattening = true;
和没有st0.Writer.CloseStream = true;
的情况下也尝试过,但我找不到任何有效的组合。
建议?