The correct code is:

import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;

import com.lowagie.text.pdf.*;



public class MinimalModifyPdfMetadata {

    public byte[] modifyMetadata(String pdf)
    throws Exception {
        ByteArrayOutputStream baos = new
        ByteArrayOutputStream();

        PdfReader pdfReader = new PdfReader(pdf);
        byte[] b =
        PdfReader.getStreamBytes((PRStream)(PdfReader.getPdfObject(
        pdfReader.getCatalog().get(new
        PdfName("Metadata")))), pdfReader.getSafeFile());

        System.out.println("original metadata");
        System.out.println(new String(b));

        PdfStamper stamp = new PdfStamper(pdfReader,
        baos);

        //set the metadata

        try {
            PdfWriter writer = stamp.getWriter();
            PdfIndirectReference ref = writer.addToBody(new
PdfStream(b)).getIndirectReference();
            pdfReader.getCatalog().put(new PdfName("Metadata"), ref);

            stamp.close();
        }//end try
        catch (Exception ex) {
            ex.printStackTrace(System.err);
        }//end catch Exception

        return baos.toByteArray();
    }//end modifyMetadata



    public static void main(String[] args)
    throws Exception {
        int code = 0;

        if (args.length > 1) {
            BufferedOutputStream bos
            = new BufferedOutputStream(new
            FileOutputStream(args[1]));

            bos.write(new
            MinimalModifyPdfMetadata().modifyMetadata(args[0]));
            bos.close();
        }//end if at least 2 args
        else {
            System.err.println(
            "Usage:  MinimalModifyPdfMetadata input-file output-file");
        }//end else, wrong # of args

        System.exit(code);
    }//end main

}//end class MinimalModifyPdfMetadata


However it wont't compile unless you make some methods in PdfWriter,
PdfIndirectObject and PdfIndirectReference public or move your code to
com.lowagie.text.pdf. I'll keep them public in the next version.

Best Regards,
Paulo Soares

----- Original Message -----
From: "Matt Benson" <[EMAIL PROTECTED]>
To: "Leonard Rosenthol" <[EMAIL PROTECTED]>; "Paulo Soares"
<[EMAIL PROTECTED]>; "itext-questions"
<[EMAIL PROTECTED]>
Sent: Wednesday, March 12, 2003 20:59
Subject: RE: [iText-questions] modifying metadata


> Okay, I am STUCK.  I can play with XMP/RDF, that's
> fine, and I've been doing it.  But I am finding myself
> unable to actually change the metadata of a given PDF.
>  I have culled together a fairly short example; can
> anyone see any obvious problems?
>
>
> import java.io.FileOutputStream;
> import java.io.BufferedOutputStream;
> import java.io.ByteArrayOutputStream;
>
> import com.lowagie.text.pdf.PdfName;
> import com.lowagie.text.pdf.PRStream;
> import com.lowagie.text.pdf.PdfObject;
> import com.lowagie.text.pdf.PdfReader;
> import com.lowagie.text.pdf.PdfStream;
> import com.lowagie.text.pdf.PdfStamper;
> import com.lowagie.text.pdf.PdfDictionary;
>
>
> public class MinimalModifyPdfMetadata
> {
>
>   public byte[] modifyMetadata(String pdf)
>    throws Exception
>   {
>     ByteArrayOutputStream baos = new
> ByteArrayOutputStream();
>
>     PdfReader pdfReader = new PdfReader(pdf);
>     byte[] b =
> PdfReader.getStreamBytes((PRStream)(PdfReader.getPdfObject(
>      pdfReader.getCatalog().get(new
> PdfName("Metadata")))), pdfReader.getSafeFile());
>
>     System.out.println("original metadata");
>     System.out.println(new String(b));
>
>     PdfStamper stamp = new PdfStamper(pdfReader,
> baos);
>
>     //set the metadata
>     pdfReader.getCatalog().put(new
> PdfName("Metadata"), new PdfStream(b));
>
>     try
>     {
>       stamp.close();
>     }//end try
>     catch (Exception ex)
>     {
>       ex.printStackTrace(System.err);
>     }//end catch Exception
>
>     return baos.toByteArray();
>   }//end modifyMetadata
>
>
>   public static void main(String[] args)
>    throws Exception
>   {
>     int code = 0;
>
>     if (args.length > 1)
>     {
>       BufferedOutputStream bos
>        = new BufferedOutputStream(new
> FileOutputStream(args[1]));
>
>       bos.write(new
> MinimalModifyPdfMetadata().modifyMetadata(args[0]));
>       bos.close();
>     }//end if at least 2 args
>     else
>     {
>       System.err.println(
>        "Usage:  MinimalModifyPdfMetadata input-file
> output-file");
>     }//end else, wrong # of args
>
>     System.exit(code);
>   }//end main
>
> }//end class MinimalModifyPdfMetadata
>
>
> I get the following stack trace when I run the above
> code:
>
> java.lang.NullPointerException
>         at
> java.io.OutputStream.write(OutputStream.java:65)
>         at
> com.lowagie.text.pdf.PdfDictionary.toPdf(PdfDictionary.java:152)
>         at
> com.lowagie.text.pdf.PdfIndirectObject.<init>(PdfIndirectObject.java:
> 138)
>         at
> com.lowagie.text.pdf.PdfIndirectObject.<init>(PdfIndirectObject.java:
> 107)
>         at
> com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:255)
>         at
> com.lowagie.text.pdf.PdfWriter.addToBody(PdfWriter.java:1361)
>         at
> com.lowagie.text.pdf.PdfStamperImp.close(PdfStamperImp.java:107)
>         at
> com.lowagie.text.pdf.PdfStamper.close(PdfStamper.java:104)
>         at
> MinimalModifyPdfMetadata.modifyMetadata(MinimalModifyPdfMetadata.java
> :36)
>         at
> MinimalModifyPdfMetadata.main(MinimalModifyPdfMetadata.java:57)
>
> --- Leonard Rosenthol <[EMAIL PROTECTED]> wrote:
> > At 03:00 PM 3/10/2003 -0800, Matt Benson wrote:
> > >Is XMP the only valid format for the Metadata
> > stream?
> >
> >          Yes.
> >
> >
> > >What means would you suggest for reading this?
> >
> >          The Adobe XMP Toolkit is the best, but that
> > assumes Mac OS or
> > Windows.   Since it's XML you can really use any XML
> > parser you want -
> > though starting with an RDF parser (on top of your
> > XML) will help...
> >
> >
> > Leonard
> >
> ------------------------------------------------------------------------
> ---
> > Leonard Rosenthol
> > <mailto:[EMAIL PROTECTED]>
> > Chief Technical Officer
> > <http://www.pdfsages.com>
> > PDF Sages, Inc.
> > 215-629-3700 (voice)
> >
> >
> >
> >
> -------------------------------------------------------
> > This sf.net email is sponsored by:ThinkGeek
> > Welcome to geek heaven.
> > http://thinkgeek.com/sf
> > _______________________________________________
> > iText-questions mailing list
> > [EMAIL PROTECTED]
> >
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Web Hosting - establish your business online
> http://webhosting.yahoo.com
>



-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open! 
Get cracking and register here for some mind boggling fun and 
the chance of winning an Apple iPod:
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to