在使用iTextSharp填写其他字段时,如果需要强制PDF格式化和计算,可以使用以下方法:
PdfStamper
类来填写表单字段,并设置FormFlattening
属性为true
,这将强制PDF格式化。using (PdfReader pdfReader = new PdfReader(inputFile))
{
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outputFile, FileMode.Create)))
{
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("fieldName", "fieldValue");
pdfStamper.FormFlattening = true;
}
}
PdfReader
和PdfStamper
类来填写表单字段,并使用PdfFormField
类的SetFieldFlags
方法设置字段标志位,以强制PDF格式化和计算。using (PdfReader pdfReader = new PdfReader(inputFile))
{
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outputFile, FileMode.Create)))
{
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("fieldName", "fieldValue");
PdfFormField field = pdfFormFields.GetFieldItem("fieldName");
field.SetFieldFlags(PdfFormField.FF_READ_ONLY);
}
}
PdfReader
和PdfStamper
类来填写表单字段,并使用PdfFormField
类的SetWidget
方法设置字段的外观,以强制PDF格式化和计算。using (PdfReader pdfReader = new PdfReader(inputFile))
{
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outputFile, FileMode.Create)))
{
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("fieldName", "fieldValue");
PdfFormField field = pdfFormFields.GetFieldItem("fieldName");
PdfDictionary widget = field.GetWidget(0);
PdfArray bBox = widget.GetAsArray(PdfName.RECT);
float x1 = bBox.GetAsNumber(0).FloatValue;
float y1 = bBox.GetAsNumber(1).FloatValue;
float x2 = bBox.GetAsNumber(2).FloatValue;
float y2 = bBox.GetAsNumber(3).FloatValue;
PdfContentByte canvas = pdfStamper.GetOverContent(widget.GetAsNumber(PdfName.PAGE).IntValue);
canvas.SetColorStroke(BaseColor.BLACK);
canvas.Rectangle(x1, y1, x2 - x1, y2 - y1);
canvas.Stroke();
}
}
以上方法可以强制PDF格式化和计算,并且不涉及到其他云计算品牌商。
领取专属 10元无门槛券
手把手带您无忧上云