/www/examples/com/lowagie/examples/directcontent/optionalcontent/OrderedLayers.java

https://github.com/virasak/iText-4.2.0 · Java · 96 lines · 65 code · 6 blank · 25 comment · 0 complexity · 944529a6bb4fe39d524ff742b80cec9a MD5 · raw file

  1. /*
  2. * $Id: OrderedLayers.java 3838 2009-04-07 18:34:15Z mstorer $
  3. *
  4. * This code is part of the 'iText Tutorial'.
  5. * You can find the complete tutorial at the following address:
  6. * http://itextdocs.lowagie.com/tutorial/
  7. *
  8. * This code is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * itext-questions@lists.sourceforge.net
  13. */
  14. package com.lowagie.examples.directcontent.optionalcontent;
  15. import java.awt.Color;
  16. import java.io.FileOutputStream;
  17. import com.lowagie.text.Document;
  18. import com.lowagie.text.Element;
  19. import com.lowagie.text.Font;
  20. import com.lowagie.text.PageSize;
  21. import com.lowagie.text.Phrase;
  22. import com.lowagie.text.pdf.ColumnText;
  23. import com.lowagie.text.pdf.PdfArray;
  24. import com.lowagie.text.pdf.PdfContentByte;
  25. import com.lowagie.text.pdf.PdfDictionary;
  26. import com.lowagie.text.pdf.PdfLayer;
  27. import com.lowagie.text.pdf.PdfLayerMembership;
  28. import com.lowagie.text.pdf.PdfName;
  29. import com.lowagie.text.pdf.PdfOCProperties;
  30. import com.lowagie.text.pdf.PdfWriter;
  31. /**
  32. * Demonstrates how to order optional content groups.
  33. */
  34. public class OrderedLayers {
  35. /**
  36. * Demonstrates how to order optional content groups.
  37. * @param args no arguments needed
  38. */
  39. public static void main(String[] args) {
  40. System.out.println("Ordering optional content groups");
  41. try {
  42. // step 1
  43. Document document = new Document(PageSize.A4, 50, 50, 50, 50);
  44. // step 2
  45. PdfWriter writer = PdfWriter.getInstance(document,
  46. new FileOutputStream("orderedlayers.pdf"));
  47. writer.setPdfVersion(PdfWriter.VERSION_1_5);
  48. writer.setViewerPreferences(PdfWriter.PageModeUseOC);
  49. // step 3
  50. document.open();
  51. // step 4
  52. PdfContentByte cb = writer.getDirectContent();
  53. Phrase explanation = new Phrase("Ordered layers", new Font(
  54. Font.HELVETICA, 20, Font.BOLD, Color.red));
  55. ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50,
  56. 650, 0);
  57. PdfLayer l1 = new PdfLayer("Layer 1", writer);
  58. PdfLayer l2 = new PdfLayer("Layer 2", writer);
  59. PdfLayer l3 = new PdfLayer("Layer 3", writer);
  60. PdfLayerMembership m1 = new PdfLayerMembership(writer);
  61. m1.addMember(l2);
  62. m1.addMember(l3);
  63. Phrase p1 = new Phrase("Text in layer 1");
  64. Phrase p2 = new Phrase("Text in layer 2 or layer 3");
  65. Phrase p3 = new Phrase("Text in layer 3");
  66. cb.beginLayer(l1);
  67. ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0);
  68. cb.endLayer();
  69. cb.beginLayer(m1);
  70. ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
  71. cb.endLayer();
  72. cb.beginLayer(l3);
  73. ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
  74. cb.endLayer();
  75. cb.sanityCheck();
  76. PdfOCProperties p = writer.getOCProperties();
  77. PdfArray order = new PdfArray();
  78. order.add(l1.getRef());
  79. order.add(l2.getRef());
  80. order.add(l3.getRef());
  81. PdfDictionary d = new PdfDictionary();
  82. d.put(PdfName.ORDER, order);
  83. p.put(PdfName.D, d);
  84. // step 5
  85. document.close();
  86. } catch (Exception de) {
  87. de.printStackTrace();
  88. }
  89. }
  90. }