Hi,

Yes, I know my question was not really good.
I’ve got frustrated about my last request on the support forum.

OK. Just for you to know DOCBOOK becomes a standard in documantation
transformation (http://www.docbook.org/).
They start the page numbering with roman letters like (I, II, III, IV, ...).
They countinue on the chapters with arabian numbers (1,2,3,4, ..).

So my goal is to
- get the pdf made by docbook (made with fop).
- parse the pdf and change the external links (to documents and graphics) 
into annotations
- finally I have to replace the external link with a internal link to the 
annotated file

So I started to convert my pdf to something like the “kubrick box” sample, 
but I didn’t
get it managed to get the links working.

By facing the deadline I decided, to do a totally dirty way. I used the 
“Layer” example, to
get something working right now. I’m not prowd on that, sure.

Best regards

Markus

________________ CODE ________________________________________

package com.zf.swdoku.pdf.embedfile;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfAppearance;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfImportedPage;
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.PdfWriter;

public class EmbedExternalFiles {

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

/**
* @param args
*/
public static void main(String[] args) {
Document document = new Document(PageSize.A4);
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
"test_attach2.pdf"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.open();
PdfContentByte cb = writer.getDirectContent();

// ============ READER =============================

PdfReader reader = null;
try {
reader = new PdfReader("link1.pdf");
} catch (IOException e) {

e.printStackTrace();
}
reader.consolidateNamedDestinations();

int n = reader.getNumberOfPages();

for (int i = 1; i <= n; i++) {

PdfDictionary pageDic = reader.getPageN(i);

cb.getPdfDocument().newPage();
// ==== import PAGE
PdfImportedPage importedPage = writer.getImportedPage(reader, i);

cb.addTemplate(importedPage, 0, 0);

// ==== import PAGE

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

// ============== READING PAGES ======================

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

PdfIndirectReference kid_ref;
PdfDictionary annota = null;

if (kids != null) {

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

while (kidsIterator.hasNext()) {

kid_ref = (PdfIndirectReference) kidsIterator.next();

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

PdfIndirectReference annotationRef = (PdfIndirectReference) annota
.get(PdfName.A);

if (annotationRef.isIndirect()) {

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

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

if (actionUri != null) {
// ====== WORKING ON ACTION EXTRACTION
// ================

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

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

String filename = pathA[pathA.length - 1];
PdfArray rectA = annota
.getAsArray(PdfName.RECT);

PdfNumber rect0 = rectA.getAsNumber(0);
PdfNumber rect1 = rectA.getAsNumber(1);
PdfNumber rect2 = rectA.getAsNumber(2);
PdfNumber rect3 = rectA.getAsNumber(3);

// ===== EXTRACTION ===========

// ====== REBUILD =============

Rectangle rect = new Rectangle(
rect0.floatValue(), rect1.floatValue(),
rect2.floatValue(), rect3.floatValue()

);
BaseColor bg = new BaseColor(122, 13, 120);
rect.setBorderColor(bg);
rect.setBorder(10);
rect.enableBorderSide(2);

PdfFileSpecification pf = null;
try {
pf = PdfFileSpecification.fileEmbedded(
writer, path, filename, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PdfAnnotation annot = null;
try {
annot = PdfAnnotation.createFileAttachment(
writer, rect, filename, pf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PdfAppearance ap = cb.createAppearance(10, 10);
ap.setBoundingBox(rect);
ap.setLineWidth(1);
annot.setAppearance(
PdfAnnotation.APPEARANCE_NORMAL, ap);
annot.setAppearance(
PdfAnnotation.APPEARANCE_DOWN, ap);
annot.setPage();
writer.addAnnotation(annot);

// ====== REBUILD =============

// ====== WORKING ON ACTION EXTRACTION
// ================

}
}
}
}

// ============== READING PAGES ======================

}

}

document.close();

}
}



From: 1T3XT BVBA
Sent: Thursday, August 29, 2013 6:26 PM
To: Post all your questions about iText here
Subject: Re: [iText-questions] addTemple(importedPage, 0, 0) will drop the 
page numbers and the inner links

On 29/08/2013 17:07, markus.sticker.e...@zf.com wrote:


Yes, I know using PDFCopy is the better way.


Indeed.





But that what I’m  trying to do was not possible


What you're trying to do is unclear to people who don't know you (and you're 
not telling them). How you're using iText seems to be completely wrong 
(based on the short code snippet).

The line where you get the page dictionary is absurd in the context of the 
rest of the snippet. Why is it there? you don't use it! 
cb.getPdfDocument().newPage() hurts my eyes. I don't want to see such a line 
ever again!

In your subject you say the page numbers are lost, but in your mail you say 
some aren't: "The page numbers (docbook starts with I, ii, … 1, 2, 3, …) are 
not lost". By the way: what is docbook?
As for: "And also the bookmarks and the links of the page index doesn’t work 
any more." Er... you're using addTemplate(). That's like taking a photocopy 
on paper and complaining the links no longer work when you touch the paper 
with your finger.

Long story short: the quality of the question is too poor for you to get an 
answer.


------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&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 


------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&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