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

The following examples show how to use org.apache.pdfbox.pdmodel.edit.PDPageContentStream#saveGraphicsState() . 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: 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 2
Source File: TextLine.java    From pdfbox-layout with MIT License 4 votes vote down vote up
public void drawAligned(PDPageContentStream contentStream, Position upperLeft,
    Alignment alignment, float availableLineWidth,
    DrawListener drawListener) throws IOException {
contentStream.saveGraphicsState();
contentStream.beginText();

float x = upperLeft.getX();
float y = upperLeft.getY() - getAscent(); // the baseline
float offset = TextSequenceUtil.getOffset(this, availableLineWidth, alignment);
x += offset;
CompatibilityHelper.setTextTranslation(contentStream, x, y);
float extraWordSpacing = 0;
if (alignment == Alignment.Justify && (getNewLine() instanceof WrappingNewLine) ){
    extraWordSpacing = (availableLineWidth - getWidth()) / (styledTextList.size()-1);
}

FontDescriptor lastFontDesc = null;
float lastBaselineOffset = 0;
Color lastColor = null;
float gap = 0;
for (StyledText styledText : styledTextList) {
    if (!styledText.getFontDescriptor().equals(lastFontDesc)) {
	lastFontDesc = styledText.getFontDescriptor();
	contentStream.setFont(lastFontDesc.getFont(),
		lastFontDesc.getSize());
    }
    if (!styledText.getColor().equals(lastColor)) {
	lastColor = styledText.getColor();
	contentStream.setNonStrokingColor(lastColor);
    }
    if (styledText.getLeftMargin() > 0) {
	gap += styledText.getLeftMargin();
    }

    boolean moveBaseline = styledText.getBaselineOffset() != lastBaselineOffset;
    if (moveBaseline || gap > 0) {
	float baselineDelta = lastBaselineOffset - styledText.getBaselineOffset();
	lastBaselineOffset = styledText.getBaselineOffset();
	CompatibilityHelper.moveTextPosition(contentStream, gap, baselineDelta);
	x += gap;
    }
    if (styledText.getText().length() > 0) {
	CompatibilityHelper.showText(contentStream,
		styledText.getText());
    }

    if (drawListener != null) {
	float currentUpperLeft = y + styledText.getAsent();
	drawListener.drawn(styledText,
		new Position(x, currentUpperLeft),
		styledText.getWidthWithoutMargin(),
		styledText.getHeight());
    }
    x += styledText.getWidthWithoutMargin();

    gap = extraWordSpacing;
    if (styledText.getRightMargin() > 0) {
	gap += styledText.getRightMargin();
    }
}
contentStream.endText();
contentStream.restoreGraphicsState();
   }