BaseURL examples

Tags: actionslink annotationbase URL

Examples written in answer to the question How to set the BaseUrl of an existing PDF document?

Files: 
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24568386/set-baseurl-of-an-existing-pdf-document
 */
package sandbox.interactive;
 
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class BaseURL1 {
    public static final String DEST = "results/interactive/base_url_1.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new BaseURL1().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        writer.addJavaScript("this.baseURL = \"http://itextpdf.com/\";");
        Anchor anchor = new Anchor("Home page");
        anchor.setReference("index.php");
        document.add(anchor);
        // step 5
        document.close();
    }
}
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24568386/set-baseurl-of-an-existing-pdf-document
 */
package sandbox.interactive;
 
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class BaseURL2 {
    public static final String DEST = "results/interactive/base_url_2.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new BaseURL2().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfDictionary uri = new PdfDictionary(PdfName.URI);
        uri.put(new PdfName("Base"), new PdfString("http://itextpdf.com/"));
        writer.getExtraCatalog().put(PdfName.URI, uri);
        Anchor anchor = new Anchor("Home page");
        anchor.setReference("index.php");
        document.add(anchor);
        // step 5
        document.close();
    }
}
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24568386/set-baseurl-of-an-existing-pdf-document
 */
package sandbox.interactive;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfString;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class BaseURL3 {
    public static final String SRC = "resources/pdfs/base_url.pdf";
    public static final String DEST = "results/interactive/base_url_3.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new BaseURL3().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfDictionary uri = new PdfDictionary(PdfName.URI);
        uri.put(new PdfName("Base"), new PdfString("http://itextpdf.com/"));
        reader.getCatalog().put(PdfName.URI, uri);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
    }
}
File nameRaw URLUpdated
BaseURL1.javaBaseURL1.java2015-11-06 3:50 pm
BaseURL2.javaBaseURL2.java2015-11-06 3:50 pm
BaseURL3.javaBaseURL3.java2015-11-06 3:50 pm
Resources: 
File nameRaw URLUpdated
base_url.pdfbase_url.pdf2015-11-06 3:51 pm
Results: 
File nameRaw URLUpdated
cmp_base_url_1.pdfcmp_base_url_1.pdf2015-11-06 3:52 pm
cmp_base_url_2.pdfcmp_base_url_2.pdf2015-11-06 3:52 pm
cmp_base_url_3.pdfcmp_base_url_3.pdf2015-11-06 3:52 pm