Am 23.07.2010 20:35, schrieb Robson Bortoleto:
I'm trying to update a database of PDF files and use a newly generated ID to 
identify them (I had problems with ID duplicity caused by an older program). 
The problem is that the result is wrong. The correct ID on the PDF file should 
look like :

/ID [<c8039fd24c97d00fbd27edd68af8e0ef><6b63a249e40a472e9e597aff2c9d8863>]

And the result of my code is :

/ID[ (c8039fd24c97d00fbd27edd68af8e0ef) (6b63a249e40a472e9e597aff2c9d8863)]

I tried two different approachs and both gave the same result (see them below)

Does anyone know what I'm doing wrong ?

There are to kinds of Strings. "Literal Strings" wrote with ( ) and "Hexadezimal Strings" wrote with < >.

The problem is, that pdfbox don't give you the chance to force write hexadezimal Strings as you wish in this situation.

Way 1:
I don't test it, but if you convert each byte-pair (2 bytes) in a char, then convert the char[] in a String and then convert it in a new COSString, maybe you will then get a hexadezimal String.

Way 2:
Wait for a patch or patch it yourself.

I append a patch to that mail, that will add a new method "public void setForceHexForm(boolean v)"


Thanks

Rob

Regards
Thomas
Index: COSString.java
===================================================================
--- COSString.java      (Revision 956459)
+++ COSString.java      (Arbeitskopie)
@@ -21,10 +21,9 @@
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 
+import org.apache.pdfbox.exceptions.COSVisitorException;
 import org.apache.pdfbox.persistence.util.COSHEXTable;
 
-import org.apache.pdfbox.exceptions.COSVisitorException;
-
 /**
  * This represents a string object in a PDF document.
  *
@@ -83,6 +82,10 @@
      */
     private boolean forceLiteralForm = false;
 
+    /**
+     * Forces the string to be serialized in hex form but not literal form.
+     */
+    private boolean forceHexForm = false;
 
     /**
      * Constructor.
@@ -166,6 +169,18 @@
     }
 
     /**
+     * Forces the string to be written in hexadecimal form instead of literal 
form.
+     * 
+     * @param v if v is true the string will be written in hexadecimal form 
otherwise it will be written in literal if
+     *          necessary.
+     */
+
+    public void setForceHexForm(boolean v)
+    {
+      forceHexForm = v;
+    }
+    
+    /**
      * This will create a COS string from a string of hex characters.
      *
      * @param hex A hex string.
@@ -305,6 +320,7 @@
     /**
      * {...@inheritdoc}
      */
+    @Override
     public String toString()
     {
         return "COSString{" + this.getString() + "}";
@@ -328,7 +344,7 @@
             //outside the ASCII range.
             outsideASCII = bytes[i] <0;
         }
-        if( !outsideASCII || forceLiteralForm )
+        if ((!outsideASCII || forceLiteralForm) && !forceHexForm)
         {
             output.write(STRING_OPEN);
             for( int i=0; i<length; i++ )
@@ -341,7 +357,7 @@
                     case '\\':
                     {
                         output.write(ESCAPE);
-                        output.write(b);
+                        output.write((byte)b);
                         break;
                     }
                     case 10: //LF
@@ -371,7 +387,7 @@
                     }
                     default:
                     {
-                        output.write( b );
+                        output.write( (byte)b );
                     }
                 }
             }
@@ -397,6 +413,7 @@
      * @return any object, depending on the visitor implementation, or null
      * @throws COSVisitorException If an error occurs while visiting this 
object.
      */
+    @Override
     public Object accept(ICOSVisitor visitor) throws COSVisitorException
     {
         return visitor.visitFromString( this );
@@ -405,6 +422,7 @@
     /**
      * {...@inheritdoc}
      */
+    @Override
     public boolean equals(Object obj)
     {
         if (obj instanceof COSString)
@@ -418,6 +436,7 @@
     /**
      * {...@inheritdoc}
      */
+    @Override
     public int hashCode()
     {
         return getString().hashCode();

Reply via email to