Claas Jäger wrote:
Maybe my approach to get the content in the PDF is wrong ? Hints and pointers greatly appreciated...

Have a look at the attachment.

You have sent two PDFs in an earlier mail.
First let's have a look at the pattern:

PdfReader reader1 = new PdfReader("Pattern_only.pdf");
PRStream stream = (PRStream)reader1.getPdfObject(19);
String pattern = new String(PdfReader.getStreamBytes(stream));
System.out.println("Pattern stream:");
System.out.println(pattern);

The result is:
q
0.1199951 0 0 0.1199951 -2.3335571 28.9373322 cm
/CS0 cs 0 0 0 /P0 scn
1 i
/GS0 gs
139 1682.67 m
<snip>
139 1682.67 l
f*
Q

This is PDF syntax. Normally I'd use getStreamBytesRaw instead of getStreamBytes and compare those raw bytes with streams in the other PDF, but this way, you can actually read the syntax (this isn't the case if you take the raw bytes).

I was looking at the streams in the other file like this:
PdfReader reader2 = new PdfReader("Pattern_and_trans_layer.pdf");
int n = reader2.getXrefSize();
PdfObject obj;
for (int i = 0; i < n; i++) {
  obj = reader2.getPdfObject(i);
  if (obj instanceof PRStream) {
    System.out.println("Object " + i + " is a stream:");
System.out.println(new String(PdfReader.getStreamBytes((PRStream)obj)));
  }
}

But none of these streams are identical to the pattern stream.
That's why comparing them at the level of the raw bytes doesn't work.
However, one stream is very similar:

q
0.1199951 0 0 0.1199951 0.5583038 152.3921661 cm
/CS0 cs 0 0 0 /P0 scn
1 i
/GS0 gs
139 1682.67 m
<snip>
139 1682.67 l
f*
Q
q
0.1199951 0 0 0.1199951 -2.8167725 438.1481628 cm
/CS0 cs 1 1 1 /P1 scn
1 i
/GS0 gs
139 1682.67 m
<snip>
139 1682.67 l
f*
Q

I assume you want to get rid of this stream.
That can be done like this:

PRStream toChange = (PRStream)reader2.getPdfObject(33);
toChange.setData(new byte[0]);
PdfStamper stamper = new PdfStamper(reader2, new FileOutputStream("result.pdf"));
stamper.close();

Now you only have to find one extra piece in the puzzle: how do you know which stream has to be removed? That's outside the scope of this list.
--
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;


public class Claas {

        public static void main(String[] args) throws IOException, 
DocumentException {
                PdfReader reader1 = new PdfReader("Pattern_only.pdf");
                PRStream stream = (PRStream)reader1.getPdfObject(19);
                String pattern = new String(PdfReader.getStreamBytes(stream));
                System.out.println("Pattern stream:");
                System.out.println(pattern);
                
                PdfReader reader2 = new 
PdfReader("Pattern_and_trans_layer.pdf");
                int n = reader2.getXrefSize();
                PdfObject obj;
                for (int i = 0; i < n; i++) {
                        obj = reader2.getPdfObject(i);
                        if (obj instanceof PRStream) {
                                System.out.println("Object " + i + " is a 
stream:");
                                System.out.println(new 
String(PdfReader.getStreamBytes((PRStream)obj)));
                        }
                }
                
                PRStream toChange = (PRStream)reader2.getPdfObject(33);
                toChange.setData(new byte[0]);
                
                PdfStamper stamper = new PdfStamper(reader2, new 
FileOutputStream("result.pdf"));
                stamper.close();
        }
}
------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to