Package org.pdfclown.documents.contents.composition

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


    // Add page!
    Page page = new Page(document);
    document.getPages().add(page);
    Dimension2D pageSize = page.getSize();

    PrimitiveComposer composer = new PrimitiveComposer(page);
    // Add the background template!
    composer.showXObject(
      SampleHelper.createTemplate(document)
      );

    // Wrap the content composer inside a block filter in order to achieve higher-level typographic control!
    BlockComposer blockComposer = new BlockComposer(composer);
    blockComposer.setHyphenation(true);

    Rectangle2D.Double frame = new Rectangle2D.Double(
      30,
      150,
      pageSize.getWidth() - 110,
      pageSize.getHeight() - 250
      );

    blockComposer.begin(frame,AlignmentXEnum.Left,AlignmentYEnum.Top);
    StandardType1Font titleFont = new StandardType1Font(
      document,
      StandardType1Font.FamilyEnum.Times,
      true,
      false
      );
    composer.setFont(titleFont,48);
    blockComposer.showText("Welcome"); blockComposer.showBreak();
    StandardType1Font bodyFont = new StandardType1Font(
      document,
      StandardType1Font.FamilyEnum.Times,
      false,
      false
      );
    composer.setFont(bodyFont,16);
    blockComposer.showText("This is an on-the-fly servlet-driven PDF sample document generated by PDF Clown for Java.");
    blockComposer.end();

    // Move past the closed block!
    frame.y = blockComposer.getBoundBox().getMaxY() + 30;
    frame.height -= (blockComposer.getBoundBox().getHeight() + 30);

    // Showing the posted image...
    // Instantiate a jpeg image object!
    Image image = null;
    try
    {
      image = Image.get(
        new Buffer(imageFileFormField.get())
        ); // Abstract image (entity).
    }
    catch(Exception e)
    {/* NOOP. */}
    if(image == null)
    {
      blockComposer.begin(frame,AlignmentXEnum.Left,AlignmentYEnum.Top);
      composer.setFont(bodyFont,12);
      composer.setFillColor(new DeviceRGBColor(1,0,0));
      blockComposer.showText("The file you uploaded wasn't a valid JPEG image!");
      blockComposer.end();

      // Move past the closed block!
      frame.y = blockComposer.getBoundBox().getMaxY() + 20;
      frame.height -= (blockComposer.getBoundBox().getHeight() + 20);
    }
    else
    {
      blockComposer.begin(frame,AlignmentXEnum.Left,AlignmentYEnum.Top);
      composer.setFont(bodyFont,12);
      blockComposer.showText("Here it is the image you uploaded: ");
      blockComposer.end();

      // Move past the closed block!
      frame.y = blockComposer.getBoundBox().getMaxY() + 20;
      frame.height -= (blockComposer.getBoundBox().getHeight() + 20);

      double width = image.getWidth(), height = image.getHeight();
      if(width > frame.getWidth())
      {
        height *= frame.getWidth() / width;
        width = frame.getWidth();
      }
      if(height > frame.getHeight() / 2)
      {
        width *= frame.getHeight() / 2 / height;
        height = frame.getHeight() / 2;
      }
      // Show the image!
      composer.showXObject(
        image.toXObject(document),
        new Point2D.Double(
          (pageSize.getWidth() - 90 - width) / 2 + 20,
          blockComposer.getBoundBox().getMaxY() + 20
          ),
        new Dimension(width,height)
        );
      // Move past the image closed block!
      frame.x = (pageSize.getWidth() - 90 - width) / 2 + 20;
      frame.y += (height + 7);
      frame.height -= (height + 7);
      frame.width = width;
    }

    if (comment != null)
    {
      blockComposer.begin(frame,AlignmentXEnum.Justify,AlignmentYEnum.Top);
      composer.setFont(
        new StandardType1Font(
          document,
          StandardType1Font.FamilyEnum.Courier,
          false,
          false
          ),
        7
        );
      blockComposer.showText(comment);
      blockComposer.end();
    }

    composer.flush();
  }
View Full Code Here


    // Size.
    template.setSize(document.getPageSize());
    Dimension2D templateSize = template.getSize();

    PrimitiveComposer composer = new PrimitiveComposer(template);

    Image image = null;
    try
    {
      // Instantiate a jpeg image object!
      image = Image.get(new java.io.File(((java.net.URL)SampleHelper.class.getResource("mountains.jpg")).toURI()).getPath()); // Abstract image (entity).
    }
    catch(Exception e)
    {/* NOOP. */}
    // Show the image inside the common content stream!
    composer.showXObject(
      image.toXObject(document),
      new Point2D.Double(0,0),
      new Dimension(
        (int)templateSize.getWidth() - 50,
        125
        )
      );

    // Showing the 'PDFClown' label inside the common content stream...
    composer.beginLocalState();
    composer.setFillColor(
      new DeviceRGBColor(115f/255,164f/255,232f/255)
      );
    // Set the font to use!
    composer.setFont(
      new StandardType1Font(
        document,
        StandardType1Font.FamilyEnum.Times,
        true,
        false
        ),
      120
      );
    // Show the text!
    composer.showText(
      "PDFClown",
      new Point2D.Double(
        0,
        templateSize.getHeight() - composer.getState().getFont().getAscent(composer.getState().getFontSize())
        )
      );

    // Drawing the side rectangle...
    composer.drawRectangle(
      new Rectangle2D.Double(
        templateSize.getWidth() - 50,
        0,
        50,
        templateSize.getHeight()
        )
      );
    composer.fill();
    composer.end();

    // Begin the graphics state!
    composer.beginLocalState();
    // Set the font to use!
    composer.setFont(
      new StandardType1Font(
        document,
        StandardType1Font.FamilyEnum.Helvetica,
        false,
        false
        ),
      8
      );
    // Set the fill color to use!
    composer.setFillColor(
      DeviceRGBColor.White
      );
    composer.beginLocalState();
    composer.translate((float)templateSize.getWidth() - 20, 20);
    composer.rotate(90);
    composer.showText("Generated by PDF Clown on " + (new Date()));
    composer.translate(0,-8);
    composer.showText("For more info, visit http://www.pdfclown.org");
    composer.end();
    // End the graphics state!
    composer.end();

    composer.flush();

    return template;
  }
View Full Code Here

    // 1. Add the page to the document!
    Page page = new Page(document); // Instantiates the page inside the document context.
    document.getPages().add(page); // Puts the page in the pages collection.

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

    BlockComposer blockComposer = new BlockComposer(composer);
    blockComposer.begin(new Rectangle2D.Double(300,400,200,100),AlignmentXEnum.Left,AlignmentYEnum.Middle);
    try
    {
      composer.setFont(
        new StandardType1Font(
          document,
          StandardType1Font.FamilyEnum.Times,
          false,
          true
          ),
        12
        );
    }
    catch(Exception e)
    {}
    composer.setFillColor(new DeviceRGBColor(115f/255,164f/255,232f/255));
    blockComposer.showText("showText() methods return the actual bounding box of the shown text, allowing to precisely determine its location on the page.");
    blockComposer.end();

    composer.setStrokeColor(new DeviceRGBColor(115f/255,164f/255,232f/255));

    // 3. Inserting contents...
    // Set the font to use!
    try
    {
      composer.setFont(
        new StandardType1Font(
          document,
          StandardType1Font.FamilyEnum.Courier,
          true,
          false
          ),
        72
        );
    }
    catch(Exception e)
    {}
    composer.drawPolygon(
      composer.showText(
        "Text frame",
        new Point2D.Double(150,360),
        AlignmentXEnum.Left,
        AlignmentYEnum.Middle,
        45
        )
      );
    composer.stroke();

    try
    {
      composer.setFont(
        Font.get(
          document,
          getInputPath() + java.io.File.separator + "fonts" + java.io.File.separator + "Ruritania-Outline.ttf"
          ),
        102
        );
    }
    catch(Exception e)
    {}
    composer.drawPolygon(
      composer.showText(
        "Text frame",
        new Point2D.Double(300,600),
        AlignmentXEnum.Center,
        AlignmentYEnum.Middle,
        -25
        )
      );
    composer.stroke();

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

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

    PrimitiveComposer composer = new PrimitiveComposer(page);

    int x = Margin, y = Margin;
    StandardType1Font titleFont = new StandardType1Font(
      document,
      StandardType1Font.FamilyEnum.Times,
      true,
      true
      );
    StandardType1Font font = null;
    // Iterating through the standard Type 1 fonts...
    for(StandardType1Font.FamilyEnum fontFamily
      : EnumSet.allOf(StandardType1Font.FamilyEnum.class))
    {
      // Iterating through the font styles...
      for(int styleIndex = 0; styleIndex < 4; styleIndex++)
      {
        /*
          NOTE: Symbol and Zapf Dingbats are available just as regular fonts (no italic or bold variant).
        */
        if(styleIndex > 0
          && (fontFamily == StandardType1Font.FamilyEnum.Symbol
            || fontFamily == StandardType1Font.FamilyEnum.ZapfDingbats))
            break;

        boolean bold, italic;
        switch(styleIndex)
        {
          case 0: // Regular.
            bold = false;
            italic = false;
            break;
          case 1: // Bold.
            bold = true;
            italic = false;
            break;
          case 2: // Italic.
            bold = false;
            italic = true;
            break;
          case 3: // Bold italic.
            bold = true;
            italic = true;
            break;
          default:
            throw new RuntimeException("styleIndex " + styleIndex + " not supported.");
        }
        // Define the font used to show its character set!
        font = new StandardType1Font(
          document,
          fontFamily,
          bold,
          italic
          );

        if(y > pageSize.getHeight() - Margin)
        {
          composer.flush();

          page = new Page(document);
          document.getPages().add(page);
          pageSize = page.getSize();
          composer = new PrimitiveComposer(page);
          x = Margin; y = Margin;
        }

        if(styleIndex == 0)
        {
          composer.drawLine(
            new Point2D.Double(x,y),
            new Point2D.Double(pageSize.getWidth() - Margin,y)
            );
          composer.stroke();
          y += 5;
        }

        composer.setFont(
          titleFont,
          FontBaseSize * (styleIndex == 0 ? 1.5f : 1)
          );
        composer.showText(
          fontFamily.name() + (bold ? " bold" : "") + (italic ? " italic" : ""),
          new Point2D.Double(x,y)
          );

        y += 40;
        // Set the font used to show its character set!
        composer.setFont(font,FontBaseSize);
        // Iterating through the font characters...
        for(int charCode = 32; charCode < 256; charCode++)
        {
          if(y > pageSize.getHeight() - Margin)
          {
            composer.flush();

            page = new Page(document);
            document.getPages().add(page);
            pageSize = page.getSize();
            composer = new PrimitiveComposer(page);
            x = Margin; y = Margin;

            composer.setFont(titleFont,FontBaseSize);
            composer.showText(
              fontFamily.name() + " (continued)",
              new Point2D.Double(pageSize.getWidth() - Margin, y),
              AlignmentXEnum.Right,
              AlignmentYEnum.Top,
              0
              );
            composer.setFont(font,FontBaseSize);
            y += FontBaseSize * 2;
          }

          try
          {
            // Show the current character (using the current standard Type 1 font)!
            composer.showText(
              new String(new char[]{(char)charCode}),
              new Point2D.Double(x,y)
              );
            x += FontBaseSize;
            if(x > pageSize.getWidth() - Margin)
            {x = Margin; y += 30;}
          }
          catch(Exception e)
          { /* Ignore */ }
        }

        x = Margin; y += Margin;
      }
    }
    composer.flush();
  }
View Full Code Here

    )
  {
    FormXObject xObject = new FormXObject(context);
    {
      xObject.setSize(getSize());
      PrimitiveComposer composer = new PrimitiveComposer(xObject);
      toInlineObject(composer);
      composer.flush();
    }
    return xObject;
  }
View Full Code Here

      - adding content objects through a ContentScanner instance;
      - invoking basic drawing functions through a PrimitiveComposer instance;
      - invoking advanced static-positioning functions through a BlockComposer instance;
      - invoking advanced dynamic-positioning functions through a FlowComposer instance (currently not implemented yet).
    */
    PrimitiveComposer composer = new PrimitiveComposer(page);
    // Wrap the content composer within a block filter!
    /*
      NOTE: The block filter is a basic typesetter. It exposes higher-level graphical
      functionalities (horizontal/vertical alignment, indentation, paragraph composition etc.)
      leveraging the content composer primitives.
      It's important to note that this is just an intermediate abstraction layer of the typesetting
      stack: further abstract levels could sit upon it, allowing the convenient treatment of
      typographic entities like titles, paragraphs, columns, tables, headers, footers etc.
      When such further abstract levels are available, the final user (developer of consuming
      applications) won't care any more of the details you can see here in the following code lines
      (such as bothering to select the first-letter font...).
    */
    BlockComposer blockComposer = new BlockComposer(composer);

    composer.beginLocalState();

    // Define the block frame that will encompass our contents on the page canvas!
    Rectangle2D frame = new Rectangle2D.Double(
      Margin_X,
      Margin_Y,
      pageSize.getWidth() - Margin_X * 2,
      pageSize.getHeight() - Margin_Y * 2
      );
    // Begin the title block!
    blockComposer.begin(frame,AlignmentXEnum.Left,AlignmentYEnum.Top);
    Font decorativeFont = Font.get(
      document,
      getInputPath() + java.io.File.separator + "fonts" + java.io.File.separator + "Ruritania-Outline.ttf"
      );
    composer.setFont(decorativeFont,56);
    blockComposer.showText("Chapter 1");
    blockComposer.showBreak();
    composer.setFont(decorativeFont,32);
    blockComposer.showText("Down the Rabbit-Hole");
    // End the title block!
    blockComposer.end();

    // Update the block frame to begin after the title!
    frame = new Rectangle2D.Double(
      blockComposer.getBoundBox().getX(),
      blockComposer.getBoundBox().getY() + blockComposer.getBoundBox().getHeight(),
      blockComposer.getBoundBox().getWidth(),
      pageSize.getHeight() - Margin_Y - (blockComposer.getBoundBox().getY() + blockComposer.getBoundBox().getHeight())
      );
    // Begin the body block!
    blockComposer.begin(frame,AlignmentXEnum.Justify,AlignmentYEnum.Bottom);
    Font bodyFont = Font.get(
      document,
      getInputPath() + java.io.File.separator + "fonts" + java.io.File.separator + "TravelingTypewriter.otf"
      );
    composer.setFont(bodyFont,14);
    composer.beginLocalState();
    composer.setFont(decorativeFont,28);
    blockComposer.showText("A");
    composer.end();
    blockComposer.showText("lice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversation?'");
    // Define new-paragraph first-line offset!
    Dimension breakSize = new Dimension(24,8); // Indentation (24pt) and top margin (8pt).
    // Begin a new paragraph!
    blockComposer.showBreak(breakSize);
    blockComposer.showText("So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.");
    // Begin a new paragraph!
    blockComposer.showBreak(breakSize);
    blockComposer.showText("There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT- POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.");
    // End the body block!
    blockComposer.end();

    composer.end();

    composer.beginLocalState();
    composer.rotate(
      90,
      new Point2D.Double(
        pageSize.getWidth() - 50,
        pageSize.getHeight() - 25
        )
      );
    blockComposer = new BlockComposer(composer);
    blockComposer.begin(
      new Rectangle2D.Double(0,0,300,50),
      AlignmentXEnum.Left,
      AlignmentYEnum.Middle
      );
    composer.setFont(bodyFont,8);
    blockComposer.showText("Generated by PDF Clown on " + new java.util.Date());
    blockComposer.showBreak();
    blockComposer.showText("For more info, visit http://www.pdfclown.org");
    blockComposer.end();
    composer.end();

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

      Dimension2D size = new Dimension(widgetBox.getWidth(),widgetBox.getHeight());
      Rectangle2D frame = new Rectangle2D.Double(0,0,size.getWidth(),size.getHeight());
      {
        onState.setSize(size);

        PrimitiveComposer composer = new PrimitiveComposer(onState);

        composer.beginLocalState();
        composer.setFillColor(getBackColor());
        composer.setStrokeColor(getForeColor());
        composer.drawRectangle(frame);
        composer.fillStroke();
        composer.end();

        BlockComposer blockComposer = new BlockComposer(composer);
        blockComposer.begin(frame,AlignmentXEnum.Center,AlignmentYEnum.Middle);
        composer.setFillColor(getForeColor());
        composer.setFont(
          new StandardType1Font(
            document,
            StandardType1Font.FamilyEnum.ZapfDingbats,
            true,
            false
            ),
          (float)(size.getHeight() * 0.8)
          );
        blockComposer.showText(new String(new char[]{getCheckSymbol()}));
        blockComposer.end();

        composer.flush();
      }

      FormXObject offState = new FormXObject(document);
      normalAppearance.put(PdfName.Off,offState);
      {
        offState.setSize(size);

        PrimitiveComposer composer = new PrimitiveComposer(offState);

        composer.beginLocalState();
        composer.setFillColor(getBackColor());
        composer.setStrokeColor(getForeColor());
        composer.drawRectangle(frame);
        composer.fillStroke();
        composer.end();

        composer.flush();
      }
    }
  }
View Full Code Here

      Dimension2D size = new Dimension(widgetBox.getWidth(),widgetBox.getHeight());
      Rectangle2D frame = new Rectangle2D.Double(1,1,size.getWidth()-2,size.getHeight()-2);
      {
        onState.setSize(size);

        PrimitiveComposer composer = new PrimitiveComposer(onState);

        composer.beginLocalState();
        composer.setFillColor(getBackColor());
        composer.setStrokeColor(getForeColor());
        composer.drawEllipse(frame);
        composer.fillStroke();
        composer.end();

        BlockComposer blockComposer = new BlockComposer(composer);
        blockComposer.begin(frame,AlignmentXEnum.Center,AlignmentYEnum.Middle);
        composer.setFillColor(getForeColor());
        composer.setFont(
          new StandardType1Font(
            document,
            StandardType1Font.FamilyEnum.ZapfDingbats,
            true,
            false
            ),
          (float)(size.getHeight() * 0.8)
          );
        blockComposer.showText(new String(new char[]{getRadioSymbol()}));
        blockComposer.end();

        composer.flush();
      }

      FormXObject offState = new FormXObject(document);
      normalAppearance.put(PdfName.Off,offState);
      {
        offState.setSize(size);

        PrimitiveComposer composer = new PrimitiveComposer(offState);

        composer.beginLocalState();
        composer.setFillColor(getBackColor());
        composer.setStrokeColor(getForeColor());
        composer.drawEllipse(frame);
        composer.fillStroke();
        composer.end();

        composer.flush();
      }
    }
  }
View Full Code Here

    FormXObject normalAppearanceState = new FormXObject(document);
    {
      Rectangle2D widgetBox = widget.getBox();
      Dimension2D size = new Dimension(widgetBox.getWidth(),widgetBox.getHeight());
      normalAppearanceState.setSize(size);
      PrimitiveComposer composer = new PrimitiveComposer(normalAppearanceState);

      composer.beginLocalState();
      float lineWidth = 1;
      composer.setLineWidth(lineWidth);
      composer.setFillColor(getBackColor());
      composer.setStrokeColor(getForeColor());
      Rectangle2D frame = new Rectangle2D.Double(lineWidth/2,lineWidth/2,size.getWidth()-lineWidth,size.getHeight()-lineWidth);
      composer.drawRectangle(frame,5);
      composer.fillStroke();
      composer.end();

      String caption = (String)field.getValue();
      if(caption != null)
      {
        BlockComposer blockComposer = new BlockComposer(composer);
        blockComposer.begin(frame,AlignmentXEnum.Center,AlignmentYEnum.Middle);
        composer.setFillColor(getForeColor());
        composer.setFont(
          new StandardType1Font(
            document,
            StandardType1Font.FamilyEnum.Helvetica,
            true,
            false
            ),
          (float)(size.getHeight() * 0.5)
          );
        blockComposer.showText(caption);
        blockComposer.end();
      }

      composer.flush();
    }
    appearance.getNormal().put(null,normalAppearanceState);
  }
View Full Code Here

    // 1. Add the page to the document!
    Page page = new Page(document); // Instantiates the page inside the document context.
    document.getPages().add(page); // Puts the page in the pages collection.

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

    // 3. Inserting contents...
    // Set the font to use!
    composer.setFont(
      new StandardType1Font(
        document,
        StandardType1Font.FamilyEnum.Courier,
        true,
        false
        ),
      32
      );
    // Show the text onto the page!
    /*
      NOTE: PrimitiveComposer's showText() method is the most basic way
      to add text to a page -- see BlockComposer for more advanced uses
      (horizontal and vertical alignment, hyphenation, etc.).
    */
    composer.showText(
      "Hello World!",
      new Point2D.Double(32,48)
      );

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

TOP

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

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.