Hello,

I tried to change an external to an internal link.
But I’m not sure what is wrong with my code, because 
the linkchange do not effect.

Ether my linktarget or my link is broken, or even both?
Maybe somebody is able to explain how this link and destination
should work. I storggle about 4 days, so I will be really grateful
for some help.

Thanks in advanced.
br

Markus

----------------------- My Code --------------------------------------
package com.zf.swdoku.pdf.embedfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfIndirectReference;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.collection.PdfTargetDictionary;

public class FileGrep {

    public FileGrep() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        FileGrep fg = new FileGrep();

        try {
            fg.pageWalker("link1.pdf");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void pageWalker(String sourcePdfFilename) {

        PdfReader reader = null;
        try {
            reader = new PdfReader(sourcePdfFilename);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        reader.consolidateNamedDestinations();

        try {
            manipulateWithCopy(reader);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (DocumentException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

    private void manipulateWithCopy(PdfReader reader) throws IOException,
            DocumentException {
        int n = reader.getNumberOfPages();
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream("copy1.pdf"));
        document.open();
        for (int i = 1; i <= n; i++) {

            PdfDictionary pageDic = reader.getPageN(i);
            pageHandler(reader, pageDic, copy);
            copy.addPage(copy.getImportedPage(reader, i));
        }
        document.close();
        copy.close();
    }

    private void pageHandler(PdfReader reader, PdfDictionary pageDic,
            PdfCopy copy) {

        if (pageDic.get(PdfName.CONTENTS) != null) {

            PdfArray kids = pageDic.getAsArray(PdfName.ANNOTS);

            PdfIndirectReference kid_ref;
            PdfDictionary annotation = null;

            if (kids != null) {

                Iterator<PdfObject> i = kids.iterator();

                while (i.hasNext()) {

                    kid_ref = (PdfIndirectReference) i.next();

                    annotation = (PdfDictionary) reader.getPdfObject(kid_ref
                            .getNumber());

                    System.out.println(annotation.getKeys());
                    PdfIndirectReference annotationRef = (PdfIndirectReference) 
annotation
                            .get(PdfName.A);

                    if (annotationRef.isIndirect()) {

                        PdfDictionary action = (PdfDictionary) reader
                                .getPdfObject(annotationRef.getNumber());

                        System.out.println("action " + action + " "
                                + annotation.get(PdfName.SUBTYPE));

                        PdfObject actionUri = action.get(PdfName.URI);

                        if (actionUri != null) {

                            PdfAction changeAction = null;

                            try {
                                changeAction = attachFile(copy, actionUri);
                                PdfDictionary a = (PdfDictionary) annotation
                                        .getDirectObject(PdfName.A);

                                System.out.println(annotation.getDirectObject(
                                        PdfName.A).type());

                                System.out.println(a.getKeys());

                                // a.remove(PdfName.S);
                                // a.remove(PdfName.URI);
                                // a.put(PdfName.ACTION, changeAction);

                                annotation.remove(PdfName.A);
                                annotation.put(PdfName.ACTION, changeAction);

                                // annotation.put(PdfName.DEST, new PdfAction(
                                // PdfAction.FIRSTPAGE));
                                System.out.println(a.getKeys());

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

                        }

                        // System.out.println(kidUri);

                    }

                }
            }

        }

    }

    private PdfAction attachFile(PdfCopy copy, PdfObject kidUri)
            throws IOException {

        String path = kidUri.toString().replace("\\", "/");

        String[] pathA = path.split("/");

        File file = new File(path);
        byte[] data = new byte[(int) file.length()];
        try {
            new FileInputStream(file).read(data);
        } catch (Exception e) {
            e.printStackTrace();
        }

        PdfTargetDictionary target = new PdfTargetDictionary(true);
        target.setEmbeddedFileName(pathA[pathA.length - 1]);

        PdfDestination dest = new PdfDestination(PdfDestination.FIT);
        dest.addFirst(new PdfNumber(1));

        PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(copy, null,
                pathA[pathA.length - 1], data);

        fs.addDescription(pathA[pathA.length - 1], true);
        copy.addFileAttachment(fs);

        PdfAction action = PdfAction.gotoEmbedded(null, target, dest, true);

        return action;

    }
}

----------------------- My Code --------------------------------------
------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and 
AppDynamics. Performance Central is your source for news, insights, 
analysis and resources for efficient Application Performance Management. 
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
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

Reply via email to