Describe the bug
I am trying to remove added image with annotation from pdf file. here my code to add image with annotation.
`
InputStream inputStream = new FileInputStream(file);
// we create a reader for a certain document
PdfReader reader = new PdfReader(inputStream);
// we create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(file));
// adding content to each page
PdfContentByte over;
// get watermark icon
Image img = Image.getInstance(PublicFunction.getByteFromDrawable(context, R.drawable.ic_chat_lawone_new));
img.setAnnotation(new Annotation(0, 0, 0, 0, "http://www.lowagie.com/iText"));
img.setAbsolutePosition(pointF.x, pointF.y);
img.scaleAbsolute(50, 50);
// get page file number count
int pageNumbers = reader.getNumberOfPages();
if (pageNumbers < pageIndex) {
// closing PdfStamper will generate the new PDF file
stamp.close();
throw new PDFException("page index is out of pdf file page numbers", new Throwable());
}
// annotation added into target page
over = stamp.getOverContent(pageIndex);
if (over == null) {
stamp.close();
throw new PDFException("getUnderContent is null", new Throwable());
}
over.addImage(img);
// closing PdfStamper will generate the new PDF file
stamp.close();
`
After that I need to clear them from page, I am trying to clear or remove added images from pdf file with this scenario:
`
InputStream inputStream = new FileInputStream(file);
// we create a reader for a certain document
PdfReader reader = new PdfReader(inputStream);
// get page file number count
int pageNumbers = reader.getNumberOfPages();
// we create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(file));
reader.removeFields();
reader.removeAnnotations();
// close reader
reader.close();
// closing PdfStamper will generate the new PDF file
stamp.close();
`
With the code above annotations with be cleared but images still exist over pdf file.
Expected behavior
I expect images and annotations remove from the pdf file, but images are still there.
System (please complete the following information):
any help appreciated
Your code adds the image to the static page content:
over = stamp.getOverContent(pageIndex);
...
over.addImage(img);
The annotation you assigned to the image
img.setAnnotation(new Annotation(0, 0, 0, 0, "http://www.lowagie.com/iText"));
merely is added to the same page with the same location and dimensions as the image but its not the holder of the image.
To also remove the image, you'll have to edit the page content streams or manipulate the XObject resources.
Ah, and by the way, this is no bug.
Yes It's not a bug.
Can you give a code snippet about this issue? just add icon with link with possibility to be deleted. I can not find any thing in DOC or Wiki.
Thanks for your help
Here my progress
Now I can achieve the XObjects added into pdf file, and I can remove them from pdf page this way:
// inout stream from file
InputStream inputStream = new FileInputStream(file);
// we create a reader for a certain document
PdfReader pdfReader = new PdfReader(inputStream);
// get page file number count
int pageNumbers = pdfReader.getNumberOfPages();
// we create a stamper that will copy the document to a new file
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(file));
// get page
PdfDictionary page = pdfReader.getPageN(currPage);
PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
// get page resources
PdfArray annots = resources.getAsArray(PdfName.ANNOTS);
PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
// remove Xobjects
for (PdfName key : xobjects.getKeys()) {
xobjects.remove(key);
}
// remove annots
for (PdfObject element : annots.getElements()) {
annots.remove(0);
}
// close pdf stamper
pdfStamper.close();
// close pdf reader
pdfReader.close();
So the XObjects will remove from the screen, but still there is a problem!!!
When I remove them and try to add a new one, last deleted object appears and add into the pdf page! REALLY!!! :))
I think there should be another place that these objects should be removed from.
Any help appreciated
But this is clearing all images, how to clear a specific image?
how to clear a specific image?
Obviously you first have to recognize that image. You talk about a _specific image_. What is it that makes that image specific?
From your answer to that question you have to derive criteria which a program can handle.
That's right, I tried to put an ID when creating the image as specified here
But I'm unable to iterate through the objets and obtain the id
Hey guys, according to my issue, I have create a new repository and added my research results into it.
I have added some base functionalities like add and remove annotations and etc.
check it out here in MagicalPdfEditor