Java Code Examples for org.apache.pdfbox.pdmodel.edit.PDPageContentStream#addRect()

The following examples show how to use org.apache.pdfbox.pdmodel.edit.PDPageContentStream#addRect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDFUtil.java    From ctsms with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void renderFrame(PDPageContentStream contentStream, org.phoenixctms.ctsms.enumeration.Color color, float x, float y, float width, float height, Alignment align,
		float lineWidth, LineStyle lineStyle) throws IOException {
	if (contentStream != null && lineWidth > 0.0f) {
		float left = x;
		float bottom = y - height;
		if (align != null) {
			switch (align) {
				case TOP_CENTER:
					left = x - width / 2.0f;
					bottom = y - height;
					break;
				case TOP_RIGHT:
					left = x - width;
					bottom = y - height;
					break;
				case MIDDLE_LEFT:
					left = x;
					bottom = y - height / 2.0f;
					break;
				case MIDDLE_CENTER:
					left = x - width / 2.0f;
					bottom = y - height / 2.0f;
					break;
				case MIDDLE_RIGHT:
					left = x - width;
					bottom = y - height / 2.0f;
					break;
				case BOTTOM_LEFT:
					left = x;
					bottom = y;
					break;
				case BOTTOM_CENTER:
					left = x - width / 2.0f;
					bottom = y;
					break;
				case BOTTOM_RIGHT:
					left = x - width;
					bottom = y;
					break;
				default: // topleft else...
			}
		}
		contentStream.setStrokingColor(CommonUtil.convertColor(color));
		setLineDashPattern(contentStream, lineStyle);
		contentStream.setLineWidth(lineWidth);
		contentStream.addRect(left, bottom, width, height);
		contentStream.closeAndStroke();
	}
}
 
Example 2
Source File: VerticalLayout.java    From pdfbox-layout with MIT License 4 votes vote down vote up
/**
    * Actually draws the (drawble) part at the
    * {@link RenderContext#getCurrentPosition()} and - depending on flag
    * <code>movePosition</code> - moves to the new Y position. Any left or
    * right margin is taken into account to calculate the position and
    * alignment.
    * 
    * @param renderContext
    *            the context providing all rendering state.
    * @param drawable
    *            the drawable to draw.
    * @param layoutHint
    *            the layout hint used to layout.
    * @param movePosition
    *            indicates if the position should be moved (vertically) after
    *            drawing.
    * @throws IOException
    *             by pdfbox
    */
   protected void drawReletivePartAndMovePosition(
    final RenderContext renderContext, Drawable drawable,
    final LayoutHint layoutHint, final boolean movePosition)
    throws IOException {
PDPageContentStream contentStream = renderContext.getContentStream();
PageFormat pageFormat = renderContext.getPageFormat();
float offsetX = 0;
if (layoutHint instanceof VerticalLayoutHint) {
    VerticalLayoutHint verticalLayoutHint = (VerticalLayoutHint) layoutHint;
    Alignment alignment = verticalLayoutHint.getAlignment();
    float horizontalExtraSpace = getTargetWidth(renderContext)
	    - drawable.getWidth();
    switch (alignment) {
    case Right:
	offsetX = horizontalExtraSpace
		- verticalLayoutHint.getMarginRight();
	break;
    case Center:
	offsetX = horizontalExtraSpace / 2f;
	break;
    default:
	offsetX = verticalLayoutHint.getMarginLeft();
	break;
    }
}

contentStream.saveGraphicsState();
contentStream.addRect(0, pageFormat.getMarginBottom(), renderContext.getPageWidth(),
	renderContext.getHeight());
CompatibilityHelper.clip(contentStream);

drawable.draw(renderContext.getPdDocument(), contentStream,
	renderContext.getCurrentPosition().add(offsetX, 0),renderContext);

contentStream.restoreGraphicsState();

if (movePosition) {
    renderContext.movePositionBy(0, -drawable.getHeight());
}
   }
 
Example 3
Source File: Rect.java    From pdfbox-layout with MIT License 4 votes vote down vote up
@Override
   public void add(PDDocument pdDocument, PDPageContentStream contentStream,
           Position upperLeft, float width, float height) throws IOException {
contentStream.addRect(upperLeft.getX(), upperLeft.getY() - height,
	width, height);
   }