我使用iText从ASP.NET网络表单生成带有填充数据的文本字段的pdf。出于某种原因,当生成pdf时,文本字段显示为空,但是当我将光标放在字段上时,文本显示。如果我单击其他位置,则文本字段中的文本会再次消失。这是代码,添加一个名为" Field_0_0"的文本字段。文字" hello1"设置为文本字段,但在单击文本框之前不会显示。在文本字段中获取文本的唯一方法是键入内容,然后在失去焦点后文本停留在那里。
byte[] templatebytes = null;
//Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
Document doc = new Document(iTextSharp.text.PageSize.LETTER);
MemoryStream stream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, stream);
doc.Open();
Paragraph para = new Paragraph("This is my first paragraph");
Phrase pharse = new Phrase("This is my first phrase");
Chunk chunk_ = new Chunk("This is my first chunk");
doc.Add(para);
doc.Add(pharse);
doc.Add(chunk_);
doc.Close();
templatebytes = stream.ToArray();
//==============================================================================================================================================
Stream stream2 = new MemoryStream(templatebytes);
byte[] finalbytes = null;
using (MemoryStream outputPdfStream = new MemoryStream())
{
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(stream2);
iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputPdfStream);
TextField tf1 = new TextField(writer, new iTextSharp.text.Rectangle(20, 20, 200, 100), "Field_0_0");
tf1.DefaultText = " ";
tf1.Options = TextField.MULTILINE | TextField.VISIBLE;
tf1.TextColor = BaseColor.BLACK;
tf1.FontSize = 10;
tf1.BorderWidth = 1;
tf1.BorderColor = BaseColor.BLUE;
pdfStamper.AddAnnotation(tf1.GetTextField(), 1);
pdfStamper.Close();
finalbytes = outputPdfStream.ToArray();
}
//====================================== set data ========================================
Stream stream3 = new MemoryStream(finalbytes);
byte[] finalbytes3 = null;
using (MemoryStream outputPdfStream = new MemoryStream())
{
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(stream3);
iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputPdfStream);
//Set fields with data
iTextSharp.text.pdf.AcroFields form = pdfStamper.AcroFields;
IDictionary<string, iTextSharp.text.pdf.AcroFields.Item> fields = form.Fields;
string value = "Hello1"; //$"Line1{Environment.NewLine}Line2";
form.SetField("Field_0_0", value); //$"Line1{Environment.NewLine}Line2"
pdfStamper.Close();
finalbytes3 = outputPdfStream.ToArray();
}
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=GeneratedPDF.pdf");
Response.AddHeader("Content-Length", finalbytes3.Length.ToString());
Response.BinaryWrite(finalbytes3);
Response.End();
希望我解释了这一点。我无法在线找到解决此问题的解决方案。
由于