Package org.pdfclown.documents.contents.composition

Examples of org.pdfclown.documents.contents.composition.BlockComposer


    document.getPages().add(page);
    Dimension2D pageSize = page.getSize();

    PrimitiveComposer composer = new PrimitiveComposer(page);
    {
      BlockComposer blockComposer = new BlockComposer(composer);
      blockComposer.setHyphenation(true);
      blockComposer.begin(
        new Rectangle2D.Double(
          Margin,
          Margin,
          (float)pageSize.getWidth() - Margin * 2,
          (float)pageSize.getHeight() - Margin * 2
          ),
        AlignmentXEnum.Justify,
        AlignmentYEnum.Top
        );
      StandardType1Font bodyFont = new StandardType1Font(
        document,
        StandardType1Font.FamilyEnum.Courier,
        true,
        false
        );
      composer.setFont(bodyFont,32);
      blockComposer.showText("Inline image sample"); blockComposer.showBreak();
      composer.setFont(bodyFont,16);
      blockComposer.showText("Showing the GNU logo as an inline image within the page content stream.");
      blockComposer.end();
    }
    // Showing the 'GNU' image...
    {
      // Instantiate the image!
      Image image = Image.get(getInputPath() + java.io.File.separator + "images" + java.io.File.separator + "gnu.jpg");
View Full Code Here


    // 2.1. Create a content composer for the page!
    PrimitiveComposer composer = new PrimitiveComposer(page);

    // 2.2. Create a block composer!
    BlockComposer blockComposer = new BlockComposer(composer);

    // 3. Inserting contents...
    // Define the font to use!
    Font font = Font.get(
      document,
      getInputPath() + java.io.File.separator + "fonts" + java.io.File.separator + "GenR102.TTF"
      );
    // Define the paragraph break size!
    Dimension breakSize = new Dimension(0,10);
    // Define the text to show!
    String[] titles = new String[]
      {
        "ΑΡΘΡΟ 1",
        "ASARIYA SINTE (1)",
        "Article 1",
        "Article premier",
        "Статья 1",
        "Artículo 1",
        "Artikel 1",
        "Madde 1",
        "Artikel 1",
        "Articolo 1",
        "Artykuł 1",
        "Bend 1",
        "Abala kìíní."
      };
    String[] bodies = new String[]
      {
        "'Ολοι οι άνθρωποι γεννιούνται ελεύθεροι και ίσοι στην αξιοπρέπεια και τα δικαιώματα. Είναι προικισμένοι με λογική και συνείδηση, και οφείλουν να συμπεριφέρονται μεταξύ τους με πνεύμα αδελφοσύνης.",
        "Aduniya kuna n gu ibuna damayo hɛi nɔ dei-dei nn daama nna n burucinitɛrɛ fɔ, n lasabu nna laakari ya nam nn mɔ huro cɛrɛ kuna nyanze tɛrɛ bɔŋɔɔ.",
        "All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.",
        "Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.",
        "Все люди рождаются свободными и равными в своем достоинстве и правах. Они наделены разумом и совестью и должны поступать в отношении друг друга в духе братства.",
        "Todos los seres humanos nacen libres e iguales en dignidad y derechos y, dotados como están de razón y conciencia, deben comportarse fraternalmente los unos con los otros.",
        "Alle Menschen sind frei und gleich an Würde und Rechten geboren. Sie sind mit Vernunft und Gewissen begabt und sollen einander im Geist der Brüderlichkeit begegnen.",
        "Bütün insanlar hür, haysiyet ve haklar bakımından eşit doğarlar. Akıl ve vicdana sahiptirler ve birbirlerine karşı kardeşlik zihniyeti ile hareket etmelidirler.",
        "Alla människor är födda fria och lika i värde och rättigheter. De har utrustats med förnuft och samvete och bör handla gentemot varandra i en anda av gemenskap.",
        "Tutti gli esseri umani nascono liberi ed eguali in dignità e diritti. Essi sono dotati di ragione e di coscienza e devono agire gli uni verso gli altri in spirito di fratellanza.",
        "Wszyscy ludzie rodzą się wolni i równi pod względem swej godności i swych praw. Są oni obdarzeni rozumem i sumieniem i powinni postępować wobec innych w duchu braterstwa.",
        "Hemû mirov azad û di weqar û mafan de wekhev tên dinyayê. Ew xwedî hiş û şuûr in û divê li hember hev bi zihniyeteke bratiyê bilivin.",
        "Gbogbo ènìyàn ni a bí ní òmìnira; iyì àti è̟tó̟ kò̟ò̟kan sì dó̟gba. Wó̟n ní è̟bùn ti làákàyè àti ti è̟rí-o̟kàn, ó sì ye̟ kí wo̟n ó máa hùwà sí ara wo̟n gé̟gé̟ bí o̟mo̟ ìyá."
      };
    String[] sources = new String[]
      {
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=grk",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=den",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=eng",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=frn",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=rus",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=spn",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=ger",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=trk",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=swd",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=itn",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=pql",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=kdb1",
        "http://www.ohchr.org/EN/UDHR/Pages/Language.aspx?LangID=yor"
      };
    // Begin the content block!
    blockComposer.begin(
      new Rectangle2D.Double(
        Margin,
        Margin,
        page.getSize().getWidth() - Margin * 2,
        page.getSize().getHeight() - Margin * 2
        ),
      AlignmentXEnum.Justify,
      AlignmentYEnum.Top
      );
    for(
      int index = 0,
        length = titles.length;
      index < length;
      index++
      )
    {
      composer.setFont(font,12);
      blockComposer.showText(titles[index]);
      blockComposer.showBreak();

      composer.setFont(font,11);
      blockComposer.showText(bodies[index]);
      blockComposer.showBreak(AlignmentXEnum.Right);

      composer.setFont(font,8);
      blockComposer.showText("[Source: " + sources[index] + "]");
      blockComposer.showBreak(breakSize,AlignmentXEnum.Justify);
    }
    // End the content block!
    blockComposer.end();

    // 4. Flush the contents into the page!
    composer.flush();
  }
View Full Code Here

        ); // 4.1. Field instantiation.
      fields.add(field); // 4.2. Field insertion into the fields collection.
      fieldStyle.apply(field); // 4.3. Appearance style applied.

      {
        BlockComposer blockComposer = new BlockComposer(composer);
        blockComposer.begin(new Rectangle2D.Double(296,50,page.getSize().getWidth()-336,36),AlignmentXEnum.Left,AlignmentYEnum.Middle);
        composer.setFont(composer.getState().getFont(),7);
        blockComposer.showText("If you click this push button, a javascript action should prompt you an alert box responding to the activation event triggered by your PDF viewer.");
        blockComposer.end();
      }
    }

    // 4.b. Check box.
    {
      composer.showText(
        "CheckBox:",
        new Point2D.Double(140, 118),
        AlignmentXEnum.Right,
        AlignmentYEnum.Middle,
        0
        );
      CheckBox field = new CheckBox(
        "myCheck",
        new Widget(
          page,
          new Rectangle(150, 100, 36, 36)
          ),
        true // Current value.
        ); // 4.1. Field instantiation.
      fieldStyle.apply(field);
      fields.add(field);
      field = new CheckBox(
        "myCheck2",
        new Widget(
          page,
          new Rectangle(200, 100, 36, 36)
          ),
        true // Current value.
        ); // 4.1. Field instantiation.
      fieldStyle.apply(field);
      fields.add(field);
      field = new CheckBox(
        "myCheck3",
        new Widget(
          page,
          new Rectangle(250, 100, 36, 36)
          ),
        false // Current value.
        ); // 4.1. Field instantiation.
      fields.add(field); // 4.2. Field insertion into the fields collection.
      fieldStyle.apply(field); // 4.3. Appearance style applied.
    }

    // 4.c. Radio button.
    {
      composer.showText(
        "RadioButton:",
        new Point2D.Double(140, 168),
        AlignmentXEnum.Right,
        AlignmentYEnum.Middle,
        0
        );
      RadioButton field = new RadioButton(
        "myRadio",
        /*
          NOTE: A radio button field typically combines multiple alternative widgets.
        */
        new DualWidget[]
        {
          new DualWidget(
            page,
            new Rectangle(150, 150, 36, 36),
            "first"
            ),
          new DualWidget(
            page,
            new Rectangle(200, 150, 36, 36),
            "second"
            ),
          new DualWidget(
            page,
            new Rectangle(250, 150, 36, 36),
            "third"
            )
        },
        "second" // Selected item (it MUST correspond to one of the available widgets' names).
        ); // 4.1. Field instantiation.
      fields.add(field); // 4.2. Field insertion into the fields collection.
      fieldStyle.apply(field); // 4.3. Appearance style applied.
    }

    // 4.d. Text field.
    {
      composer.showText(
        "TextField:",
        new Point2D.Double(140, 218),
        AlignmentXEnum.Right,
        AlignmentYEnum.Middle,
        0
        );
      TextField field = new TextField(
        "myText",
        new Widget(
          page,
          new Rectangle(150, 200, 200, 36)
          ),
        "Carmen Consoli" // Current value.
        ); // 4.1. Field instantiation.
      field.setSpellChecked(false); // Avoids text spell check.
      FieldActions fieldActions = new FieldActions(document);
      field.setActions(fieldActions);
      fieldActions.setOnValidate(
        new JavaScript(
          document,
          "app.alert(\"Text '\" + this.getField(\"myText\").value + \"' has changed!\",3,0,\"Validation event\");"
          )
        );
      fields.add(field); // 4.2. Field insertion into the fields collection.
      fieldStyle.apply(field); // 4.3. Appearance style applied.

      {
        BlockComposer blockComposer = new BlockComposer(composer);
        blockComposer.begin(new Rectangle2D.Double(360,200,page.getSize().getWidth()-400,36),AlignmentXEnum.Left,AlignmentYEnum.Middle);
        composer.setFont(composer.getState().getFont(),7);
        blockComposer.showText("If you leave this text field after changing its content, a javascript action should prompt you an alert box responding to the validation event triggered by your PDF viewer.");
        blockComposer.end();
      }
    }

    // 4.e. Choice fields.
    {
View Full Code Here

        {ctmInversionApproximations[index] = Math.abs(finalCtmValues[index]) - initialCtmValues[index];}
      }
      maxCtmInversionApproximation = max(ctmInversionApproximations);
    }

    final BlockComposer blockComposer = new BlockComposer(composer);
    blockComposer.setLineSpace(new Length(.25f, UnitModeEnum.Relative));

    composer.beginLocalState();
    composer.setFillColor(
      new DeviceRGBColor(115f/255,164f/255,232f/255)
      );
    final Rectangle2D frame = new Rectangle2D.Double(
      18,
      18,
      pageSize.getWidth() * .5,
      pageSize.getHeight() * .5
      );
    blockComposer.begin(frame,AlignmentXEnum.Left,AlignmentYEnum.Top);
    composer.setFont(ResourceName_DefaultFont,24);
    blockComposer.showText("Page coordinates sample");
    Dimension2D breakSize = new Dimension(8,4);
    blockComposer.showBreak(breakSize);
    composer.setFont(ResourceName_DefaultFont,8);
    blockComposer.showText(
      "This sample shows the effects of the manipulation of the CTM (Current Transformation Matrix), "
        + "that is the mathematical device which affects the page coordinate system used to place "
        + "graphic contents onto the canvas."
      );
    blockComposer.showBreak(breakSize);
    blockComposer.showText(
      "The following steps represent the operations applied to this page's CTM in order to alter it. "
        + "Each step writes the word \"Step\" at the lower-left corner of the current page frame:"
      );
    blockComposer.showBreak(breakSize);
    for(int i = 0; i < steps.length; i++)
    {
      composer.setFillColor(colors[i]);
      blockComposer.showText("Step " + i + ")");
      composer.setFillColor(
        new DeviceRGBColor(115f/255,164f/255,232f/255)
        );
      blockComposer.showText(" " + steps[i]);
      blockComposer.showBreak(breakSize);
    }
    blockComposer.showText("Note that the approximation (" + maxCtmInversionApproximation + ") of the CTM components at step 4 is due to floating point precision limits; their exact values should be 1.0, 0.0, 0.0, 1.0, 0.0, 0.0.");
    blockComposer.end();
    composer.end();
  }
View Full Code Here

        0
        );
    }
    // 3.2. Showing the comments on the page...
    {
      BlockComposer blockComposer = new BlockComposer(composer);
      blockComposer.begin(
        new Rectangle2D.Double(
          18,
          18,
          pageSize.getWidth() * .5,
          pageSize.getHeight() * .5
          ),
        AlignmentXEnum.Justify,
        AlignmentYEnum.Top
        );
      StandardType1Font bodyFont = new StandardType1Font(
        document,
        StandardType1Font.FamilyEnum.Courier,
        true,
        false
        );
      composer.setFont(bodyFont,24);
      blockComposer.showText("Page-to-form sample");
      Dimension2D breakSize = new Dimension(0,8);
      blockComposer.showBreak(breakSize);
      composer.setFont(bodyFont,8);
      blockComposer.showText("This sample shows how to convert a page to a reusable form that can be placed multiple times on other pages scaling, rotating, anchoring and aligning it.");
      blockComposer.showBreak(breakSize);
      blockComposer.showText("On this page you can see some of the above-mentioned transformations:");
      breakSize.setSize(8,8);
      blockComposer.showBreak(breakSize);
      blockComposer.showText("1. anchored to the center of the page, rotated by 45 degrees counterclockwise, 300 point wide (preserved proportions);"); blockComposer.showBreak(breakSize);
      blockComposer.showText("2. anchored to the bottom-left corner of the page, 300 point high (preserved proportions);"); blockComposer.showBreak(breakSize);
      blockComposer.showText("3. anchored to the bottom-right corner of the page, 80 point wide and 200 point high (altered proportions).");
      blockComposer.end();
    }

    // 4. Flush the contents into the content stream!
    composer.flush();
  }
View Full Code Here

    }
    catch(Exception e)
    {}

    PrimitiveComposer composer = new PrimitiveComposer(page);
    BlockComposer blockComposer = new BlockComposer(composer);

    /*
      2.1. Goto-URI link.
    */
    {
      blockComposer.begin(new Rectangle2D.Double(30,100,200,50),AlignmentXEnum.Left,AlignmentYEnum.Middle);
      composer.setFont(font,12);
      blockComposer.showText("Goto-URI link");
      composer.setFont(font,8);
      blockComposer.showText("\nIt allows you to navigate to a network resource.");
      composer.setFont(font,5);
      blockComposer.showText("\n\nClick on the box to go to the project's SourceForge.net repository.");
      blockComposer.end();

      try
      {
        /*
          NOTE: This statement instructs the PDF viewer to navigate to the given URI when the link is clicked.
        */
        Link link = new Link(
          page,
          new Rectangle(240,100,100,50),
          new GoToURI(
            document,
            new URI("http://www.sourceforge.net/projects/clown")
            )
          );
        link.setBorder(
          new Border(
            document,
            3,
            Border.StyleEnum.Beveled,
            null
            )
          );
      }
      catch(Exception exception)
      {throw new RuntimeException(exception);}
    }

    /*
      2.2. Embedded-goto link.
    */
    {
      // Get the path of the PDF file to attach!
      String filePath = promptPdfFileChoice("Please select a PDF file to attach");

      /*
        NOTE: These statements instruct PDF Clown to attach a PDF file to the current document.
        This is necessary in order to test the embedded-goto functionality,
        as you can see in the following link creation (see below).
      */
      int fileAttachmentPageIndex = page.getIndex();
      String fileAttachmentName = "attachedSamplePDF";
      String fileName = new java.io.File(filePath).getName();
      FileAttachment attachment = new FileAttachment(
        page,
        new Rectangle(0, -20, 10, 10),
        new FileSpec(
          EmbeddedFile.get(
            document,
            filePath
            ),
          fileName
          )
        );
      attachment.setName(fileAttachmentName);
      attachment.setText("File attachment annotation");
      attachment.setIconType(FileAttachment.IconTypeEnum.PaperClip);

      blockComposer.begin(new Rectangle2D.Double(30,170,200,50),AlignmentXEnum.Left,AlignmentYEnum.Middle);
      composer.setFont(font,12);
      blockComposer.showText("Embedded-goto link");
      composer.setFont(font,8);
      blockComposer.showText("\nIt allows you to navigate to a destination within an embedded PDF file.");
      composer.setFont(font,5);
      blockComposer.showText("\n\nClick on the button to go to the 2nd page of the attached PDF file (" + fileName + ").");
      blockComposer.end();

      /*
        NOTE: This statement instructs the PDF viewer to navigate to the page 2 of a PDF file
        attached inside the current document as described by the FileAttachment annotation on page 1 of the current document.
      */
      Link link = new Link(
        page,
        new Rectangle(240,170,100,50),
        new GoToEmbedded(
          document,
          new GoToEmbedded.TargetObject(
            document,
            fileAttachmentPageIndex, // Page of the current document containing the file attachment annotation of the target document.
            fileAttachmentName, // Name of the file attachment annotation corresponding to the target document.
            null // No sub-target.
            ), // Target represents the document to go to.
          new RemoteDestination(
            document,
            1, // Show the page 2 of the target document.
            Destination.ModeEnum.Fit, // Show the target document page entirely on the screen.
            null // No view parameters.
            ) // The destination must be within the target document.
          )
        );
      link.setBorder(
        new Border(
          document,
          1,
          Border.StyleEnum.Dashed,
          new LineDash(new float[]{8,5,2,5})
          )
        );
    }

    /*
      2.3. Textual link.
    */
    {
      blockComposer.begin(new Rectangle2D.Double(30,240,200,50),AlignmentXEnum.Left,AlignmentYEnum.Middle);
      composer.setFont(font,12);
      blockComposer.showText("Textual link");
      composer.setFont(font,8);
      blockComposer.showText("\nIt allows you to expose any kind of link (including the above-mentioned types) as text.");
      composer.setFont(font,5);
      blockComposer.showText("\n\nClick on the text links to go either to the project's SourceForge.net repository or to the project's home page.");
      blockComposer.end();

      try
      {
        composer.beginLocalState();
        composer.setFont(font,10);
View Full Code Here

    }
    catch(Exception e)
    {throw new RuntimeException(e);}

    {
      BlockComposer blockComposer = new BlockComposer(composer);
      blockComposer.begin(new Rectangle2D.Double(30,0,pageSize.getWidth()-60,50),AlignmentXEnum.Center,AlignmentYEnum.Middle);
      blockComposer.showText("Curves");
      blockComposer.end();
    }

    // 3.1. Arcs.
    {
      float y = 100;
View Full Code Here

    }
    catch(Exception e)
    {throw new RuntimeException(e);}

    {
      BlockComposer blockComposer = new BlockComposer(composer);
      blockComposer.begin(new Rectangle2D.Double(30,0,pageSize.getWidth()-60,50),AlignmentXEnum.Center,AlignmentYEnum.Middle);
      blockComposer.showText("Miscellaneous");
      blockComposer.end();
    }

    composer.beginLocalState();
    composer.setLineJoin(LineJoinEnum.Round);
    composer.setLineCap(LineCapEnum.Round);
View Full Code Here

    EnumSet<AlignmentXEnum> xAlignments = EnumSet.allOf(AlignmentXEnum.class);
    EnumSet<AlignmentYEnum> yAlignments = EnumSet.allOf(AlignmentYEnum.class);
    int step = (int)(pageSize.getHeight()) / ((xAlignments.size()-1) * yAlignments.size()+1);

    BlockComposer blockComposer = new BlockComposer(composer);
    Rectangle2D frame = new Rectangle2D.Double(
      30,
      0,
      pageSize.getWidth()-60,
      step/2
      );
    blockComposer.begin(frame,AlignmentXEnum.Center,AlignmentYEnum.Middle);
    blockComposer.showText(
      "Simple text alignment"
      );
    blockComposer.end();

    frame = new Rectangle2D.Double(
      30,
      pageSize.getHeight()-step/2,
      pageSize.getWidth()-60,
      step/2 -10
      );
    blockComposer.begin(frame,AlignmentXEnum.Left,AlignmentYEnum.Bottom);
    composer.setFont(composer.getState().getFont(),10);
    blockComposer.showText(
      "NOTE: showText(...) methods return the actual bounding box of the text shown.\n"
        + "NOTE: The rotation parameter can be freely defined as a floating point value."
      );
    blockComposer.end();

    composer.setFont(composer.getState().getFont(),12);
    int x = 30;
    int y = step;
    int alignmentIndex = 0;
View Full Code Here

    EnumSet<AlignmentXEnum> xAlignments = EnumSet.allOf(AlignmentXEnum.class);
    EnumSet<AlignmentYEnum> yAlignments = EnumSet.allOf(AlignmentYEnum.class);
    int step = (int)(pageSize.getHeight()) / (xAlignments.size() * yAlignments.size()+1);

    BlockComposer blockComposer = new BlockComposer(composer);
    {
      blockComposer.begin(
        new Rectangle2D.Double(
          30,
          0,
          pageSize.getWidth()-60,
          step*.8
          ),
        AlignmentXEnum.Center,
        AlignmentYEnum.Middle
        );
      blockComposer.showText(
        "Text block alignment"
        );
      blockComposer.end();
    }

    // Drawing the text blocks...
    int x = 30;
    int y = (int)(step*1.2);
    for(AlignmentXEnum alignmentX
      : EnumSet.allOf(AlignmentXEnum.class))
    {
      for(AlignmentYEnum alignmentY
        : EnumSet.allOf(AlignmentYEnum.class))
      {
        composer.setFont(
          composer.getState().getFont(),
          12
          );
        composer.showText(
          alignmentX + " " + alignmentY + ":",
          new Point2D.Double(x,y),
          AlignmentXEnum.Left,
          AlignmentYEnum.Middle,
          0
          );

        composer.setFont(
          composer.getState().getFont(),
          8
          );
        for(
          int index = 0;
          index < 2;
          index++
          )
        {
          int frameX;
          switch(index)
          {
            case 0:
              frameX = 150;
              blockComposer.setHyphenation(false);
              break;
            case 1:
              frameX = 360;
              blockComposer.setHyphenation(true);
              break;
            default:
              throw new RuntimeException();
          }

          Rectangle2D frame = new Rectangle2D.Double(
            frameX,
            y-step*.4,
            200,
            step*.8
            );
          blockComposer.begin(frame,alignmentX,alignmentY);
          blockComposer.showText(
            "Demonstrating how to constrain text inside a page area using PDF Clown. See the other available code samples (such as TypesettingSample) to discover more functionality details."
            );
          blockComposer.end();

          composer.beginLocalState();
          composer.setLineWidth(.2f);
          composer.setLineDash(5,5,5);
          composer.drawRectangle(frame);
View Full Code Here

TOP

Related Classes of org.pdfclown.documents.contents.composition.BlockComposer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.