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


Java PDPageContentStream.setTextMatrix方法代码示例

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


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

示例1: drawText

import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
@Override
public void drawText(AttributedCharacterIterator iterator, IFontTextDrawerEnv env)
		throws IOException, FontFormatException {
	PDPageContentStream contentStream = env.getContentStream();

	contentStream.beginText();

	Matrix textMatrix = new Matrix();
	textMatrix.scale(1, -1);
	contentStream.setTextMatrix(textMatrix);

	StringBuilder sb = new StringBuilder();
	boolean run = true;
	while (run) {

		Font attributeFont = (Font) iterator.getAttribute(TextAttribute.FONT);
		if (attributeFont == null)
			attributeFont = env.getFont();

		Number fontSize = ((Number) iterator.getAttribute(TextAttribute.SIZE));
		if (fontSize != null)
			attributeFont = attributeFont.deriveFont(fontSize.floatValue());
		PDFont font = applyFont(attributeFont, env);

		Paint paint = (Paint) iterator.getAttribute(TextAttribute.FOREGROUND);
		if (paint == null)
			paint = env.getPaint();

		/*
		 * Apply the paint
		 */
		env.applyPaint(paint);

		boolean isStrikeThrough = TextAttribute.STRIKETHROUGH_ON
				.equals(iterator.getAttribute(TextAttribute.STRIKETHROUGH));
		boolean isUnderline = TextAttribute.UNDERLINE_ON.equals(iterator.getAttribute(TextAttribute.UNDERLINE));
		boolean isLigatures = TextAttribute.LIGATURES_ON.equals(iterator.getAttribute(TextAttribute.LIGATURES));

		run = iterateRun(iterator, sb);
		String text = sb.toString();

		/*
		 * If we force the text write we may encounter situations where the font can not
		 * display the characters. PDFBox will throw an exception in this case. We will
		 * just silently ignore the text and not display it instead.
		 */
		try {
			showTextOnStream(env, contentStream, attributeFont, font, isStrikeThrough, isUnderline, isLigatures,
					text);
		} catch (IllegalArgumentException e) {
			if (font instanceof PDType1Font && !font.isEmbedded()) {
				/*
				 * We tried to use a builtin default font, but it does not have the needed
				 * characters. So we use a embedded font as fallback.
				 */
				try {
					if (fallbackFontUnknownEncodings == null)
						fallbackFontUnknownEncodings = findFallbackFont(env);
					if (fallbackFontUnknownEncodings != null) {
						env.getContentStream().setFont(fallbackFontUnknownEncodings, attributeFont.getSize2D());
						showTextOnStream(env, contentStream, attributeFont, fallbackFontUnknownEncodings,
								isStrikeThrough, isUnderline, isLigatures, text);
						e = null;
					}
				} catch (IllegalArgumentException ignored) {
					e = ignored;
				}
			}

			if (e != null)
				System.err.println("PDFBoxGraphics: Can not map text " + text + " with font "
						+ attributeFont.getFontName() + ": " + e.getMessage());
		}
	}
	contentStream.endText();
}
 
开发者ID:rototor,项目名称:pdfbox-graphics2d,代码行数:77,代码来源:PdfBoxGraphics2DFontTextDrawer.java

示例2: exportGraphic

import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
@SuppressWarnings("SpellCheckingInspection")
void exportGraphic(String dir, String name, GraphicsExporter exporter) {
	try {
		PDDocument document = new PDDocument();

		PDFont pdArial = PDFontFactory.createDefaultFont();

		File parentDir = new File("target/test/" + dir);
		// noinspection ResultOfMethodCallIgnored
		parentDir.mkdirs();

		BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_4BYTE_ABGR);
		Graphics2D imageGraphics = image.createGraphics();
		exporter.draw(imageGraphics);
		imageGraphics.dispose();
		ImageIO.write(image, "PNG", new File(parentDir, name + ".png"));

		for (Mode m : Mode.values()) {
			PDPage page = new PDPage(PDRectangle.A4);
			document.addPage(page);

			PDPageContentStream contentStream = new PDPageContentStream(document, page);
			PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 400, 400);
			PdfBoxGraphics2DFontTextDrawer fontTextDrawer = null;
			contentStream.beginText();
			contentStream.setStrokingColor(0, 0, 0);
			contentStream.setNonStrokingColor(0, 0, 0);
			contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15);
			contentStream.setTextMatrix(Matrix.getTranslateInstance(10, 800));
			contentStream.showText("Mode " + m);
			contentStream.endText();
			switch (m) {
			case FontTextIfPossible:
				fontTextDrawer = new PdfBoxGraphics2DFontTextDrawer();
				fontTextDrawer.registerFont(
						new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
				break;
			case DefaultFontText: {
				fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
				fontTextDrawer.registerFont(
						new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
				break;
			}
			case ForceFontText:
				fontTextDrawer = new PdfBoxGraphics2DFontTextForcedDrawer();
				fontTextDrawer.registerFont(
						PdfBoxGraphics2DTestBase.class.getResourceAsStream("DejaVuSerifCondensed.ttf"));
				fontTextDrawer.registerFont("Arial", pdArial);
				break;
			case DefaultVectorized:
			default:
				break;
			}

			if (fontTextDrawer != null) {
				pdfBoxGraphics2D.setFontTextDrawer(fontTextDrawer);
			}

			exporter.draw(pdfBoxGraphics2D);
			pdfBoxGraphics2D.dispose();

			PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
			Matrix matrix = new Matrix();
			matrix.translate(0, 20);
			contentStream.transform(matrix);
			contentStream.drawForm(appearanceStream);

			matrix.scale(1.5f, 1.5f);
			matrix.translate(0, 100);
			contentStream.transform(matrix);
			contentStream.drawForm(appearanceStream);
			contentStream.close();
		}

		document.save(new File(parentDir, name + ".pdf"));
		document.close();
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
开发者ID:rototor,项目名称:pdfbox-graphics2d,代码行数:81,代码来源:PdfBoxGraphics2DTestBase.java

示例3: drawStringRotatedWithResizing

import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
/**
     * Draws the given string, optionally supports scaling to fit.
     * @param x Left side of text, or center point of text if centered (1/72 inch)
     * @param y Bottom of text, in points (1/72 inch)
     * @param text Text to draw
     * @param optOrig Resize Options
     * @throws IOException Error generating PDF
     */
    void drawStringRotatedWithResizing(PDPageContentStream stream, float x, float y, int width, int height, String text, ResizeOptions optOrig) throws IOException {
        stream.setNonStrokingColor(Color.gray);
        PDRectangle boundingBox = new PDRectangle(x, y, width, height);
        stream.fillRect(x, y, width, height);

        ResizeOptions opt = new ResizeOptions(optOrig);
        float textSize = opt.font.getStringWidth(text); // in thousandths of font pt size.
        float size = opt.size;

        float centeredXPosition = x + (boundingBox.getWidth()/2f);
        float stringWidth = opt.font.getStringWidth( text );
        float centeredYPosition = y + (boundingBox.getHeight() /2f);

        stream.setNonStrokingColor(Color.GREEN);
        stream.fillRect(centeredXPosition, centeredYPosition, 3, 3);

        Matrix offset = Matrix.getRotateInstance(90 * Math.PI * 0.25,
                centeredXPosition, centeredYPosition-(stringWidth/1000/2));
        stream.beginText();
        stream.setTextMatrix(offset);

        stream.setStrokingColor(Color.black);
        stream.setNonStrokingColor(Color.black);
        stream.setFont(opt.font, size);
        stream.showText(text);
        stream.endText();

//
//        // If text size is greater than maximum width, recalculate the correct font size, based on our restrictions
//        if (textSize * (size/1000.0f) > opt.maxTextWidth) {
//            size = opt.maxTextWidth * 1000.0f / textSize;
//            if (size < opt.minFontSize) {
//                // We have utterly failed to fit the text with the minimum font size,
//                // So we're forced to use that.
//                size = opt.minFontSize;
//            }
//        }
//
//        if (opt.centered) {
//            y -= textSize * (size/(2*1000.0f));
//        }
////
////        Matrix offset = Matrix.getRotateInstance(i * Math.PI * 0.25,
////                centered XPosition, pageSize.getHeight() - centeredYPosition)
//        Matrix offset = new Matrix();
//        offset.rotate(Math.toRadians(5));
//        offset.translate(100, 100);
//        // Actually draw the text
    }
 
开发者ID:kumoregdev,项目名称:kumoreg,代码行数:58,代码来源:FormatterBase.java

示例4: setTextTranslation

import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
public static void setTextTranslation(
    final PDPageContentStream contentStream, final float x,
    final float y) throws IOException {
contentStream.setTextMatrix(Matrix.getTranslateInstance(x, y));
   }
 
开发者ID:ralfstuckert,项目名称:pdfbox-layout,代码行数:6,代码来源:CompatibilityHelper.java


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