Linking pdfs of Portable Collection using Itext





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have portable collection in that total of two pdf document is there. My requirement is that if I open one pdf document and click the link or any text, it should redirect to another pdf of that portable Collection.



For example: Consider there are two pdfs inside a portable collection namely Programming.pdf and Java.pdf. If I click any particular text or variable inside Programming.pdf, it should redirect to Java.pdf. Is it possible...?.










share|improve this question

























  • The question doesn't appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, and show a specific roadblock you're running into with Minimal, Complete, and Verifiable example. For more information, please see How to Ask.

    – Andreas
    Nov 23 '18 at 4:31











  • @mkl Any suggestions.

    – Nagaraj S Kharvi
    Nov 26 '18 at 11:25











  • That would require Embedded Go-To actions; I have not played around with them yet. Probably I find some time to do so this week.

    – mkl
    Nov 26 '18 at 11:30











  • @mkl please update if you get any solution.

    – Nagaraj S Kharvi
    Nov 28 '18 at 5:33











  • @mkl please look at this stackoverflow.com/questions/55451570/…

    – Nagaraj S Kharvi
    Apr 1 at 10:59


















0















I have portable collection in that total of two pdf document is there. My requirement is that if I open one pdf document and click the link or any text, it should redirect to another pdf of that portable Collection.



For example: Consider there are two pdfs inside a portable collection namely Programming.pdf and Java.pdf. If I click any particular text or variable inside Programming.pdf, it should redirect to Java.pdf. Is it possible...?.










share|improve this question

























  • The question doesn't appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, and show a specific roadblock you're running into with Minimal, Complete, and Verifiable example. For more information, please see How to Ask.

    – Andreas
    Nov 23 '18 at 4:31











  • @mkl Any suggestions.

    – Nagaraj S Kharvi
    Nov 26 '18 at 11:25











  • That would require Embedded Go-To actions; I have not played around with them yet. Probably I find some time to do so this week.

    – mkl
    Nov 26 '18 at 11:30











  • @mkl please update if you get any solution.

    – Nagaraj S Kharvi
    Nov 28 '18 at 5:33











  • @mkl please look at this stackoverflow.com/questions/55451570/…

    – Nagaraj S Kharvi
    Apr 1 at 10:59














0












0








0


1






I have portable collection in that total of two pdf document is there. My requirement is that if I open one pdf document and click the link or any text, it should redirect to another pdf of that portable Collection.



For example: Consider there are two pdfs inside a portable collection namely Programming.pdf and Java.pdf. If I click any particular text or variable inside Programming.pdf, it should redirect to Java.pdf. Is it possible...?.










share|improve this question
















I have portable collection in that total of two pdf document is there. My requirement is that if I open one pdf document and click the link or any text, it should redirect to another pdf of that portable Collection.



For example: Consider there are two pdfs inside a portable collection namely Programming.pdf and Java.pdf. If I click any particular text or variable inside Programming.pdf, it should redirect to Java.pdf. Is it possible...?.







pdf itext






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 '18 at 12:27









mkl

55.9k1270150




55.9k1270150










asked Nov 23 '18 at 4:16









Nagaraj S KharviNagaraj S Kharvi

148




148













  • The question doesn't appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, and show a specific roadblock you're running into with Minimal, Complete, and Verifiable example. For more information, please see How to Ask.

    – Andreas
    Nov 23 '18 at 4:31











  • @mkl Any suggestions.

    – Nagaraj S Kharvi
    Nov 26 '18 at 11:25











  • That would require Embedded Go-To actions; I have not played around with them yet. Probably I find some time to do so this week.

    – mkl
    Nov 26 '18 at 11:30











  • @mkl please update if you get any solution.

    – Nagaraj S Kharvi
    Nov 28 '18 at 5:33











  • @mkl please look at this stackoverflow.com/questions/55451570/…

    – Nagaraj S Kharvi
    Apr 1 at 10:59



















  • The question doesn't appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, and show a specific roadblock you're running into with Minimal, Complete, and Verifiable example. For more information, please see How to Ask.

    – Andreas
    Nov 23 '18 at 4:31











  • @mkl Any suggestions.

    – Nagaraj S Kharvi
    Nov 26 '18 at 11:25











  • That would require Embedded Go-To actions; I have not played around with them yet. Probably I find some time to do so this week.

    – mkl
    Nov 26 '18 at 11:30











  • @mkl please update if you get any solution.

    – Nagaraj S Kharvi
    Nov 28 '18 at 5:33











  • @mkl please look at this stackoverflow.com/questions/55451570/…

    – Nagaraj S Kharvi
    Apr 1 at 10:59

















The question doesn't appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, and show a specific roadblock you're running into with Minimal, Complete, and Verifiable example. For more information, please see How to Ask.

– Andreas
Nov 23 '18 at 4:31





The question doesn't appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, and show a specific roadblock you're running into with Minimal, Complete, and Verifiable example. For more information, please see How to Ask.

– Andreas
Nov 23 '18 at 4:31













@mkl Any suggestions.

– Nagaraj S Kharvi
Nov 26 '18 at 11:25





@mkl Any suggestions.

– Nagaraj S Kharvi
Nov 26 '18 at 11:25













That would require Embedded Go-To actions; I have not played around with them yet. Probably I find some time to do so this week.

– mkl
Nov 26 '18 at 11:30





That would require Embedded Go-To actions; I have not played around with them yet. Probably I find some time to do so this week.

– mkl
Nov 26 '18 at 11:30













@mkl please update if you get any solution.

– Nagaraj S Kharvi
Nov 28 '18 at 5:33





@mkl please update if you get any solution.

– Nagaraj S Kharvi
Nov 28 '18 at 5:33













@mkl please look at this stackoverflow.com/questions/55451570/…

– Nagaraj S Kharvi
Apr 1 at 10:59





@mkl please look at this stackoverflow.com/questions/55451570/…

– Nagaraj S Kharvi
Apr 1 at 10:59












1 Answer
1






active

oldest

votes


















0














To navigate between documents in a portable collection, you have to employ Embedded Go-To actions. They in particular contain a Target dictionary which actually can be the start of a chain of target dictionaries to navigate between collection members with arbitrary embedding depths. To navigate between sibling documents, you need one target dictionary going up to the parent document containing another target dictionary going down again from that parent to the desired sibling. For details cf. ISO 32000-2 section 12.6.4.4 "Embedded Go-To actions".



In iText 5.x you build such embedded goto actions like this:





PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
sibbling.setEmbeddedFileName(linkId);
PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
Chunk chunk = new Chunk("Go to " + linkId + ".");
PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
dest.addFirst(new PdfNumber(0));
PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
chunk.setAction(action);
document.add(chunk);


(from EmbeddedLinks helper createPage)



For example the following test creates a portfolio with four documents A, B, C, and D which all contain links to each other:



public void testLinkToSibblingInPortfolio() throws IOException, DocumentException {
Files.write(new File("portfolio-with-embedded-gotos.pdf").toPath(), createPdf(new String {"A", "B", "C", "D"}));
}

public byte createPdf(String allIds) throws DocumentException, IOException {
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("This document contains a collection of PDFs."));

PdfCollection collection = new PdfCollection(PdfCollection.HIDDEN);
PdfCollectionSchema schema = getCollectionSchema();
collection.setSchema(schema);
PdfCollectionSort sort = new PdfCollectionSort("TITLE");
collection.setSort(sort);
collection.setInitialDocument("A");
writer.setCollection(collection);

PdfFileSpecification fs;
PdfCollectionItem item;
for (String id : allIds) {
fs = PdfFileSpecification.fileEmbedded(writer, null,
String.format("%s.pdf", id),
createPage(id, allIds));
fs.addDescription(id, false);

item = new PdfCollectionItem(schema);
item.addItem("TITLE", id);
fs.addCollectionItem(item);
writer.addFileAttachment(fs);
}

document.close();
return baos.toByteArray();
}

public byte createPage(String id, String allIds) throws DocumentException, IOException {
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();

Paragraph p = new Paragraph(id,
FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 48));
document.add(p);

for (String linkId : allIds) {
if (!id.equals(linkId)) {
PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
sibbling.setEmbeddedFileName(linkId);
PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
Chunk chunk = new Chunk("Go to " + linkId + ".");
PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
dest.addFirst(new PdfNumber(0));
PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
chunk.setAction(action);
document.add(chunk);
}
}

document.close();
return baos.toByteArray();
}

private static PdfCollectionSchema getCollectionSchema() {
PdfCollectionSchema schema = new PdfCollectionSchema();

PdfCollectionField size = new PdfCollectionField("File size", PdfCollectionField.SIZE);
size.setOrder(2);
schema.addField("SIZE", size);

PdfCollectionField filename = new PdfCollectionField("File name", PdfCollectionField.FILENAME);
filename.setVisible(false);
schema.addField("FILE", filename);

PdfCollectionField title = new PdfCollectionField("Title", PdfCollectionField.TEXT);
title.setOrder(0);
schema.addField("TITLE", title);

return schema;
}


(EmbeddedLinks test testLinkToSibblingInPortfolio and helpers createPdf, createPage, and getCollectionSchema)



This actually borrows a lot from the iText examples KubrickBox, KubrickCollection, and KubrickMovies.





Tested with the current iText 5 development version 5.5.14-SNAPSHOT.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440583%2flinking-pdfs-of-portable-collection-using-itext%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    To navigate between documents in a portable collection, you have to employ Embedded Go-To actions. They in particular contain a Target dictionary which actually can be the start of a chain of target dictionaries to navigate between collection members with arbitrary embedding depths. To navigate between sibling documents, you need one target dictionary going up to the parent document containing another target dictionary going down again from that parent to the desired sibling. For details cf. ISO 32000-2 section 12.6.4.4 "Embedded Go-To actions".



    In iText 5.x you build such embedded goto actions like this:





    PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
    sibbling.setEmbeddedFileName(linkId);
    PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
    Chunk chunk = new Chunk("Go to " + linkId + ".");
    PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
    dest.addFirst(new PdfNumber(0));
    PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
    chunk.setAction(action);
    document.add(chunk);


    (from EmbeddedLinks helper createPage)



    For example the following test creates a portfolio with four documents A, B, C, and D which all contain links to each other:



    public void testLinkToSibblingInPortfolio() throws IOException, DocumentException {
    Files.write(new File("portfolio-with-embedded-gotos.pdf").toPath(), createPdf(new String {"A", "B", "C", "D"}));
    }

    public byte createPdf(String allIds) throws DocumentException, IOException {
    Document document = new Document();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, baos);
    document.open();
    document.add(new Paragraph("This document contains a collection of PDFs."));

    PdfCollection collection = new PdfCollection(PdfCollection.HIDDEN);
    PdfCollectionSchema schema = getCollectionSchema();
    collection.setSchema(schema);
    PdfCollectionSort sort = new PdfCollectionSort("TITLE");
    collection.setSort(sort);
    collection.setInitialDocument("A");
    writer.setCollection(collection);

    PdfFileSpecification fs;
    PdfCollectionItem item;
    for (String id : allIds) {
    fs = PdfFileSpecification.fileEmbedded(writer, null,
    String.format("%s.pdf", id),
    createPage(id, allIds));
    fs.addDescription(id, false);

    item = new PdfCollectionItem(schema);
    item.addItem("TITLE", id);
    fs.addCollectionItem(item);
    writer.addFileAttachment(fs);
    }

    document.close();
    return baos.toByteArray();
    }

    public byte createPage(String id, String allIds) throws DocumentException, IOException {
    Document document = new Document();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, baos);
    document.open();

    Paragraph p = new Paragraph(id,
    FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 48));
    document.add(p);

    for (String linkId : allIds) {
    if (!id.equals(linkId)) {
    PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
    sibbling.setEmbeddedFileName(linkId);
    PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
    Chunk chunk = new Chunk("Go to " + linkId + ".");
    PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
    dest.addFirst(new PdfNumber(0));
    PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
    chunk.setAction(action);
    document.add(chunk);
    }
    }

    document.close();
    return baos.toByteArray();
    }

    private static PdfCollectionSchema getCollectionSchema() {
    PdfCollectionSchema schema = new PdfCollectionSchema();

    PdfCollectionField size = new PdfCollectionField("File size", PdfCollectionField.SIZE);
    size.setOrder(2);
    schema.addField("SIZE", size);

    PdfCollectionField filename = new PdfCollectionField("File name", PdfCollectionField.FILENAME);
    filename.setVisible(false);
    schema.addField("FILE", filename);

    PdfCollectionField title = new PdfCollectionField("Title", PdfCollectionField.TEXT);
    title.setOrder(0);
    schema.addField("TITLE", title);

    return schema;
    }


    (EmbeddedLinks test testLinkToSibblingInPortfolio and helpers createPdf, createPage, and getCollectionSchema)



    This actually borrows a lot from the iText examples KubrickBox, KubrickCollection, and KubrickMovies.





    Tested with the current iText 5 development version 5.5.14-SNAPSHOT.






    share|improve this answer




























      0














      To navigate between documents in a portable collection, you have to employ Embedded Go-To actions. They in particular contain a Target dictionary which actually can be the start of a chain of target dictionaries to navigate between collection members with arbitrary embedding depths. To navigate between sibling documents, you need one target dictionary going up to the parent document containing another target dictionary going down again from that parent to the desired sibling. For details cf. ISO 32000-2 section 12.6.4.4 "Embedded Go-To actions".



      In iText 5.x you build such embedded goto actions like this:





      PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
      sibbling.setEmbeddedFileName(linkId);
      PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
      Chunk chunk = new Chunk("Go to " + linkId + ".");
      PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
      dest.addFirst(new PdfNumber(0));
      PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
      chunk.setAction(action);
      document.add(chunk);


      (from EmbeddedLinks helper createPage)



      For example the following test creates a portfolio with four documents A, B, C, and D which all contain links to each other:



      public void testLinkToSibblingInPortfolio() throws IOException, DocumentException {
      Files.write(new File("portfolio-with-embedded-gotos.pdf").toPath(), createPdf(new String {"A", "B", "C", "D"}));
      }

      public byte createPdf(String allIds) throws DocumentException, IOException {
      Document document = new Document();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      PdfWriter writer = PdfWriter.getInstance(document, baos);
      document.open();
      document.add(new Paragraph("This document contains a collection of PDFs."));

      PdfCollection collection = new PdfCollection(PdfCollection.HIDDEN);
      PdfCollectionSchema schema = getCollectionSchema();
      collection.setSchema(schema);
      PdfCollectionSort sort = new PdfCollectionSort("TITLE");
      collection.setSort(sort);
      collection.setInitialDocument("A");
      writer.setCollection(collection);

      PdfFileSpecification fs;
      PdfCollectionItem item;
      for (String id : allIds) {
      fs = PdfFileSpecification.fileEmbedded(writer, null,
      String.format("%s.pdf", id),
      createPage(id, allIds));
      fs.addDescription(id, false);

      item = new PdfCollectionItem(schema);
      item.addItem("TITLE", id);
      fs.addCollectionItem(item);
      writer.addFileAttachment(fs);
      }

      document.close();
      return baos.toByteArray();
      }

      public byte createPage(String id, String allIds) throws DocumentException, IOException {
      Document document = new Document();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      PdfWriter.getInstance(document, baos);
      document.open();

      Paragraph p = new Paragraph(id,
      FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 48));
      document.add(p);

      for (String linkId : allIds) {
      if (!id.equals(linkId)) {
      PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
      sibbling.setEmbeddedFileName(linkId);
      PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
      Chunk chunk = new Chunk("Go to " + linkId + ".");
      PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
      dest.addFirst(new PdfNumber(0));
      PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
      chunk.setAction(action);
      document.add(chunk);
      }
      }

      document.close();
      return baos.toByteArray();
      }

      private static PdfCollectionSchema getCollectionSchema() {
      PdfCollectionSchema schema = new PdfCollectionSchema();

      PdfCollectionField size = new PdfCollectionField("File size", PdfCollectionField.SIZE);
      size.setOrder(2);
      schema.addField("SIZE", size);

      PdfCollectionField filename = new PdfCollectionField("File name", PdfCollectionField.FILENAME);
      filename.setVisible(false);
      schema.addField("FILE", filename);

      PdfCollectionField title = new PdfCollectionField("Title", PdfCollectionField.TEXT);
      title.setOrder(0);
      schema.addField("TITLE", title);

      return schema;
      }


      (EmbeddedLinks test testLinkToSibblingInPortfolio and helpers createPdf, createPage, and getCollectionSchema)



      This actually borrows a lot from the iText examples KubrickBox, KubrickCollection, and KubrickMovies.





      Tested with the current iText 5 development version 5.5.14-SNAPSHOT.






      share|improve this answer


























        0












        0








        0







        To navigate between documents in a portable collection, you have to employ Embedded Go-To actions. They in particular contain a Target dictionary which actually can be the start of a chain of target dictionaries to navigate between collection members with arbitrary embedding depths. To navigate between sibling documents, you need one target dictionary going up to the parent document containing another target dictionary going down again from that parent to the desired sibling. For details cf. ISO 32000-2 section 12.6.4.4 "Embedded Go-To actions".



        In iText 5.x you build such embedded goto actions like this:





        PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
        sibbling.setEmbeddedFileName(linkId);
        PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
        Chunk chunk = new Chunk("Go to " + linkId + ".");
        PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
        dest.addFirst(new PdfNumber(0));
        PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
        chunk.setAction(action);
        document.add(chunk);


        (from EmbeddedLinks helper createPage)



        For example the following test creates a portfolio with four documents A, B, C, and D which all contain links to each other:



        public void testLinkToSibblingInPortfolio() throws IOException, DocumentException {
        Files.write(new File("portfolio-with-embedded-gotos.pdf").toPath(), createPdf(new String {"A", "B", "C", "D"}));
        }

        public byte createPdf(String allIds) throws DocumentException, IOException {
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("This document contains a collection of PDFs."));

        PdfCollection collection = new PdfCollection(PdfCollection.HIDDEN);
        PdfCollectionSchema schema = getCollectionSchema();
        collection.setSchema(schema);
        PdfCollectionSort sort = new PdfCollectionSort("TITLE");
        collection.setSort(sort);
        collection.setInitialDocument("A");
        writer.setCollection(collection);

        PdfFileSpecification fs;
        PdfCollectionItem item;
        for (String id : allIds) {
        fs = PdfFileSpecification.fileEmbedded(writer, null,
        String.format("%s.pdf", id),
        createPage(id, allIds));
        fs.addDescription(id, false);

        item = new PdfCollectionItem(schema);
        item.addItem("TITLE", id);
        fs.addCollectionItem(item);
        writer.addFileAttachment(fs);
        }

        document.close();
        return baos.toByteArray();
        }

        public byte createPage(String id, String allIds) throws DocumentException, IOException {
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();

        Paragraph p = new Paragraph(id,
        FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 48));
        document.add(p);

        for (String linkId : allIds) {
        if (!id.equals(linkId)) {
        PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
        sibbling.setEmbeddedFileName(linkId);
        PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
        Chunk chunk = new Chunk("Go to " + linkId + ".");
        PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
        dest.addFirst(new PdfNumber(0));
        PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
        chunk.setAction(action);
        document.add(chunk);
        }
        }

        document.close();
        return baos.toByteArray();
        }

        private static PdfCollectionSchema getCollectionSchema() {
        PdfCollectionSchema schema = new PdfCollectionSchema();

        PdfCollectionField size = new PdfCollectionField("File size", PdfCollectionField.SIZE);
        size.setOrder(2);
        schema.addField("SIZE", size);

        PdfCollectionField filename = new PdfCollectionField("File name", PdfCollectionField.FILENAME);
        filename.setVisible(false);
        schema.addField("FILE", filename);

        PdfCollectionField title = new PdfCollectionField("Title", PdfCollectionField.TEXT);
        title.setOrder(0);
        schema.addField("TITLE", title);

        return schema;
        }


        (EmbeddedLinks test testLinkToSibblingInPortfolio and helpers createPdf, createPage, and getCollectionSchema)



        This actually borrows a lot from the iText examples KubrickBox, KubrickCollection, and KubrickMovies.





        Tested with the current iText 5 development version 5.5.14-SNAPSHOT.






        share|improve this answer













        To navigate between documents in a portable collection, you have to employ Embedded Go-To actions. They in particular contain a Target dictionary which actually can be the start of a chain of target dictionaries to navigate between collection members with arbitrary embedding depths. To navigate between sibling documents, you need one target dictionary going up to the parent document containing another target dictionary going down again from that parent to the desired sibling. For details cf. ISO 32000-2 section 12.6.4.4 "Embedded Go-To actions".



        In iText 5.x you build such embedded goto actions like this:





        PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
        sibbling.setEmbeddedFileName(linkId);
        PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
        Chunk chunk = new Chunk("Go to " + linkId + ".");
        PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
        dest.addFirst(new PdfNumber(0));
        PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
        chunk.setAction(action);
        document.add(chunk);


        (from EmbeddedLinks helper createPage)



        For example the following test creates a portfolio with four documents A, B, C, and D which all contain links to each other:



        public void testLinkToSibblingInPortfolio() throws IOException, DocumentException {
        Files.write(new File("portfolio-with-embedded-gotos.pdf").toPath(), createPdf(new String {"A", "B", "C", "D"}));
        }

        public byte createPdf(String allIds) throws DocumentException, IOException {
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("This document contains a collection of PDFs."));

        PdfCollection collection = new PdfCollection(PdfCollection.HIDDEN);
        PdfCollectionSchema schema = getCollectionSchema();
        collection.setSchema(schema);
        PdfCollectionSort sort = new PdfCollectionSort("TITLE");
        collection.setSort(sort);
        collection.setInitialDocument("A");
        writer.setCollection(collection);

        PdfFileSpecification fs;
        PdfCollectionItem item;
        for (String id : allIds) {
        fs = PdfFileSpecification.fileEmbedded(writer, null,
        String.format("%s.pdf", id),
        createPage(id, allIds));
        fs.addDescription(id, false);

        item = new PdfCollectionItem(schema);
        item.addItem("TITLE", id);
        fs.addCollectionItem(item);
        writer.addFileAttachment(fs);
        }

        document.close();
        return baos.toByteArray();
        }

        public byte createPage(String id, String allIds) throws DocumentException, IOException {
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();

        Paragraph p = new Paragraph(id,
        FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 48));
        document.add(p);

        for (String linkId : allIds) {
        if (!id.equals(linkId)) {
        PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
        sibbling.setEmbeddedFileName(linkId);
        PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
        Chunk chunk = new Chunk("Go to " + linkId + ".");
        PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
        dest.addFirst(new PdfNumber(0));
        PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
        chunk.setAction(action);
        document.add(chunk);
        }
        }

        document.close();
        return baos.toByteArray();
        }

        private static PdfCollectionSchema getCollectionSchema() {
        PdfCollectionSchema schema = new PdfCollectionSchema();

        PdfCollectionField size = new PdfCollectionField("File size", PdfCollectionField.SIZE);
        size.setOrder(2);
        schema.addField("SIZE", size);

        PdfCollectionField filename = new PdfCollectionField("File name", PdfCollectionField.FILENAME);
        filename.setVisible(false);
        schema.addField("FILE", filename);

        PdfCollectionField title = new PdfCollectionField("Title", PdfCollectionField.TEXT);
        title.setOrder(0);
        schema.addField("TITLE", title);

        return schema;
        }


        (EmbeddedLinks test testLinkToSibblingInPortfolio and helpers createPdf, createPage, and getCollectionSchema)



        This actually borrows a lot from the iText examples KubrickBox, KubrickCollection, and KubrickMovies.





        Tested with the current iText 5 development version 5.5.14-SNAPSHOT.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 '18 at 12:24









        mklmkl

        55.9k1270150




        55.9k1270150
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440583%2flinking-pdfs-of-portable-collection-using-itext%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?