pub struct PdfString(pub Vec<u8>);Expand description
PDF String object - Text data in PDF files.
PDF strings can contain arbitrary binary data and use various encodings.
They can be written as literal strings (text) or hexadecimal strings <48656C6C6F>.
§Encoding
String encoding depends on context:
- Text strings: Usually PDFDocEncoding or UTF-16BE
- Font strings: Encoding specified by the font
- Binary data: No encoding, raw bytes
§Example
use oxidize_pdf::parser::objects::PdfString;
// Create from UTF-8
let string = PdfString::new(b"Hello World".to_vec());
// Try to decode as UTF-8
if let Ok(text) = string.as_str() {
println!("Text: {}", text);
}Tuple Fields§
§0: Vec<u8>Implementations§
Source§impl PdfString
impl PdfString
Sourcepub fn as_str(&self) -> Result<&str, Utf8Error>
pub fn as_str(&self) -> Result<&str, Utf8Error>
Get as UTF-8 string if possible.
Attempts to decode the string bytes as UTF-8. Note that PDF strings may use other encodings.
§Returns
Ok(&str) if valid UTF-8, Err otherwise.
§Example
use oxidize_pdf::parser::objects::PdfString;
let string = PdfString::new(b"Hello".to_vec());
assert_eq!(string.as_str(), Ok("Hello"));
let binary = PdfString::new(vec![0xFF, 0xFE]);
assert!(binary.as_str().is_err());Sourcepub fn to_text(&self) -> String
pub fn to_text(&self) -> String
Decode as a PDF text string (ISO 32000-1 §7.9.2.2).
A text string is either UTF-16BE introduced by a 0xFE 0xFF byte order
mark, or PDFDocEncoding. Without a BOM this decodes through the WinAnsi
(Windows-1252) table, which agrees with PDFDocEncoding across the Latin
letters and diverges only where few real documents go: PDFDocEncoding
puts typographic punctuation in 0x80..=0x9F in a different order than
WinAnsi does, and maps 0xA0 to € where WinAnsi has a no-break space.
Producers that need those characters emit the BOM. Swapping in the full
PDFDocEncoding table would only change the reading of those slots.
Use this for entries a PDF defines as text — /Title, /Author,
/ActualText. Entries that are binary — /U, /O, /Perms, /ID —
must be read with as_bytes: decoding them as text and
re-encoding the result changes their content (issue #459).
§Example
use oxidize_pdf::parser::objects::PdfString;
// PDFDocEncoding
assert_eq!(PdfString::new(vec![b'a', 0xF1, b'o']).to_text(), "año");
// UTF-16BE with a byte order mark
let utf16 = vec![0xFE, 0xFF, 0x00, b'A', 0x00, 0xF1, 0x00, b'o'];
assert_eq!(PdfString::new(utf16).to_text(), "Año");