selvi
2006-09-13 06:44:37 UTC
Hi,
when i execute the code listed .l iam getting below listed error.
ERROR:
C:\jerry>java ImageLink
java.lang.UnsupportedOperationException: Use PdfStamper.getUnderContent() or PdfStamper.getOverContent()
at com.lowagie.text.pdf.PdfStamperImp.getDirectContent(PdfStamperImp.java:1365)
at ImageLink.main(ImageLink.java:36)
code:
import java.io.FileOutputStream;
import java.util.HashMap;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfAppearance;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFileSpecification;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
/**
* Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
*/
public class ImageLink {
public static void main(String[] args) {
try {
// we create a reader for a certain document
PdfReader reader = new PdfReader("page81.pdf");
int n = reader.getNumberOfPages();
// we create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("page81_new.pdf"));
// adding some metadata
HashMap moreInfo = new HashMap();
moreInfo.put("Author", "Sundar");
stamp.setMoreInfo(moreInfo);
// adding content to each page
int i = 1;
// while (i < n) {
///i++;
PdfContentByte cb = stamp.getWriter().getDirectContent();
cb.rectangle(250/*left*/, 250/*top*/,250/*right*/, 250/*bottom*/);
cb.stroke();
Rectangle rect = new Rectangle(100/*left*/, 0/*top*/,350/*right*/, 350/*bottom*/);
PdfFileSpecification pf = PdfFileSpecification.fileEmbedded(stamp.getWriter(),"attached image.JPG", "attached image.JPG", null);
PdfAnnotation annot = PdfAnnotation.createFileAttachment(stamp.getWriter(), rect,"attached image.JPG", pf);
PdfAppearance ap = cb.createAppearance(300, 300);
ap.setBoundingBox(rect);
ap.setLineWidth(1);
annot.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
annot.setAppearance(PdfAnnotation.APPEARANCE_DOWN, ap);
annot.setPage();
stamp.addAnnotation(annot, i);
//}
stamp.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
}
when i execute the code listed .l iam getting below listed error.
ERROR:
C:\jerry>java ImageLink
java.lang.UnsupportedOperationException: Use PdfStamper.getUnderContent() or PdfStamper.getOverContent()
at com.lowagie.text.pdf.PdfStamperImp.getDirectContent(PdfStamperImp.java:1365)
at ImageLink.main(ImageLink.java:36)
code:
import java.io.FileOutputStream;
import java.util.HashMap;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfAppearance;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFileSpecification;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
/**
* Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
*/
public class ImageLink {
public static void main(String[] args) {
try {
// we create a reader for a certain document
PdfReader reader = new PdfReader("page81.pdf");
int n = reader.getNumberOfPages();
// we create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("page81_new.pdf"));
// adding some metadata
HashMap moreInfo = new HashMap();
moreInfo.put("Author", "Sundar");
stamp.setMoreInfo(moreInfo);
// adding content to each page
int i = 1;
// while (i < n) {
///i++;
PdfContentByte cb = stamp.getWriter().getDirectContent();
cb.rectangle(250/*left*/, 250/*top*/,250/*right*/, 250/*bottom*/);
cb.stroke();
Rectangle rect = new Rectangle(100/*left*/, 0/*top*/,350/*right*/, 350/*bottom*/);
PdfFileSpecification pf = PdfFileSpecification.fileEmbedded(stamp.getWriter(),"attached image.JPG", "attached image.JPG", null);
PdfAnnotation annot = PdfAnnotation.createFileAttachment(stamp.getWriter(), rect,"attached image.JPG", pf);
PdfAppearance ap = cb.createAppearance(300, 300);
ap.setBoundingBox(rect);
ap.setLineWidth(1);
annot.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
annot.setAppearance(PdfAnnotation.APPEARANCE_DOWN, ap);
annot.setPage();
stamp.addAnnotation(annot, i);
//}
stamp.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
}