Hi

Here is a little sample of what I want to do.
The principle:
- create a link in a PDF,
- create an anchor in another PDF
- Concatenate the 2 PDFs and have the link that points to the anchor .

Unfortunately, this does not work because the anchor is not in the same file
that the link
It may be laying the link after the concatenation but I do not see how.

An idea?

2008/11/5 1T3XT info <[EMAIL PROTECTED]>

> nicolas DOS SANTOS wrote:
> > OK
> > Thank you
>
> Do you have some small sample PDF files.
> That always helps.
> --
> This answer is provided by 1T3XT BVBA
> http://www.1t3xt.com/ - http://www.1t3xt.info
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's
> challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> iText-questions mailing list
> iText-questions@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
> Buy the iText book: http://www.1t3xt.com/docs/book.php
>
package com._4d.docadvance.generator.pdf.tests;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SimpleBookmark;

public class PdfSample {

	final static String DEFAULT_DESTINATION = "defaultDestination";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File f1 = new File("C:/doc1.pdf");
		File f2 = new File("C:/doc2.pdf");
		File finalDoc = new File("C:/finalDoc.pdf");

		List<String> pdfFiles = new ArrayList<String>();
		pdfFiles.add(f1.getAbsolutePath());
		pdfFiles.add(f2.getAbsolutePath());

		makeFirstDocument(f1);
		makeSecondDocument(f2);

		concatenate(pdfFiles, finalDoc.getAbsolutePath());

		try {
			Process proc = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+finalDoc.getPath());
			proc.waitFor();
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	private static void makeFirstDocument(File f) {
		try {
			Document doc = new Document(PageSize.A4);
			PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(f));

			doc.open();

			doc.add(new Phrase(new Chunk("Link\n").setLocalGoto(DEFAULT_DESTINATION)));

			((PdfWriter)writer).setPageEmpty(false);
			doc.newPage();
			((PdfWriter)writer).setPageEmpty(false);
			doc.newPage();

			doc.close();
		} catch(Exception exc) {
			exc.printStackTrace();
		}
	}

	private static void makeSecondDocument(File f) {
		try {
			Document doc = new Document(PageSize.A4);
			PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(f));

			doc.open();

			doc.add(new Chunk(" ").setLocalDestination(DEFAULT_DESTINATION));
			doc.add(new Phrase(new Chunk("Anchor")));

			doc.close();

			try {
				Process proc = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+f.getPath());
				proc.waitFor();
			} catch (Exception exc) {
				exc.printStackTrace();
			}
		} catch(Exception exc) {
			exc.printStackTrace();
		}
	}

	public static void concatenate(List<String> pdfFiles, String outFile) {
		if(outFile==null || outFile.equals("")) {
			System.err.println("out file not defined");
			return;
		}
		if (pdfFiles.size() == 0) {
			System.err.println("No file to concatenate");
			return;
		}

		try {
			int pageOffset = 0;
			ArrayList master = new ArrayList();
			Document document = null;
			PdfCopy  writer = null;
			for(int f=0; f<pdfFiles.size(); f++) {
				PdfReader reader = new PdfReader(pdfFiles.get(f));
				reader.consolidateNamedDestinations();
				int n = reader.getNumberOfPages();
				List bookmarks = SimpleBookmark.
				getBookmark(reader);
				if (bookmarks != null) {
					if (pageOffset != 0)
						SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
					master.addAll(bookmarks);
				}
				pageOffset += n;
				if (f == 0) {
					document = new Document(reader.getPageSizeWithRotation(1));
					writer = new PdfCopy(document, new FileOutputStream(outFile));
					document.open();
				}
				PdfImportedPage page;
				for (int i = 0; i < n; ) {
					++i;
					page = writer.getImportedPage(reader, i);
					writer.addPage(page);
				}
				PRAcroForm form = reader.getAcroForm();
				if (form != null)
					writer.copyAcroForm(reader);
			}
			if (!master.isEmpty())
				writer.setOutlines(master);
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to