setCompressionLevel(0) but wit PDFBox?
itext, java, pdf, pdfbox
Solution
In contrast to iText, PDFBox does not provide a global setting controlling the compression of the PDF. You can control compression on a per-stream basis, though:
I assume you used a `PDPageContentStream` instance to generate the page content stream of your PDF (otherwise please supply code) and instantiated that class using the 2-parameter constructor
public PDPageContentStream( PDDocument document, PDPage sourcePage )
throws IOException
This constructor replaces the current page content of the page (if any) and requests compression.
If you use the 4-parameter constructor instead, you can control these behaviors:
public PDPageContentStream( PDDocument document, PDPage sourcePage,
boolean appendContent, boolean compress ) throws IOException
PS: In a comment you said that with compression deactivated you get
<4C61205374E967616E6F677261706869652064616E7320504446> Tj
but would prefer a regular, not hex-encoded string.
PDFBox here uses the hex-encoded string variant because your text "La Stéganographie dans PDF" contains a character whose character code is outside the ASCII'ish range 0..127, namely the 'é'. `COSString.writePDF` (which is used by `PDPageContentStream.drawString`) in such a case automatically uses the hex-encoded string form.
If you don't want that for your string, you have to construct the string drawing command yourself. Instead of
pageContentStream.drawString("La Stéganographie dans PDF");
you could write something like
pageContentStream.appendRawCommands("(La Stéganographie dans PDF) Tj\n");
Beware: This obviously only works as long as your string only contains character in the ISO-8859-1 range (because `appendRawCommands` underneath retrieves the bytes for that encoding) and the encoding of your font coincides with ISO-8859-1 at least for the characters involved.
Problem
I know that in iText, we have the "setCompressionLevel(0)" command in order to be able to show the file Structure in a more visible way. But now, i am using PDFBox. So how compress level 0 with PDFBox? I created a PDF document with PDFBox containing "hello world" as string. When i open the file structure, i noticed the following stream : ``` stream xœs áÒw3P04RIã24U0¶0UIáÒÈHÍÉÉW(Ï/ÊIÑTÉâr á endStream ``` My problem is which command must be used in order to show the following stream: ``` stream <hello world>Tj endStream ``` Thanks in advance. Liszt this is my code, i also get the same problem while i use the 4 parameters. ``` public static void CreatePdf(String src) throws IOException, COSVisitorException{ PDRectangle rec= new PDRectangle(400,400); PDDocument document= null; document = new PDDocument(); PDPage page = new PDPage(rec); document.addPage(page); PDDocumentInformation info=document.getDocumentInformation(); PDStream stream= new PDStream(document); stream.addCompression(); info.setAuthor("PdfBox"); info.setCreator("Pdf"); info.setSubject("Stéganographie"); info.setTitle("Stéganographie dans les documents PDF"); info.setKeywords("Stéganographie, pdf"); content= new PDPageContentStream(document, page, true, true ); font= PDType1Font.HELVETICA; String texte="La Stéganographie dans PDF"; content.beginText(); content.setFont(font, 12); content.moveTextPositionByAmount(15, 385); // content.appendRawCommands("3 Tr"); content.drawString(texte); content.endText(); content.close(); document.save("doc.pdf"); document.close(); } ```