import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PRIndirectReference;
import com.lowagie.text.pdf.PdfString;
import com.lowagie.text.pdf.RandomAccessFileOrArray;

public class CreateBackLinks
{
	public static void main(String[] args)
	{
		LinksMain linksMain = new LinksMain();
	}
}

class LinksMain
{	
	LinksMain()
	{			
		try
		{
			PdfReader reader = new PdfReader(getBytesFromFile(new File("Index-2pg.pdf")));
			PdfObject o;
			PdfDictionary d;
			PdfDictionary l;
			PdfName n;
			
			for(int a = 1; a <= reader.getNumberOfPages(); a ++)
			{
				PdfReader page = new PdfReader(reader.getPageContent(a));
				System.out.println("Page num - " + a);
				
				/*for(int i = 1; i < page.getXrefSize(); i++)
				{				
					o = page.getPdfObject(i);
					if( o instanceof PdfDictionary)
					{
						d = (PdfDictionary) o;
						o = d.get(PdfName.A);
						
						if( o == null) continue;
						if( o instanceof PdfDictionary)
						{
							l = (PdfDictionary) o;
						}
						else
						{
							PRIndirectReference r = (PRIndirectReference)o;
							l = (PdfDictionary) reader.getPdfObject(r.getNumber());
						}
						n = (PdfName) l.get(PdfName.S);
						if(PdfName.LAUNCH.equals(n))
						{
							if(l.get(PdfName.F) != null)
							{
								System.out.println("Removed: " + l.get(PdfName.F));
								//l.remove(PdfName.F);
							}
							if(l.get(PdfName.WIN) != null)
							{
								System.out.println("Removed: " + l.get(PdfName.WIN));
								//l.remove(PdfName.WIN);
							}
							l.put(PdfName.S, PdfName.JAVASCRIPT);
							l.put(PdfName.JS, new PdfString(
								"app.alert('Launch Application Action removed by iText');\r"));
						}
					}
				}*/
			}			
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
		
	} // close LinksMain()
	
	public static byte[] getBytesFromFile(File file) throws IOException 
	{
        InputStream is = new FileInputStream(file);
    
        // Get the size of the file
        long length = file.length();
    
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }
    
        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];
    
        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
    
        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
    
        // Close the input stream and return bytes
        is.close();
        return bytes;
    }
	
} // close class LinksMain