The code you quoted to us won't throw that exception.
You're doing something like:
PdfDictionary myDict = (PdfDictionary)
someVar.callSomeFunctionThatReturnsAPdfObject();
Not all PdfObject's are PdfDictionaries. Of particular interest to you, some
of them are indirect references to PdfObjects of one sort or another.
What you want is more like:
PdfObject myObj = someVar.callSome...();
myObj = PdfReader.getObject( myObj );
if (myObj instanceof PdfDictionary) {
PdfDictionary myDict = (PdfDictionary)myObj;
}
If you tell us which function you're calling to get the PdfObject, we might be
able to help you further.
PS: Including a huge block of commented out imports? Not very helpful. In
fact in sample code like this, we don't need to see any of it. We DO need to
see your exception stack trace, including the lines of your code that actually
threw the exception.
--Mark Storer
Senior Software Engineer
Cardiff.com
import legalese.Disclaimer;
Disclaimer<Cardiff> DisCard = null;
________________________________
From: Humayan Kabir [mailto:[email protected]]
Sent: Monday, October 04, 2010 4:20 PM
To: [email protected]
Subject: [iText-questions] help
Why this code gives me this error message-à an Exception in thread
"main" java.lang.ClassCastException: com.itextpdf.text.pdf.PRIndirectReference
cannot be cast to com.itextpdf.text.pdf.PdfDictionary.
//----------------------------------------------my codes
/*
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.parser.PdfContentReaderTool;
import java.awt.Color;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Section;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfImage;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import java.io.PrintWriter;
*/
import com.itextpdf.text.pdf.PdfStamper;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.*;
import java.util.Iterator;
import com.itextpdf.text.pdf.PdfLayer;
import java.util.Map;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfDictionary;
import java.lang.String;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfLayer;
import com.itextpdf.text.pdf.parser.PdfContentReaderTool;
import java.io.FileNotFoundException;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import com.itextpdf.text.pdf.PdfWriter;
import java.util.Dictionary;
import java.util.Iterator;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfIndirectReference;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfSignatureAppearance;
import java.util.Collection;
import java.util.Hashtable;
public class info {
public static void main(String[] args) throws
FileNotFoundException, DocumentException,
IOException {
PdfReader reader = new PdfReader("personalize.pdf"); //
personalize.pdf is a pdf file with 4 layers
if (isWatermarked(reader))
System.out.println("is marked");
else
System.out.println("is not marked");
reader.close();
}
public static boolean isWatermarked(PdfReader reader) throws
DocumentException, IOException{
PdfStamper stamper = new PdfStamper (reader, new
FileOutputStream("myTest2.pdf"));
Map<String, PdfLayer> layers =(Map) stamper.getPdfLayers();
boolean wm=false;
if (!layers.isEmpty()) {
Set<String> keys = layers.keySet();
for (String key : keys) {
if(key.toString().equalsIgnoreCase("layout"))
wm=true;
System.out.println("Layer name: " + key);
//PdfLayer layer = layers.get(key);
// do stuff with layer...
}
}
stamper.close();
return wm;
}
}// end of class
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.862 / Virus Database: 271.1.1/3170 - Release Date:
10/04/10 11:35:00
------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3.
Spend less time writing and rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php