From 45f8080b5f1891d72019df533059f6a8cc993b76 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Camilo?= <andre.camilo@premium-minds.com>
Date: Tue, 21 Feb 2012 12:37:53 +0000
Subject: [PATCH] PDFBOX-1231: corrected bug with AcroForm encoding

---
 .../pdmodel/interactive/form/PDAppearance.java     |   56 +++++++-------------
 1 files changed, 20 insertions(+), 36 deletions(-)

diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearance.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearance.java
index c365c54..461009c 100644
--- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearance.java
+++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearance.java
@@ -20,6 +20,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 
 import java.util.ArrayList;
@@ -212,10 +213,11 @@ public class PDAppearance
         // MulitLine check and set
         if ( parent.isMultiline() && apValue.indexOf('\n') != -1 )
         {
-            apValue = convertToMultiLine( apValue );
+        	value = convertToMultiLine( apValue );
+        } else {
+        	value = apValue;
         }
-
-        value = apValue;
+        
         Iterator<COSObjectable> widgetIter = widgets.iterator();
         while( widgetIter.hasNext() )
         {
@@ -300,44 +302,26 @@ public class PDAppearance
                         }
                         ByteArrayOutputStream output = new ByteArrayOutputStream();
                         ContentStreamWriter writer = new ContentStreamWriter( output );
-                        float fontSize = calculateFontSize( pdFont, appearanceStream.getBoundingBox(), tokens, null );
-                        boolean foundString = false;
-                        for( int i=0; i<tokens.size(); i++ )
-                        {
-                            if( tokens.get( i ) instanceof COSString )
-                            {
-                                foundString = true;
-                                COSString drawnString =((COSString)tokens.get(i));
-                                drawnString.reset();
-                                drawnString.append( apValue.getBytes("ISO-8859-1") );
-                            }
-                        }
+                        float fontSize = calculateFontSize( pdFont, appearanceStream.getBoundingBox(), tokens, daTokens );
                         int setFontIndex = tokens.indexOf( PDFOperator.getOperator( "Tf" ));
                         tokens.set( setFontIndex-1, new COSFloat( fontSize ) );
-                        if( foundString )
+                        int bmcIndex = tokens.indexOf( PDFOperator.getOperator( "BMC" ) );
+                        int emcIndex = tokens.indexOf( PDFOperator.getOperator( "EMC" ) );
+
+                        if( bmcIndex != -1 )
                         {
-                            writer.writeTokens( tokens );
+                            writer.writeTokens( tokens, 0, bmcIndex+1 );
                         }
                         else
                         {
-                            int bmcIndex = tokens.indexOf( PDFOperator.getOperator( "BMC" ) );
-                            int emcIndex = tokens.indexOf( PDFOperator.getOperator( "EMC" ) );
-
-                            if( bmcIndex != -1 )
-                            {
-                                writer.writeTokens( tokens, 0, bmcIndex+1 );
-                            }
-                            else
-                            {
-                                writer.writeTokens( tokens );
-                            }
-                            output.write( "\n".getBytes("ISO-8859-1") );
-                            insertGeneratedAppearance( widget, output,
-                                pdFont, tokens, appearanceStream );
-                            if( emcIndex != -1 )
-                            {
-                                writer.writeTokens( tokens, emcIndex, tokens.size() );
-                            }
+                            writer.writeTokens( tokens );
+                        }
+                        output.write( "\n".getBytes("ISO-8859-1") );
+                        insertGeneratedAppearance( widget, output,
+                            pdFont, tokens, appearanceStream );
+                        if( emcIndex != -1 )
+                        {
+                            writer.writeTokens( tokens, emcIndex, tokens.size() );
                         }
                         writeToStream( output.toByteArray(), appearanceStream );
                     }
@@ -353,7 +337,7 @@ public class PDAppearance
     private void insertGeneratedAppearance( PDAnnotationWidget fieldWidget, OutputStream output,
         PDFont pdFont, List tokens, PDAppearanceStream appearanceStream ) throws IOException
     {
-        PrintWriter printWriter = new PrintWriter( output, true );
+        PrintWriter printWriter = new PrintWriter( new OutputStreamWriter(output, "ISO-8859-1"), true );
         float fontSize = 0.0f;
         PDRectangle boundingBox = null;
         boundingBox = appearanceStream.getBoundingBox();
-- 
1.7.5.4

