当前位置: 首页>>代码示例>>Java>>正文


Java PDPageContentStream.saveGraphicsState方法代码示例

本文整理汇总了Java中org.apache.pdfbox.pdmodel.edit.PDPageContentStream.saveGraphicsState方法的典型用法代码示例。如果您正苦于以下问题:Java PDPageContentStream.saveGraphicsState方法的具体用法?Java PDPageContentStream.saveGraphicsState怎么用?Java PDPageContentStream.saveGraphicsState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.pdfbox.pdmodel.edit.PDPageContentStream的用法示例。


在下文中一共展示了PDPageContentStream.saveGraphicsState方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: watermarkOriginal

import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
/**
 * <p>
 * This is the OP's initial approach: "the problem is where ever my watermark
 * message comes it is hiding my page content."
 * </p>
 * <p>
 * I enclosed the mark drawing code in <code>saveGraphicsState</code> and
 * <code>restoreGraphicsState</code> to protect the original content from
 * being influenced by state changes by the the mark drawing code.
 * </p>
 */
void watermarkOriginal(PDDocument document, PDFont pdfFont, double degree, double x, double y, String text) throws IOException
{
    List<?> pages = document.getDocumentCatalog().getAllPages();
    float fontSize = 70.0f;
    for (int i = 0; i < pages.size(); i++) {
        PDPage page = (PDPage) pages.get(i);
        PDRectangle pageSize = page.findMediaBox();
        float stringWidth = pdfFont.getStringWidth(text) * fontSize
                / 1000f;
        // calculate to center of the page
        int rotation = page.findRotation();
        boolean rotate = degree > 0;
        float pageWidth = rotate ? pageSize.getHeight() : pageSize
                .getWidth();
        float pageHeight = rotate ? pageSize.getWidth() : pageSize
                .getHeight();
        double centeredXPosition = rotate ? pageHeight / 2f
                : (pageWidth - stringWidth) / 2f;
        double centeredYPosition = rotate ? (pageWidth - stringWidth) / 2f
                : pageHeight / 2f;
        // append the content to the existing stream
        PDPageContentStream contentStream = new PDPageContentStream(
                document, page, true, true, true);
        contentStream.saveGraphicsState();
        contentStream.beginText();
        // set font and font size
        contentStream.setFont(pdfFont, fontSize);
        // set text color to red
        contentStream.setNonStrokingColor(240, 240, 240);
        if (rotate) {
            // rotate the text according to the page rotation
            contentStream.setTextRotation(degree, x, y);
        } else {
            contentStream.setTextTranslation(centeredXPosition,
                    centeredYPosition);
        }
        contentStream.drawString(text);
        contentStream.endText();
        contentStream.restoreGraphicsState();
        contentStream.close();
    }
}
 
开发者ID:mkl-public,项目名称:testarea-pdfbox1,代码行数:54,代码来源:UnderlayText.java

示例2: drawReletivePartAndMovePosition

import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
/**
    * 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());
}
   }
 
开发者ID:ralfstuckert,项目名称:pdfbox-layout,代码行数:60,代码来源:VerticalLayout.java

示例3: drawAligned

import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
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();
   }
 
开发者ID:ralfstuckert,项目名称:pdfbox-layout,代码行数:64,代码来源:TextLine.java


注:本文中的org.apache.pdfbox.pdmodel.edit.PDPageContentStream.saveGraphicsState方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。