iText 7 Building Blocks - Chapter 6: actions, destinations, bookmarks

This is a code example of iText PDF, discover more.

14th August 2016
iText PDF

Switch code for this example

C06E01_URIAction.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.highlevel.util.CsvTo2DList;
  8. import com.itextpdf.io.image.ImageDataFactory;
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfWriter;
  11. import com.itextpdf.kernel.pdf.action.PdfAction;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.element.Image;
  14. import com.itextpdf.layout.element.ListItem;
  15. import com.itextpdf.layout.element.Paragraph;
  16. import com.itextpdf.layout.property.ListNumberingType;
  17. import com.itextpdf.test.annotations.WrapToTest;
  18.  
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.List;
  22.  
  23. /**
  24.  * @author Bruno Lowagie (iText Software)
  25.  */
  26. @WrapToTest
  27. public class C06E01_URIAction {
  28.    
  29.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  30.     public static final String DEST = "results/chapter06/jekyll_hyde_action_uri.pdf";
  31.        
  32.     public static void main(String args[]) throws IOException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new C06E01_URIAction().createPdf(DEST);
  36.     }
  37.    
  38.     public void createPdf(String dest) throws IOException {
  39.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  40.         Document document = new Document(pdf);
  41.         List resultSet = CsvTo2DList.convert(SRC, "|");
  42.         resultSet.remove(0);
  43.         com.itextpdf.layout.element.List list =
  44.             new com.itextpdf.layout.element.List(ListNumberingType.DECIMAL);
  45.         for (List record : resultSet) {
  46.             ListItem li = new ListItem();
  47.             li.setKeepTogether(true);
  48.             li.add(new Paragraph().setFontSize(14).add(record.get(2)))
  49.                 .add(new Paragraph(String.format(
  50.                     "Directed by %s (%s, %s)",
  51.                     record.get(3), record.get(4), record.get(1))));
  52.             File file = new File(String.format(
  53.                 "src/main/resources/img/%s.jpg", record.get(0)));
  54.             if (file.exists()) {
  55.                 Image img = new Image(ImageDataFactory.create(file.getPath()));
  56.                 img.scaleToFit(10000, 120);
  57.                 li.add(img);
  58.             }
  59.             String url = String.format(
  60.                 "http://www.imdb.com/title/tt%s", record.get(0));
  61.             li.setAction(PdfAction.createURI(url));
  62.             list.add(li);
  63.         }
  64.         document.add(list);
  65.         document.close();
  66.     }
  67.    
  68. }
C06E02_NamedAction.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.highlevel.util.CsvTo2DList;
  8. import com.itextpdf.io.image.ImageDataFactory;
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfName;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.kernel.pdf.action.PdfAction;
  13. import com.itextpdf.layout.Document;
  14. import com.itextpdf.layout.element.Image;
  15. import com.itextpdf.layout.element.ListItem;
  16. import com.itextpdf.layout.element.Paragraph;
  17. import com.itextpdf.layout.property.ListNumberingType;
  18. import com.itextpdf.test.annotations.WrapToTest;
  19.  
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.List;
  23.  
  24. /**
  25.  * @author Bruno Lowagie (iText Software)
  26.  */
  27. @WrapToTest
  28. public class C06E02_NamedAction {
  29.    
  30.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  31.     public static final String DEST = "results/chapter06/jekyll_hyde_named.pdf";
  32.        
  33.     public static void main(String args[]) throws IOException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new C06E02_NamedAction().createPdf(DEST);
  37.     }
  38.    
  39.     public void createPdf(String dest) throws IOException {
  40.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  41.         Document document = new Document(pdf);
  42.         Paragraph p = new Paragraph()
  43.             .add("Go to last page")
  44.             .setAction(PdfAction.createNamed(PdfName.LastPage));
  45.         document.add(p);
  46.         List resultSet = CsvTo2DList.convert(SRC, "|");
  47.         resultSet.remove(0);
  48.         com.itextpdf.layout.element.List list =
  49.             new com.itextpdf.layout.element.List(ListNumberingType.DECIMAL);
  50.         for (List record : resultSet) {
  51.             ListItem li = new ListItem();
  52.             li.setKeepTogether(true);
  53.             li.add(new Paragraph().setFontSize(14).add(record.get(2)))
  54.                 .add(new Paragraph(String.format(
  55.                     "Directed by %s (%s, %s)",
  56.                     record.get(3), record.get(4), record.get(1))));
  57.             File file = new File(String.format(
  58.                 "src/main/resources/img/%s.jpg", record.get(0)));
  59.             if (file.exists()) {
  60.                 Image img = new Image(ImageDataFactory.create(file.getPath()));
  61.                 img.scaleToFit(10000, 120);
  62.                 li.add(img);
  63.             }
  64.             list.add(li);
  65.         }
  66.         document.add(list);
  67.         p = new Paragraph()
  68.             .add("Go to first page")
  69.             .setAction(PdfAction.createNamed(PdfName.FirstPage));
  70.         document.add(p);
  71.         document.close();
  72.     }
  73.    
  74. }
C06E03_TOC_GoToPage.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.kernel.font.PdfFont;
  9. import com.itextpdf.kernel.font.PdfFontFactory;
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.kernel.pdf.action.PdfAction;
  13. import com.itextpdf.kernel.pdf.canvas.draw.DottedLine;
  14. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  15. import com.itextpdf.layout.Document;
  16. import com.itextpdf.layout.element.AreaBreak;
  17. import com.itextpdf.layout.element.Paragraph;
  18. import com.itextpdf.layout.element.Tab;
  19. import com.itextpdf.layout.element.TabStop;
  20. import com.itextpdf.layout.hyphenation.HyphenationConfig;
  21. import com.itextpdf.layout.property.AreaBreakType;
  22. import com.itextpdf.layout.property.TabAlignment;
  23. import com.itextpdf.layout.property.TextAlignment;
  24. import com.itextpdf.test.annotations.WrapToTest;
  25.  
  26. import java.io.BufferedReader;
  27. import java.io.File;
  28. import java.io.FileReader;
  29. import java.io.IOException;
  30. import java.util.AbstractMap.SimpleEntry;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33.  
  34. /**
  35.  * @author Bruno Lowagie (iText Software)
  36.  */
  37. @WrapToTest
  38. public class C06E03_TOC_GoToPage {
  39.     public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
  40.     public static final String DEST = "results/chapter06/jekyll_hyde_toc1.pdf";
  41.    
  42.     public static void main(String args[]) throws IOException {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new C06E03_TOC_GoToPage().createPdf(DEST);
  46.     }
  47.    
  48.     public void createPdf(String dest) throws IOException {
  49.         //Initialize PDF document
  50.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  51.        
  52.         // Initialize document
  53.         Document document = new Document(pdf);
  54.         PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  55.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  56.         document.setTextAlignment(TextAlignment.JUSTIFIED)
  57.             .setHyphenation(new HyphenationConfig("en", "uk", 3, 3))
  58.             .setFont(font)
  59.             .setFontSize(11);
  60.        
  61.         BufferedReader br = new BufferedReader(new FileReader(SRC));
  62.         String name, line;
  63.         Paragraph p;
  64.         boolean title = true;
  65.         int counter = 0;
  66.         List toc = new ArrayList();
  67.         while ((line = br.readLine()) != null) {
  68.             p = new Paragraph(line);
  69.             p.setKeepTogether(true);
  70.             if (title) {
  71.                 name = String.format("title%02d", counter++);
  72.                 p.setFont(bold).setFontSize(12)
  73.                     .setKeepWithNext(true)
  74.                     .setDestination(name);
  75.                 title = false;
  76.                 document.add(p);
  77.                 // The following line is problematic when using setKeepWithNext
  78.                 toc.add(new SimpleEntry(line, pdf.getNumberOfPages()));
  79.             }
  80.             else {
  81.                 p.setFirstLineIndent(36);
  82.                 if (line.isEmpty()) {
  83.                     p.setMarginBottom(12);
  84.                     title = true;
  85.                 }
  86.                 else {
  87.                     p.setMarginBottom(0);
  88.                 }
  89.                 document.add(p);
  90.             }
  91.         }
  92.        
  93.         document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
  94.        
  95.         p = new Paragraph().setFont(bold).add("Table of Contents");
  96.         document.add(p);
  97.        
  98.         toc.remove(0);
  99.         List tabstops = new ArrayList();
  100.         tabstops.add(new TabStop(580, TabAlignment.RIGHT, new DottedLine()));
  101.         for (SimpleEntry entry : toc) {
  102.             p = new Paragraph()
  103.                 .addTabStops(tabstops)
  104.                 .add(entry.getKey())
  105.                 .add(new Tab())
  106.                 .add(String.valueOf(entry.getValue()))
  107.                 .setAction(PdfAction.createGoTo(
  108.                         PdfExplicitDestination.createFit(entry.getValue())));
  109.             document.add(p);
  110.         }
  111.        
  112.         //Close document
  113.         document.close();
  114.     }
  115.  
  116. }
C06E04_TOC_GoToNamed.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.kernel.font.PdfFont;
  9. import com.itextpdf.kernel.font.PdfFontFactory;
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.kernel.pdf.action.PdfAction;
  13. import com.itextpdf.kernel.pdf.canvas.draw.DottedLine;
  14. import com.itextpdf.layout.Document;
  15. import com.itextpdf.layout.element.AreaBreak;
  16. import com.itextpdf.layout.element.Paragraph;
  17. import com.itextpdf.layout.element.Tab;
  18. import com.itextpdf.layout.element.TabStop;
  19. import com.itextpdf.layout.hyphenation.HyphenationConfig;
  20. import com.itextpdf.layout.layout.LayoutContext;
  21. import com.itextpdf.layout.layout.LayoutResult;
  22. import com.itextpdf.layout.property.AreaBreakType;
  23. import com.itextpdf.layout.property.TabAlignment;
  24. import com.itextpdf.layout.property.TextAlignment;
  25. import com.itextpdf.layout.renderer.ParagraphRenderer;
  26. import com.itextpdf.test.annotations.WrapToTest;
  27.  
  28. import java.io.BufferedReader;
  29. import java.io.File;
  30. import java.io.FileReader;
  31. import java.io.IOException;
  32. import java.util.AbstractMap.SimpleEntry;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35.  
  36. /**
  37.  * @author Bruno Lowagie (iText Software)
  38.  */
  39. @WrapToTest
  40. public class C06E04_TOC_GoToNamed {
  41.     public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
  42.     public static final String DEST = "results/chapter06/jekyll_hyde_toc2.pdf";
  43.    
  44.     public static void main(String args[]) throws IOException {
  45.         File file = new File(DEST);
  46.         file.getParentFile().mkdirs();
  47.         new C06E04_TOC_GoToNamed().createPdf(DEST);
  48.     }
  49.    
  50.     public void createPdf(String dest) throws IOException {
  51.         //Initialize PDF document
  52.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  53.        
  54.         // Initialize document
  55.         Document document = new Document(pdf);
  56.         PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  57.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  58.         document.setTextAlignment(TextAlignment.JUSTIFIED)
  59.             .setHyphenation(new HyphenationConfig("en", "uk", 3, 3))
  60.             .setFont(font)
  61.             .setFontSize(11);
  62.        
  63.         BufferedReader br = new BufferedReader(new FileReader(SRC));
  64.         String name, line;
  65.         Paragraph p;
  66.         boolean title = true;
  67.         int counter = 0;
  68.         List toc = new ArrayList();
  69.         while ((line = br.readLine()) != null) {
  70.             p = new Paragraph(line);
  71.             p.setKeepTogether(true);
  72.             if (title) {
  73.                 name = String.format("title%02d", counter++);
  74.                 SimpleEntry titlePage
  75.                         = new SimpleEntry(line, pdf.getNumberOfPages());
  76.                 p.setFont(bold).setFontSize(12)
  77.                     .setKeepWithNext(true)
  78.                     .setDestination(name)
  79.                     .setNextRenderer(new UpdatePageRenderer(p, titlePage));
  80.                 title = false;
  81.                 document.add(p);
  82.                 toc.add(new SimpleEntry(name, titlePage));
  83.             }
  84.             else {
  85.                 p.setFirstLineIndent(36);
  86.                 if (line.isEmpty()) {
  87.                     p.setMarginBottom(12);
  88.                     title = true;
  89.                 }
  90.                 else {
  91.                     p.setMarginBottom(0);
  92.                 }
  93.                 document.add(p);
  94.             }
  95.         }
  96.        
  97.         document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
  98.        
  99.         p = new Paragraph().setFont(bold)
  100.             .add("Table of Contents").setDestination("toc");
  101.         document.add(p);
  102.        
  103.         toc.remove(0);
  104.         List tabstops = new ArrayList();
  105.         tabstops.add(new TabStop(580, TabAlignment.RIGHT, new DottedLine()));
  106.         for (SimpleEntry entry : toc) {
  107.             SimpleEntry text = entry.getValue();
  108.             p = new Paragraph()
  109.                 .addTabStops(tabstops)
  110.                 .add(text.getKey())
  111.                 .add(new Tab())
  112.                 .add(String.valueOf(text.getValue()))
  113.                 .setAction(PdfAction.createGoTo(entry.getKey()));
  114.             document.add(p);
  115.         }
  116.        
  117.         //Close document
  118.         document.close();
  119.     }
  120.        
  121.     protected class UpdatePageRenderer extends ParagraphRenderer {
  122.         protected SimpleEntry entry;
  123.  
  124.         public UpdatePageRenderer(Paragraph modelElement, SimpleEntry entry) {
  125.             super(modelElement);
  126.             this.entry = entry;
  127.         }
  128.  
  129.         @Override
  130.         public LayoutResult layout(LayoutContext layoutContext) {
  131.             LayoutResult result = super.layout(layoutContext);
  132.             entry.setValue(layoutContext.getArea().getPageNumber());
  133.             return result;
  134.         }
  135.     }
  136. }
C06E05_RemoteGoto.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.kernel.color.Color;
  8. import com.itextpdf.kernel.pdf.PdfDocument;
  9. import com.itextpdf.kernel.pdf.PdfWriter;
  10. import com.itextpdf.kernel.pdf.action.PdfAction;
  11. import com.itextpdf.layout.Document;
  12. import com.itextpdf.layout.element.Link;
  13. import com.itextpdf.layout.element.Paragraph;
  14. import com.itextpdf.test.annotations.WrapToTest;
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  18. /**
  19.  * @author iText
  20.  */
  21. @WrapToTest
  22. public class C06E05_RemoteGoto {
  23.    
  24.     public static final String DEST = "results/chapter06/jekyll_hyde_remote.pdf";
  25.    
  26.    
  27.     public static void main(String args[]) throws IOException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new C06E05_RemoteGoto().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException {
  34.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  35.         Document document = new Document(pdf);
  36.         Link link1 = new Link("Strange Case of Dr. Jekyll and Mr. Hyde",
  37.             PdfAction.createGoToR(
  38.                 new File(C06E04_TOC_GoToNamed.DEST).getName(), 1, true));
  39.         Link link2 = new Link("table of contents",
  40.             PdfAction.createGoToR(
  41.                 new File(C06E04_TOC_GoToNamed.DEST).getName(), "toc", false));
  42.         Paragraph p = new Paragraph()
  43.             .add("Read the amazing horror story ")
  44.             .add(link1.setFontColor(Color.BLUE))
  45.             .add(" or, if you're too afraid to start reading the story, read the ")
  46.             .add(link2.setFontColor(Color.BLUE))
  47.             .add(".");
  48.         document.add(p);
  49.         document.close();
  50.     }
  51. }
C06E06_JavaScript.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.kernel.color.Color;
  8. import com.itextpdf.kernel.pdf.PdfDocument;
  9. import com.itextpdf.kernel.pdf.PdfWriter;
  10. import com.itextpdf.kernel.pdf.action.PdfAction;
  11. import com.itextpdf.layout.Document;
  12. import com.itextpdf.layout.element.Link;
  13. import com.itextpdf.layout.element.Paragraph;
  14. import com.itextpdf.test.annotations.WrapToTest;
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  18. /**
  19.  * @author iText
  20.  */
  21. @WrapToTest
  22. public class C06E06_JavaScript {
  23.    
  24.     public static final String DEST = "results/chapter06/jekyll_hyde_javascript.pdf";
  25.    
  26.    
  27.     public static void main(String args[]) throws IOException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new C06E06_JavaScript().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException {
  34.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  35.         Document document = new Document(pdf);
  36.         Link link = new Link("here",
  37.             PdfAction.createJavaScript("app.alert('Boo!');"));
  38.         Paragraph p = new Paragraph()
  39.             .add("Click ")
  40.             .add(link.setFontColor(Color.BLUE))
  41.             .add(" if you want to be scared.");
  42.         document.add(p);
  43.         document.close();
  44.     }
  45. }
C06E07_ChainedActions.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.kernel.color.Color;
  8. import com.itextpdf.kernel.pdf.PdfDocument;
  9. import com.itextpdf.kernel.pdf.PdfWriter;
  10. import com.itextpdf.kernel.pdf.action.PdfAction;
  11. import com.itextpdf.layout.Document;
  12. import com.itextpdf.layout.element.Link;
  13. import com.itextpdf.layout.element.Paragraph;
  14. import com.itextpdf.test.annotations.WrapToTest;
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  18. /**
  19.  * @author iText
  20.  */
  21. @WrapToTest
  22. public class C06E07_ChainedActions {
  23.    
  24.     public static final String DEST = "results/chapter06/jekyll_hyde_chained.pdf";
  25.    
  26.    
  27.     public static void main(String args[]) throws IOException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new C06E07_ChainedActions().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException {
  34.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  35.         Document document = new Document(pdf);
  36.         PdfAction action = PdfAction.createJavaScript("app.alert('Boo');");
  37.         action.next(PdfAction.createGoToR(
  38.                 new File(C06E04_TOC_GoToNamed.DEST).getName(), 1, true));
  39.         Link link = new Link("here", action);
  40.         Paragraph p = new Paragraph()
  41.             .add("Click ")
  42.             .add(link.setFontColor(Color.BLUE))
  43.             .add(" if you want to be scared.");
  44.         document.add(p);
  45.         document.close();
  46.     }
  47. }
C06E08_ExplicitDestinations.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks.
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.kernel.pdf.PdfDocument;
  8. import com.itextpdf.kernel.pdf.PdfWriter;
  9. import com.itextpdf.kernel.pdf.navigation.PdfDestination;
  10. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  11. import com.itextpdf.layout.Document;
  12. import com.itextpdf.layout.element.AreaBreak;
  13. import com.itextpdf.layout.element.Link;
  14. import com.itextpdf.layout.element.Paragraph;
  15. import com.itextpdf.layout.property.AreaBreakType;
  16. import com.itextpdf.test.annotations.WrapToTest;
  17. import java.io.File;
  18. import java.io.IOException;
  19.  
  20. /**
  21.  * @author iText
  22.  */
  23. @WrapToTest
  24. public class C06E08_ExplicitDestinations {
  25.     public static final String DEST = "results/chapter06/jekyll_hyde_explicit.pdf";
  26.    
  27.     public static void main(String args[]) throws IOException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new C06E08_ExplicitDestinations().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException {
  34.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  35.         Document document = new Document(pdf);
  36.        
  37.         PdfDestination jekyll = PdfExplicitDestination.createFitH(1, 416);
  38.         PdfDestination hyde = PdfExplicitDestination.createXYZ(1, 150, 516, 2);
  39.         PdfDestination jekyll2 = PdfExplicitDestination.createFitR(2, 50, 380, 130, 440);
  40.         document.add(new Paragraph()
  41.                 .add(new Link("Link to Dr. Jekyll", jekyll)));
  42.         document.add(new Paragraph()
  43.                 .add(new Link("Link to Mr. Hyde", hyde)));
  44.         document.add(new Paragraph()
  45.                 .add(new Link("Link to Dr. Jekyll on page 2", jekyll2)));
  46.        
  47.         document.add(new Paragraph()
  48.             .setFixedPosition(50, 400, 80)
  49.             .add("Dr. Jekyll"));
  50.         document.add(new Paragraph()
  51.             .setFixedPosition(150, 500, 80)
  52.             .add("Mr. Hyde"));
  53.         document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
  54.         document.add(new Paragraph()
  55.             .setFixedPosition(50, 400, 80)
  56.             .add("Dr. Jekyll on page 2"));
  57.        
  58.         document.close();
  59.     }
  60.    
  61. }
C06E09_Annotation.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.kernel.geom.Rectangle;
  8. import com.itextpdf.kernel.pdf.PdfDocument;
  9. import com.itextpdf.kernel.pdf.PdfWriter;
  10. import com.itextpdf.kernel.pdf.action.PdfAction;
  11. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  12. import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
  13. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  14. import com.itextpdf.layout.Document;
  15. import com.itextpdf.layout.element.AreaBreak;
  16. import com.itextpdf.layout.element.Link;
  17. import com.itextpdf.layout.element.Paragraph;
  18. import com.itextpdf.layout.property.AreaBreakType;
  19. import com.itextpdf.test.annotations.WrapToTest;
  20. import java.io.File;
  21. import java.io.IOException;
  22.  
  23. /**
  24.  * @author iText
  25.  */
  26. @WrapToTest
  27. public class C06E09_Annotation {
  28.    
  29.     public static final String DEST = "results/chapter06/jekyll_hyde_annotation.pdf";
  30.    
  31.    
  32.     public static void main(String args[]) throws IOException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new C06E09_Annotation().createPdf(DEST);
  36.     }
  37.    
  38.     public void createPdf(String dest) throws IOException {
  39.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  40.         Document document = new Document(pdf);
  41.         PdfAction js = PdfAction.createJavaScript("app.alert('Boo!');");
  42.         PdfAnnotation la1 = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0))
  43.             .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
  44.             .setAction(js).setBorderStyle(PdfAnnotation.STYLE_UNDERLINE);
  45.         Link link1 = new Link("here", (PdfLinkAnnotation)la1);
  46.         document.add(new Paragraph()
  47.             .add("Click ")
  48.             .add(link1)
  49.             .add(" if you want to be scared."));
  50.         PdfAnnotation la2 = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0))
  51.             .setDestination(PdfExplicitDestination.createFit(2))
  52.             .setHighlightMode(PdfAnnotation.HIGHLIGHT_PUSH)
  53.             .setBorderStyle(PdfAnnotation.STYLE_INSET);
  54.         Link link2 = new Link("next page", (PdfLinkAnnotation)la2);
  55.         document.add(new Paragraph()
  56.             .add("Go to the ")
  57.             .add(link2)
  58.             .add(" if you're too scared."));
  59.         document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
  60.         document.add(new Paragraph().add("There, there, everything is OK."));
  61.         document.close();
  62.     }
  63. }
C06E10_TOC_OutlinesNames.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.kernel.font.PdfFont;
  9. import com.itextpdf.kernel.font.PdfFontFactory;
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfName;
  12. import com.itextpdf.kernel.pdf.PdfOutline;
  13. import com.itextpdf.kernel.pdf.PdfString;
  14. import com.itextpdf.kernel.pdf.PdfWriter;
  15. import com.itextpdf.kernel.pdf.navigation.PdfDestination;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Paragraph;
  18. import com.itextpdf.layout.hyphenation.HyphenationConfig;
  19. import com.itextpdf.layout.property.TextAlignment;
  20. import com.itextpdf.test.annotations.WrapToTest;
  21.  
  22. import java.io.BufferedReader;
  23. import java.io.File;
  24. import java.io.FileReader;
  25. import java.io.IOException;
  26.  
  27. /**
  28.  * @author Bruno Lowagie (iText Software)
  29.  */
  30. @WrapToTest
  31. public class C06E10_TOC_OutlinesNames {
  32.     public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
  33.     public static final String DEST = "results/chapter06/jekyll_hyde_outline1.pdf";
  34.    
  35.     public static void main(String args[]) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new C06E10_TOC_OutlinesNames().createPdf(DEST);
  39.     }
  40.    
  41.     public void createPdf(String dest) throws IOException {
  42.         //Initialize PDF document
  43.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  44.         pdf.getCatalog().setPageMode(PdfName.UseOutlines);
  45.         // Initialize document
  46.         Document document = new Document(pdf);
  47.         PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  48.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  49.         document.setTextAlignment(TextAlignment.JUSTIFIED)
  50.             .setHyphenation(new HyphenationConfig("en", "uk", 3, 3))
  51.             .setFont(font)
  52.             .setFontSize(11);
  53.        
  54.         BufferedReader br = new BufferedReader(new FileReader(SRC));
  55.         String name, line;
  56.         Paragraph p;
  57.         boolean title = true;
  58.         int counter = 0;
  59.         PdfOutline outline = null;
  60.         while ((line = br.readLine()) != null) {
  61.             p = new Paragraph(line);
  62.             p.setKeepTogether(true);
  63.             if (title) {
  64.                 name = String.format("title%02d", counter++);
  65.                 outline = createOutline(outline, pdf, line, name);
  66.                 p.setFont(bold).setFontSize(12)
  67.                     .setKeepWithNext(true)
  68.                     .setDestination(name);
  69.                 title = false;
  70.                 document.add(p);
  71.             }
  72.             else {
  73.                 p.setFirstLineIndent(36);
  74.                 if (line.isEmpty()) {
  75.                     p.setMarginBottom(12);
  76.                     title = true;
  77.                 }
  78.                 else {
  79.                     p.setMarginBottom(0);
  80.                 }
  81.                 document.add(p);
  82.             }
  83.         }
  84.        
  85.         //Close document
  86.         document.close();
  87.     }
  88.  
  89.     public PdfOutline createOutline(PdfOutline outline, PdfDocument pdf, String title, String name) {
  90.         if (outline ==  null) {
  91.             outline = pdf.getOutlines(false);
  92.             outline = outline.addOutline(title);
  93.             outline.addDestination(PdfDestination.makeDestination(new PdfString(name)));
  94.             return outline;
  95.         }
  96.         PdfOutline kid = outline.addOutline(title);
  97.         kid.addDestination(PdfDestination.makeDestination(new PdfString(name)));
  98.         return outline;
  99.     }
  100.    
  101. }
C06E11_TOC_OutlinesDestinations.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.kernel.font.PdfFont;
  9. import com.itextpdf.kernel.font.PdfFontFactory;
  10. import com.itextpdf.kernel.geom.Rectangle;
  11. import com.itextpdf.kernel.pdf.PdfDocument;
  12. import com.itextpdf.kernel.pdf.PdfName;
  13. import com.itextpdf.kernel.pdf.PdfOutline;
  14. import com.itextpdf.kernel.pdf.PdfWriter;
  15. import com.itextpdf.kernel.pdf.navigation.PdfDestination;
  16. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.layout.hyphenation.HyphenationConfig;
  20. import com.itextpdf.layout.property.TextAlignment;
  21. import com.itextpdf.layout.renderer.DrawContext;
  22. import com.itextpdf.layout.renderer.ParagraphRenderer;
  23. import com.itextpdf.test.annotations.WrapToTest;
  24.  
  25. import java.io.BufferedReader;
  26. import java.io.File;
  27. import java.io.FileReader;
  28. import java.io.IOException;
  29.  
  30. /**
  31.  * @author Bruno Lowagie (iText Software)
  32.  */
  33. @WrapToTest
  34. public class C06E11_TOC_OutlinesDestinations {
  35.     public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
  36.     public static final String DEST = "results/chapter06/jekyll_hyde_outline2.pdf";
  37.    
  38.     public static void main(String args[]) throws IOException {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new C06E11_TOC_OutlinesDestinations().createPdf(DEST);
  42.     }
  43.    
  44.     public void createPdf(String dest) throws IOException {
  45.         //Initialize PDF document
  46.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  47.         pdf.getCatalog().setPageMode(PdfName.UseOutlines);
  48.         // Initialize document
  49.         Document document = new Document(pdf);
  50.         PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  51.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  52.         document.setTextAlignment(TextAlignment.JUSTIFIED)
  53.             .setHyphenation(new HyphenationConfig("en", "uk", 3, 3))
  54.             .setFont(font)
  55.             .setFontSize(11);
  56.        
  57.         BufferedReader br = new BufferedReader(new FileReader(SRC));
  58.         String line;
  59.         Paragraph p;
  60.         boolean title = true;
  61.         PdfOutline outline = null;
  62.         while ((line = br.readLine()) != null) {
  63.             p = new Paragraph(line);
  64.             p.setKeepTogether(true);
  65.             if (title) {
  66.                 outline = createOutline(outline, pdf, line, p);
  67.                 p.setFont(bold).setFontSize(12)
  68.                     .setKeepWithNext(true);
  69.                 title = false;
  70.                 document.add(p);
  71.             }
  72.             else {
  73.                 p.setFirstLineIndent(36);
  74.                 if (line.isEmpty()) {
  75.                     p.setMarginBottom(12);
  76.                     title = true;
  77.                 }
  78.                 else {
  79.                     p.setMarginBottom(0);
  80.                 }
  81.                 document.add(p);
  82.             }
  83.         }
  84.        
  85.         //Close document
  86.         document.close();
  87.     }
  88.  
  89.     public PdfOutline createOutline(PdfOutline outline, PdfDocument pdf, String title, Paragraph p) {
  90.         if (outline ==  null) {
  91.             outline = pdf.getOutlines(false);
  92.             outline = outline.addOutline(title);
  93.             return outline;
  94.         }
  95.         OutlineRenderer renderer = new OutlineRenderer(p, title, outline);
  96.         p.setNextRenderer(renderer);
  97.         return outline;
  98.     }
  99.        
  100.     protected class OutlineRenderer extends ParagraphRenderer {
  101.         protected PdfOutline parent;
  102.         protected String title;
  103.  
  104.         public OutlineRenderer(Paragraph modelElement, String title, PdfOutline parent) {
  105.             super(modelElement);
  106.             this.title = title;
  107.             this.parent = parent;
  108.         }
  109.  
  110.         @Override
  111.         public void draw(DrawContext drawContext) {
  112.             super.draw(drawContext);
  113.             Rectangle rect = getOccupiedAreaBBox();
  114.             PdfDestination dest =
  115.                 PdfExplicitDestination.createFitH(
  116.                     drawContext.getDocument().getLastPage(),
  117.                     rect.getTop());
  118.             PdfOutline outline = parent.addOutline(title);
  119.             outline.addDestination(dest);
  120.         }
  121.     }
  122. }
C06E12_Outlines.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter06;
  6.  
  7. import com.itextpdf.highlevel.util.CsvTo2DList;
  8. import com.itextpdf.kernel.color.Color;
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfName;
  11. import com.itextpdf.kernel.pdf.PdfOutline;
  12. import com.itextpdf.kernel.pdf.PdfWriter;
  13. import com.itextpdf.kernel.pdf.action.PdfAction;
  14. import com.itextpdf.test.annotations.WrapToTest;
  15.  
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.List;
  19.  
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class C06E12_Outlines {
  25.    
  26.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  27.     public static final String DEST = "results/chapter06/jekyll_hyde_outlines.pdf";
  28.        
  29.     public static void main(String args[]) throws IOException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new C06E12_Outlines().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException {
  36.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  37.         pdf.addNewPage();
  38.         pdf.getCatalog().setPageMode(PdfName.UseOutlines);
  39.         PdfOutline root = pdf.getOutlines(false);
  40.         List resultSet = CsvTo2DList.convert(SRC, "|");
  41.         resultSet.remove(0);
  42.         for (List record : resultSet) {
  43.             PdfOutline movie = root.addOutline(record.get(2));
  44.             PdfOutline imdb = movie.addOutline("Link to IMDB");
  45.             imdb.setColor(Color.BLUE);
  46.             imdb.setStyle(PdfOutline.FLAG_BOLD);
  47.             String url = String.format(
  48.                 "http://www.imdb.com/title/tt%s", record.get(0));
  49.             imdb.addAction(PdfAction.createURI(url));
  50.             PdfOutline info = movie.addOutline("More info:");
  51.             info.setOpen(false);
  52.             info.setStyle(PdfOutline.FLAG_ITALIC);
  53.             PdfOutline director = info.addOutline("Directed by " + record.get(3));
  54.             director.setColor(Color.RED);
  55.             PdfOutline place = info.addOutline("Produced in " + record.get(4));
  56.             place.setColor(Color.MAGENTA);
  57.             PdfOutline year = info.addOutline("Released in " + record.get(1));
  58.             year.setColor(Color.DARK_GRAY);
  59.         }
  60.         pdf.close();
  61.     }
  62.    
  63. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now