Title: Pdf internal link lost
Only PdfCopy, PdfCopyFields and PdfStamper keep the links.
 
Best Regards,
Paulo Soares


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Forest, Sebastien
Sent: Thursday, March 04, 2004 6:19 PM
To: '[EMAIL PROTECTED]'
Subject: [iText-questions] Pdf internal link lost

Hi
I open a pdf document with iText and when I re-save it to create another pdf document, I lost my internal link.
I join the original pdf and my java code follow:
<<test2.pdf>>
/*
 * $Id: Chap0101.java,v 1.4 2003/06/25 07:36:32 blowagie Exp $
 * $Name:  $
 *
 * This code is free software. It may only be copied or modified
 * if you include the following copyright notice:
 *
 * --> Copyright 2001 by Bruno Lowagie <--
 *
 * This code is part of the 'iText Tutorial'.
 * You can find the complete tutorial at the following address:
 * http://www.lowagie.com/iText/tutorial/
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * [EMAIL PROTECTED]
 */

import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

import java.util.StringTokenizer;

public class Test_02 {

public static void main(String[] args) {

    // change graphic size and position, page dimension
    setGraphicsPosition("../../Stylesheet/test2.pdf", "Test_02.pdf");

}

private static void setGraphicsPosition(String fileIn, String fileOut) {
    try {
        // creation of a reader for a certain document
        PdfReader reader = new PdfReader(fileIn);
        // total number of pages
        int n = reader.getNumberOfPages();
        // size of the first page
        Rectangle psize = reader.getPageSize(1);
        float width = psize.width();
        float height = psize.height();

        /*
        // creation of a document-object
        Document document = new Document(psize);
        // creation of a writer that listens to the document
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileOut));
        // open the document
        document.open();
        // add content
        PdfContentByte cb = writer.getDirectContent();
        */

        int i = 0;
        while (i < n) {
            i++;

            /*
            document.setPageSize(psize);
            //document.newPage();
            PdfImportedPage page1 = writer.getImportedPage(reader, i);
            //cb.addTemplate(page1, 0, 0);
            */

            byte[] pageB = reader.getPageContent(i, reader.getSafeFile());
            String pageS = new String(pageB);
            int idxSearch = -2;
            int startSearch = 0;
            String currentLine = null;
            String previousLine = null;
            String newLine = "";
            while ((idxSearch = pageS.indexOf("\n", startSearch))!=-1) {
                currentLine = pageS.substring(startSearch, idxSearch);
                /*if (currentLine.indexOf("/Im")!=-1) {
                    if (previousLine!=null) {
                        StringTokenizer tokens = new StringTokenizer(previousLine, " ");
                        //for (int j=1; j <=4; j++) {
                        //     tokens.nextToken();
                        //}

                        // change page format
                        document.setPageSize(psize.rotate());

                        Float imgHeight = new Float(tokens.nextToken());
                        tokens.nextToken();
                        tokens.nextToken();
                        Float imgWidth = new Float(tokens.nextToken());
                        Float posLeft = new Float(tokens.nextToken());
                        Float posBottom = new Float(tokens.nextToken());
                        newLine = imgHeight + " 0 0 " + imgWidth + " ";
                        //newLine += posLeft + " " + posBottom + " cm\n";
                        newLine += posLeft + " " + "100" + " cm\n";
                        //System.out.println(previousLine);
                        pageS = replace(pageS, previousLine, newLine);
                        startSearch = idxSearch - previousLine.length() + newLine.length() + 1;
                    }
                    else {
                        startSearch = idxSearch + 1;
                    }
                }
                else  {*/
                    startSearch = idxSearch + 1;
                //}
                previousLine = currentLine;
                System.out.println(previousLine);
            }
            /*
            document.newPage();
            cb.addTemplate(page1, 0, 0);

            //System.out.println(pageS);
            reader.setPageContent(i, pageS.getBytes());
            */
        }
        // close the document
        //document.close();
    }
    catch (Exception de) {
        de.printStackTrace();
    }
}

public static String replace(String sContent, String sSearch, String sReplace) {
    String result = null;
    int idx = -2;
    if ((idx = sContent.indexOf(sSearch))!=-1) {
        result = sContent.substring(0, idx) + sReplace + sContent.substring(idx + sSearch.length() + 1);
    }
    return result;
}

}


Reply via email to