我有一个 PDF 模板文件,我试图用“MyDocument”的内容填充它。所有字段都很好,但问题是我的 PDF 中的“计算”字段没有刷新,其他字段的格式设置也没有。如何使用 ITextSharp 使计算字段刷新和格式化工作?(我不在乎我是否得到 C# 或 VB.NET 答案)
VB.NET:
Public Shared Sub Serialize(ByVal stmt As MyDocument, ByVal file As FileInfo)
Dim reader As New PdfReader(TemplateFilePath.FullName)
Dim pdfStamper As New PdfStamper(reader, New FileStream(file.FullName, FileMode.Open))
Try
With itsDaDetailFields
.MoveFirst()
While Not .EOF
Dim pdfFieldName As String = NsT(Of String)(!PDFFieldName, Nothing)
If Not String.IsNullOrEmpty(pdfFieldName) Then
Dim value As String = NsT(Of String)(stmt.GetValueFromPDFField(pdfFieldName), Nothing)
If Not String.IsNullOrEmpty(value) Then
pdfStamper.AcroFields.SetField(pdfFieldName, value)
End If
End If
.MoveNext()
End While
End With
Finally
pdfStamper.FormFlattening = False
reader.Close()
pdfStamper.Close()
End Try
End Sub
C#:
public static void Serialize(MyDocument stmt, FileInfo file)
{
PdfReader reader = new PdfReader(TemplateFilePath.FullName);
PdfStamper pdfStamper = new PdfStamper(reader, new FileStream(file.FullName, FileMode.Open));
try {
var _with1 = itsDaDetailFields;
_with1.MoveFirst();
while (!_with1.EOF) {
string pdfFieldName = NsT<string>(_with1["PDFFieldName"], null);
if (!string.IsNullOrEmpty(pdfFieldName)) {
string value = NsT<string>(stmt.GetValueFromPDFField(pdfFieldName), null);
if (!string.IsNullOrEmpty(value)) {
pdfStamper.AcroFields.SetField(pdfFieldName, value);
}
}
_with1.MoveNext();
}
} finally {
pdfStamper.FormFlattening = false;
reader.Close();
pdfStamper.Close();
}
}