Thanks!  I think I am close to getting it working, but right now I get a 
message from all the viewers that there is an error in my PDF.  Here is my 
code.  Does anyone see something obvious that I am missing?

Thanks Doug

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PRTokeniser;
import com.lowagie.text.pdf.PdfContentParser;
import com.lowagie.text.pdf.PdfNumber;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfString;
import com.lowagie.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dlim
 */
public class ReadAndRewrite {
    /** buffer to build new content to output */
    private ArrayList<Byte> outputContent = new ArrayList<Byte>();


    public void addToContent(byte[] array) {
        for (byte element: array) {
                this.outputContent.add(element);
            }
    }

    public void addToContent(PdfObject pdfObject) {
        addToContent(pdfObject.getBytes());
    }
    public void addToContent(String string) {
        addToContent( new PdfString(string) );
    }
 
    public void convert() {
        try {
            PdfReader reader = new PdfReader("Glaucoma.pdf");
            FileOutputStream fileOutputStream = new 
FileOutputStream("FixedGlaucoma.pdf");
            PdfStamper pdfStamper = new PdfStamper(reader, 
fileOutputStream);
 
            byte[] content = reader.getPageContent(1);

            // debug - show the content stream
            for (byte letter : content) {
               System.out.print((char)letter);
            }

//            TEST the unmodified content out. <= this works
//            reader.setPageContent(1, content);
//            pdfStamper.close();
//            System.exit(0);
 
            String cmd;
            boolean lastCmdET = false;
            PdfContentParser contentParser = new PdfContentParser(new 
PRTokeniser(content));
            ArrayList<PdfObject> tokens = new ArrayList<PdfObject>();
            byte[] space = new PdfString(" ").getBytes();
            byte[] eol = new PdfString("\n").getBytes();
 
            System.out.println("=== Parsed lines ===");
 
            contentParser.parse(tokens);

            while ( !tokens.isEmpty() ) {
                for (PdfObject value: tokens) {
                    System.out.print(value + " ");
                }

                System.out.println();
 
                cmd = tokens.get(tokens.size() - 1).toString();
 
                if (!lastCmdET || !"h".equals(cmd)) {
                    // not an 'h' following an 'ET'
                    for (PdfObject value: tokens) {
                        addToContent(value);
                    }
                } else {
                    System.out.println("DEBUG: h command removed.");
                }
 
                if ( "ET".equals(cmd) ) {
                    System.out.println("DEBUG: ET last set.");
                    lastCmdET = true;
                } else {
                    lastCmdET = false;
                }
 
                contentParser.parse(tokens);
            }

            byte[] finalContent = new byte[this.outputContent.size()];
 
            for (int i = 0; i < this.outputContent.size(); ++i) {
                finalContent[i] = this.outputContent.get(i);
            }
 
            reader.setPageContent(1, finalContent);
            pdfStamper.close();
        } catch (DocumentException ex) {
 Logger.getLogger(ReadAndRewrite.class.getName()).log(Level.SEVERE, null, 
ex);
        } catch (IOException ex) {
 Logger.getLogger(ReadAndRewrite.class.getName()).log(Level.SEVERE, null, 
ex);
        }
    }
 
    public static void main(String args[]) {
        ReadAndRewrite readAndRewrite = new ReadAndRewrite();
        readAndRewrite.convert();
    }
}

----- Original Message ----- 

You'll need PdfStamper for the output, PdfReader.getPageContent(), 
PdfReader.setPageContent() and PdfContentParser.

Paulo

----- Original Message ----- 
From: "Lim, Doug" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, October 11, 2008 1:54 AM
Subject: [iText-questions] Editing PDF Stream



Hi,

I will apologize up-front. I am a complete newbie to iText and PDF files. 
The problem I am trying to solve is that I have a bunch of PDF files that 
are incorrectly formed. They can be read and viewed by Adobe Reader 9, but 

not by our embedded PDF reader. The error in the PDF file can be 
characterized by an "h" (close subpath) command immediately following an 
"ET" (end text) command in a stream. Using a commercial PDF editor we can 
delete all of the "h" commands that immediately follow an "ET" command and 

our reader can read the documents. The short term solution I am trying to 
implement is to remove any "h" commands immediately following the "ET" 
command programmatically. Is iText the right tool for solving this 
problem? 
If not can anyone suggest another tool or approach to solving this 
problem?

Thanks,
Doug 


Thanks,
Doug
________ 
 
Carl Zeiss Meditec, Inc.
Research and Development
Senior Staff Software Engineer
5160 Hacienda Drive
Dublin, CA 94568

D o u g   L i m

Phone: +1 925.557.4453
Fax: +1 925.557.4615
mailto:[EMAIL PROTECTED]
http://www.meditec.zeiss.com 


---------------------------------------- 
This message is intended for a particular addressee only and may contain 
business or company secrets. If you have received this email in error, please 
contact the sender and delete the message immediately. Any use of this email, 
including saving, publishing, copying, replication or forwarding of the message 
or the contents is not permitted.  
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

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

Reply via email to