Skip to content

Commit a426be8

Browse files
committed
Support UTF-8 encoded strings introduced in PDF 2.0
# Conflicts: # src/main/java/org/sejda/sambox/cos/COSString.java
1 parent 9c64747 commit a426be8

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/main/java/org/sejda/sambox/cos/COSString.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ public String getString()
125125
}
126126
}
127127

128+
// (PDF 2.0)
129+
if ((bytes.length >= 3) && ((bytes[0] & 0xff) == 0xEF && (bytes[1] & 0xff) == 0xBB
130+
&& (bytes[2] & 0xff) == 0xBF))
131+
{
132+
133+
// UTF-8
134+
return new String(bytes, 3, bytes.length - 3, StandardCharsets.UTF_8);
135+
}
136+
128137
// otherwise use PDFDocEncoding
129138
return PDFDocEncoding.toString(bytes);
130139
}

src/test/java/org/sejda/sambox/cos/COSStringTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.IOException;
2626
import java.io.UnsupportedEncodingException;
27+
import java.nio.ByteBuffer;
2728
import java.nio.charset.StandardCharsets;
2829

2930
import org.junit.Test;
@@ -94,7 +95,7 @@ public void asciiParseLiteral() throws UnsupportedEncodingException
9495
}
9596

9697
@Test
97-
public void unitocde8BitsParseLiteral() throws UnsupportedEncodingException
98+
public void unitocde8BitsParseLiteral()
9899
{
99100
/** En français où les choses sont accentués. En español, así */
100101
String text8Bit = "En fran\u00e7ais o\u00f9 les choses sont accentu\u00e9s. En espa\u00f1ol, as\u00ed";
@@ -159,15 +160,33 @@ private static String createHex(String str)
159160
return sb.toString().toUpperCase();
160161
}
161162

163+
@Test
164+
public void utf8EncodedStrings()
165+
{
166+
String text = "Det är kallt vid -30°";
167+
byte[] utf8Bytes = text.getBytes(StandardCharsets.UTF_8);
168+
ByteBuffer buffer = ByteBuffer.allocate(3 + utf8Bytes.length);
169+
// Put the BOM bytes (0xEF, 0xBB, 0xBF)
170+
buffer.put((byte) 0xEF);
171+
buffer.put((byte) 0xBB);
172+
buffer.put((byte) 0xBF);
173+
174+
//Put the original data
175+
buffer.put(utf8Bytes);
176+
177+
assertEquals(text, new COSString(buffer.array()).getString());
178+
}
179+
162180
/**
163181
* PDFBOX-3881: Test that if String has only the BOM, that it be an empty string.
164-
*
182+
*
165183
* @throws IOException
166184
*/
167185
@Test
168186
public void testEmptyStringWithBOM() throws IOException
169187
{
170188
assertTrue(COSString.parseHex("FEFF").getString().isEmpty());
171189
assertTrue(COSString.parseHex("FFFE").getString().isEmpty());
190+
assertTrue(COSString.parseHex("EFBBBF").getString().isEmpty());
172191
}
173192
}

0 commit comments

Comments
 (0)