Showing posts with label itext. Show all posts
Showing posts with label itext. Show all posts

Thursday, April 29, 2010

How to extract comments from pdf using Itext

Hi Friends,

Here is the code of extracting comments from the PDF file using the itext.This extracts the comments of each page one by one and print them on the console.


/******************************************************************************/
package pdftest;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfString;
import java.util.ListIterator;

public class CommentsTest {

public static void main(String args[]) {
try {
System.out.println("Hi in the Comments Test");
PdfReader reader = new PdfReader("c:\\temp\\testattachments.pdf");

for (int i = 1; i <= reader.getNumberOfPages(); i++)
{

PdfDictionary page = reader.getPageN(i);
PdfArray annotsArray = null;

if(page.getAsArray(PdfName.ANNOTS)==null)
continue;

annotsArray = page.getAsArray(PdfName.ANNOTS);
for (ListIterator iter = annotsArray.listIterator(); iter.hasNext();)
{
PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(iter.next());
PdfString content = (PdfString) PdfReader.getPdfObject(annot.get(PdfName.CONTENTS));
if (content != null) {
System.out.println(content);
}
}
}


} catch (Exception e) {
e.printStackTrace();
}

}
}

/******************************************************************************/


Regards,
Vajahat Ali

Wednesday, March 31, 2010

Remove Atatchments from PDF file using itext

Hi Friends:
Here is the code that will be used to unpack(remove) the attachments from the PDF using itext.


PdfReader reader = new PdfReader(filename);
PdfDictionary root = reader.getCatalog();
PdfDictionary documentnames = root.getAsDict(PdfName.NAMES);
PdfDictionary embeddedfiles = documentnames.getAsDict(PdfName.EMBEDDEDFILES);
PdfArray filespecs = embeddedfiles.getAsArray(PdfName.NAMES);
PdfDictionary filespec;
PdfDictionary refs;
FileOutputStream fos;
PRStream stream;
for (int i = 0; i < filespecs.size(); ) {
filespecs.getAsName(i++);
filespec = filespecs.getAsDict(i++);
refs = filespec.getAsDict(PdfName.EF);
for (PdfName key : refs.getKeys()) {
fos = new FileOutputStream(filespec.getAsString(key).toString());
stream = (PRStream) PdfReader.getPdfObject(refs.getAsIndirectObject(key));
fos.write(PdfReader.getStreamBytes(stream));
fos.flush();
fos.close();
}
}