iTextSharp.LGPLv2.Core icon indicating copy to clipboard operation
iTextSharp.LGPLv2.Core copied to clipboard

Incorrect rendering of text fields

Open arquitecturatic opened this issue 4 years ago • 0 comments

Summary of the issue

There is an error when try to fill the Acrobat Form. Attached the templates and the results after filled.

Environment

iTextSharp.LGPLv2.Core version: 1.7.5
.NET Core SDK version: .Net Standard 2.0
IDE: VS 2019

Example code/Steps to reproduce:

var workStream = new MemoryStream();
var reader = new PdfReader(@"R:\\template.pdf");
var stamper = new PdfStamper(reader, workStream);

stamper.AcroFields.SetField("{NUMERO_DIAS_VISTA2}", "2039");

stamper.Writer.CloseStream = false;
stamper.Close();
reader.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
File.WriteAllBytes(@"r:\demo1.pdf", byteInfo);

Fix

diff --git "a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs" "b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs"
index dd7b950..0667b5d 100644
--- "a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs"
+++ "b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs"
@@ -2502,7 +2502,17 @@ namespace iTextSharp.text.pdf
                     if (map.ContainsKey(k))
                     {
                         Tokens.Seek(address[k]);
-                        PdfObject obj = ReadPrObject();
+                        Tokens.NextToken();
+                        PdfObject obj;
+                        if (Tokens.TokenType == PrTokeniser.TK_NUMBER)
+                        {
+                            obj = new PdfNumber(Tokens.StringValue);
+                        }
+                        else
+                        {
+                            Tokens.Seek(address[k]);
+                            obj = ReadPrObject();
+                        }
                         _xrefObj[objNumber[k]] = obj;
                     }
                 }

Output:

template.pdf filled_fixed.pdf filled_175.pdf

arquitecturatic avatar Mar 14 '22 16:03 arquitecturatic