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


Java Matrix.scale方法代码示例

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


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

示例1: testMultiPageJFreeChart

import org.apache.pdfbox.util.Matrix; //导入方法依赖的package包/类
@Test
public void testMultiPageJFreeChart() throws IOException {
	File parentDir = new File("target/test/multipage");
	// noinspection ResultOfMethodCallIgnored
	parentDir.mkdirs();
	File targetPDF = new File(parentDir, "multipage.pdf");
	PDDocument document = new PDDocument();
	for (int i = 0; i < 4; i++) {
		PDPage page = new PDPage(PDRectangle.A4);
		document.addPage(page);

		PDPageContentStream contentStream = new PDPageContentStream(document, page);
		PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 800, 400);
		drawOnGraphics(pdfBoxGraphics2D, i);
		pdfBoxGraphics2D.dispose();

		PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
		Matrix matrix = new Matrix();
		matrix.translate(0, 30);
		matrix.scale(0.7f, 1f);

		contentStream.saveGraphicsState();
		contentStream.transform(matrix);
		contentStream.drawForm(appearanceStream);
		contentStream.restoreGraphicsState();

		contentStream.close();
	}
	document.save(targetPDF);
	document.close();
}
 
开发者ID:rototor,项目名称:pdfbox-graphics2d,代码行数:32,代码来源:MultiPageTest.java

示例2: drawText

import org.apache.pdfbox.util.Matrix; //导入方法依赖的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

示例3: exportGraphic

import org.apache.pdfbox.util.Matrix; //导入方法依赖的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


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