• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java PDFormXObject类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject的典型用法代码示例。如果您正苦于以下问题:Java PDFormXObject类的具体用法?Java PDFormXObject怎么用?Java PDFormXObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



PDFormXObject类属于org.apache.pdfbox.pdmodel.graphics.form包,在下文中一共展示了PDFormXObject类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: testMultiPageJFreeChart

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的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: exportGraphic

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的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: getImagesFromResources

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的package包/类
private List<RenderedImage> getImagesFromResources(PDResources resources) throws IOException {

        List<RenderedImage> images = new ArrayList<>();
        for (COSName xObjectName : resources.getXObjectNames()) {
            PDXObject xObject = resources.getXObject(xObjectName);
            if (xObject instanceof PDImageXObject) {
                images.add(((PDImageXObject) xObject).getImage());
            } else if (xObject instanceof PDFormXObject) {
                images.addAll(getImagesFromResources(((PDFormXObject) xObject).getResources()));
            }
        }
        return images;
    }
 
开发者ID:abelsromero,项目名称:pdf-box-examples,代码行数:14,代码来源:PdfImagesHelper.java


示例4: extractFontResources

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的package包/类
private void extractFontResources(PDResources resources) throws IOException {
    for (COSName key : resources.getFontNames()) {
        PDFont font = resources.getFont(key);
        extractStrategy.extract(font);
    }

    for (COSName name : resources.getXObjectNames()) {
        PDXObject xobject = resources.getXObject(name);
        if (xobject instanceof PDFormXObject) {
            PDFormXObject xObjectForm = (PDFormXObject) xobject;
            PDResources formResources = xObjectForm.getResources();

            if (formResources != null)
                extractFontResources(formResources);
        }
    }
}
 
开发者ID:m-abboud,项目名称:FontVerter,代码行数:18,代码来源:PdfFontExtractor.java


示例5: join

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的package包/类
/**
 * @see #testJoinSmallAndBig()
 */
void join(PDDocument target, PDDocument topSource, PDDocument bottomSource) throws IOException {
    LayerUtility layerUtility = new LayerUtility(target);
    PDFormXObject topForm = layerUtility.importPageAsForm(topSource, 0);
    PDFormXObject bottomForm = layerUtility.importPageAsForm(bottomSource, 0);

    float height = topForm.getBBox().getHeight() + bottomForm.getBBox().getHeight();
    float width, topMargin, bottomMargin;
    if (topForm.getBBox().getWidth() > bottomForm.getBBox().getWidth()) {
        width = topForm.getBBox().getWidth();
        topMargin = 0;
        bottomMargin = (topForm.getBBox().getWidth() - bottomForm.getBBox().getWidth()) / 2;
    } else {
        width = bottomForm.getBBox().getWidth();
        topMargin = (bottomForm.getBBox().getWidth() - topForm.getBBox().getWidth()) / 2;
        bottomMargin = 0;
    }

    PDPage targetPage = new PDPage(new PDRectangle(width, height));
    target.addPage(targetPage);


    PDPageContentStream contentStream = new PDPageContentStream(target, targetPage);
    if (bottomMargin != 0)
        contentStream.transform(Matrix.getTranslateInstance(bottomMargin, 0));
    contentStream.drawForm(bottomForm);
    contentStream.transform(Matrix.getTranslateInstance(topMargin - bottomMargin, bottomForm.getBBox().getHeight()));
    contentStream.drawForm(topForm);
    contentStream.close();
}
 
开发者ID:mkl-public,项目名称:testarea-pdfbox2,代码行数:33,代码来源:JoinPages.java


示例6: processFontResources

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的package包/类
private void processFontResources(PDResources resources, FontTable table) throws IOException
{
    String fontNotSupportedMessage = "Font: {} skipped because type '{}' is not supported.";

    for (COSName key : resources.getFontNames())
    {
        PDFont font = resources.getFont(key);
        if (font instanceof PDTrueTypeFont)
        {
            table.addEntry( font);
            log.debug("Font: " + font.getName() + " TTF");
        }
        else if (font instanceof PDType0Font)
        {
            PDCIDFont descendantFont = ((PDType0Font) font).getDescendantFont();
            if (descendantFont instanceof PDCIDFontType2)
                table.addEntry(font);
            else
                log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName());
        }
        else if (font instanceof PDType1CFont)
            table.addEntry(font);
        else
            log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName());
    }

    for (COSName name : resources.getXObjectNames())
    {
        PDXObject xobject = resources.getXObject(name);
        if (xobject instanceof PDFormXObject)
        {
            PDFormXObject xObjectForm = (PDFormXObject) xobject;
            PDResources formResources = xObjectForm.getResources();
            if (formResources != null)
                processFontResources(formResources, table);
        }
    }

}
 
开发者ID:radkovo,项目名称:Pdf2Dom,代码行数:40,代码来源:PDFBoxTree.java


示例7: getXFormObject

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的package包/类
/**
 * 
 * @return the PDAppearanceStream which resulted in this graphics
 */
@SuppressWarnings("WeakerAccess")
public PDFormXObject getXFormObject() {
	if (document != null)
		throw new IllegalStateException("You can only get the XformObject after you disposed the Graphics2D!");
	if (cloneInfo != null)
		throw new IllegalStateException("You can not get the Xform stream from the clone");
	return xFormObject;
}
 
开发者ID:rototor,项目名称:pdfbox-graphics2d,代码行数:13,代码来源:PdfBoxGraphics2D.java


示例8: processOperator

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的package包/类
/**
 * This is used to handle an operation.
 *
 * @param operator The operation to perform.
 * @param operands The list of arguments.
 * @throws IOException If there is an error processing the operation.
 */
@Override
protected void processOperator(Operator operator, List<COSBase> operands) throws IOException {
    String operation = operator.getName();
    if (INVOKE_OPERATOR.equals(operation)) {
        COSName objectName = (COSName) operands.get(0);
        PDXObject xobject = getResources().getXObject(objectName);
        if (xobject instanceof PDImageXObject) {

            PDImageXObject image = (PDImageXObject) xobject;
            Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();

            Image im = new Image();
            im.setPage(currentPage);
            im.setXPosition(ctmNew.getTranslateX());
            im.setYPosition(ctmNew.getTranslateY());
            im.setOriginalHeight(image.getHeight());
            im.setOriginalWidth(image.getWidth());
            im.setRenderedWidth(Math.round(ctmNew.getScaleX()));
            im.setRenderedHeight(Math.round(ctmNew.getScaleY()));
            images.add(im);

            if (!output.exists()) output.mkdirs();
            // TODO enable option to set the output format. right now it uses original which means: tiff needs extra dependency and tiff are HUGE!
            // String extension = "png";
            String extension = image.getSuffix();
            File out = new File(output, basename + "-" + currentPage + "-" + count + "." + extension);

            ImageIO.write(image.getImage(), extension, new FileOutputStream(out));
            count++;

        } else if (xobject instanceof PDFormXObject) {
            PDFormXObject form = (PDFormXObject) xobject;
            showForm(form);
        }
    } else {
        super.processOperator(operator, operands);
    }
}
 
开发者ID:abelsromero,项目名称:pdf-box-examples,代码行数:46,代码来源:ImageExtractor.java


示例9: drawForm

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的package包/类
/**
 * Draws the given Form XObject at the current location.
 *
 * @param form
 *        Form XObject
 * @throws IOException
 *         if the content stream could not be written
 * @throws IllegalStateException
 *         If the method was called within a text block.
 */
public void drawForm (final PDFormXObject form) throws IOException
{
  if (inTextMode)
  {
    throw new IllegalStateException ("Error: drawForm is not allowed within a text block.");
  }

  writeOperand (resources.add (form));
  writeOperator ((byte) 'D', (byte) 'o');
}
 
开发者ID:phax,项目名称:ph-pdf-layout,代码行数:21,代码来源:PDPageContentStreamExt.java


示例10: removeToUnicodeMaps

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的package包/类
void removeToUnicodeMaps(PDResources pdResources) throws IOException
{
    COSDictionary resources = pdResources.getCOSObject();

    COSDictionary fonts = asDictionary(resources, COSName.FONT);
    if (fonts != null)
    {
        for (COSBase object : fonts.getValues())
        {
            while (object instanceof COSObject)
                object = ((COSObject)object).getObject();
            if (object instanceof COSDictionary)
            {
                COSDictionary font = (COSDictionary)object;
                font.removeItem(COSName.TO_UNICODE);
            }
        }
    }

    for (COSName name : pdResources.getXObjectNames())
    {
        PDXObject xobject = pdResources.getXObject(name);
        if (xobject instanceof PDFormXObject)
        {
            PDResources xobjectPdResources = ((PDFormXObject)xobject).getResources();
            removeToUnicodeMaps(xobjectPdResources);
        }
    }
}
 
开发者ID:mkl-public,项目名称:testarea-pdfbox2,代码行数:30,代码来源:ExtractText.java


示例11: process

import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; //导入依赖的package包/类
@Override
public void process(Operator operator, List<COSBase> arguments)
    throws IOException {
  // Get the name of the PDXOject.
  COSName name = (COSName) arguments.get(0);

  // Get the PDXObject.
  PDXObject xobject = context.getResources().getXObject(name);

  if (xobject instanceof PDFormXObject) {
    context.processStream((PDFormXObject) xobject);
  } else if(xobject instanceof PDImageXObject) {
    PDImageXObject image = (PDImageXObject) xobject;

    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    Matrix ctm = context.getCurrentTransformationMatrix().clone();
    AffineTransform ctmAT = ctm.createAffineTransform();
    ctmAT.scale(1f / imageWidth, 1f / imageHeight);
    Matrix at = new Matrix(ctmAT);

    Rectangle boundBox = SimpleRectangle.from2Vertices(
        new SimplePoint(ctm.getTranslateX(), ctm.getTranslateY()),
        new SimplePoint(ctm.getTranslateX() + at.getScaleX() * imageWidth,
            ctm.getTranslateY() + at.getScaleY() * imageHeight));

    // Rectangle boundBox = new SimpleRectangle();
    // boundBox.setMinX(ctm.getTranslateX());
    // boundBox.setMinY(ctm.getTranslateY());
    // boundBox.setMaxX(ctm.getTranslateX() + at.getScaleX() * imageWidth);
    // boundBox.setMaxY(ctm.getTranslateY() + at.getScaleY() * imageHeight);

    PdfBoxColor exclusiveColor = getExclusiveColor(image.getImage());

    if (exclusiveColor != null) {
      PdfBoxShape shape = new PdfBoxShape(context.getCurrentPage());
      shape.setRectangle(boundBox);
      shape.setColor(exclusiveColor);
      context.showShape(shape);
    } else {
      PdfBoxFigure figure = new PdfBoxFigure(context.getCurrentPage());
      figure.setRectangle(boundBox);
      context.showFigure(figure);
    }
  }
}
 
开发者ID:ckorzen,项目名称:icecite,代码行数:48,代码来源:Invoke.java



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java ManagedBean类代码示例发布时间:2022-05-23
下一篇:
Java Func类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap