Java Code Examples for org.apache.pdfbox.pdmodel.PDPageContentStream.drawImage()

The following are Jave code examples for showing how to use drawImage() of the org.apache.pdfbox.pdmodel.PDPageContentStream class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: PrintDF   File: BookletMaker.java   Source Code and License Vote up 7 votes
/**
 * Creates a pdf page from two pages from another 'original' pdf document
 * @param doc original pdf from which the pages will be taken
 * @param leftPage page number of the page to go on the left side 
 * @param rightPage page number of the page to go on the right side
 * @return generated page containing the left and right pages from the original document side-by-side.
 */
private static PDPage createBookPage(PDDocument doc, int leftPage, int rightPage) {
	// double the width of a normal page to create the booklet
	PDRectangle baseSize = doc.getPage(0).getMediaBox();		
	PDRectangle box = new PDRectangle(baseSize.getWidth()*2, baseSize.getHeight());
	

	if(sizeOverride != null) {
		box = sizeOverride.asPDRectangle();
	}
	
	PDPage page = new PDPage(box);
	
	try {
		PDImageXObject leftImg = PrintDF.pageToImage(doc, leftPage, scale);
		PDImageXObject rightImg = PrintDF.pageToImage(doc, rightPage, scale);
		
		
		
		PDPageContentStream contentStream = new PDPageContentStream(doc, page);
		if(leftImg != null)
			contentStream.drawImage(leftImg, 0, 0, box.getWidth()/2, box.getHeight());
		if(rightImg != null)
			contentStream.drawImage(rightImg, box.getWidth()/2, 0, box.getWidth()/2, box.getHeight());
		contentStream.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	return page;
}
 
Example 2
Project: kumoreg   File: StaffBadge2017.java   Source Code and License Vote up 7 votes
private void drawAgeImageFront(PDPage page, Attendee attendee) throws IOException {

        String imageFilename = BadgeLib.getStaffAgeImageFilename(attendee, currentDateForAgeCalculation);
        if (imageFilename == null) { return; }

        PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);

        PDImageXObject xImage = PDImageXObject.createFromFile(imageFilename, document);
        Dimension scaledDim = getScaledDimension(
                new Dimension(xImage.getWidth(),  xImage.getHeight()),
                new Dimension(149, 130));
        stream.drawImage(xImage,
                45 + ((149-scaledDim.width)/2),
                335 + ((158-scaledDim.height)/2),
                scaledDim.width, scaledDim.height);
        stream.close();
    }
 
Example 3
Project: kumoreg   File: StaffBadge2017.java   Source Code and License Vote up 7 votes
private void drawAgeImageBack(PDPage page, Attendee attendee) throws IOException {
    String imageFilename = BadgeLib.getStaffAgeImageFilename(attendee, currentDateForAgeCalculation);
    if (imageFilename == null) { return; }

    PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, DRAW_BOUNDING_BOX);

    PDImageXObject xImage = PDImageXObject.createFromFile(imageFilename, document);
    Dimension scaledDim = getScaledDimension(
            new Dimension(xImage.getWidth(),  xImage.getHeight()),
            new Dimension(149, 115));
    stream.drawImage(xImage,
            201 + ((149-scaledDim.width)/2),
            344 + ((158-scaledDim.height)/2),
            scaledDim.width, scaledDim.height);
    stream.close();
}
 
Example 4
Project: pdfbox-layout   File: CompatibilityHelper.java   Source Code and License Vote up 7 votes
public static void drawImage(final BufferedImage image,
    final PDDocument document, final PDPageContentStream contentStream,
    Position upperLeft, final float width, final float height)
    throws IOException {
PDImageXObject cachedImage = getCachedImage(document, image);
float x = upperLeft.getX();
float y = upperLeft.getY() - height;
contentStream.drawImage(cachedImage, x, y, width, height);
   }
 
Example 5
Project: pdfbox-graphics2d   File: PdfBoxGraphics2DPaintApplier.java   Source Code and License Vote up 6 votes
private void applyTexturePaint(TexturePaint texturePaint) throws IOException {
	Rectangle2D anchorRect = texturePaint.getAnchorRect();
	PDTilingPattern pattern = new PDTilingPattern();
	pattern.setPaintType(PDTilingPattern.PAINT_COLORED);
	pattern.setTilingType(PDTilingPattern.TILING_CONSTANT_SPACING_FASTER_TILING);

	pattern.setBBox(new PDRectangle((float) anchorRect.getX(), (float) anchorRect.getY(),
			(float) anchorRect.getWidth(), (float) anchorRect.getHeight()));
	pattern.setXStep((float) anchorRect.getWidth());
	pattern.setYStep((float) anchorRect.getHeight());

	AffineTransform patternTransform = new AffineTransform();
	patternTransform.translate(0, anchorRect.getHeight());
	patternTransform.scale(1f, -1f);
	pattern.setMatrix(patternTransform);

	PDAppearanceStream appearance = new PDAppearanceStream(document);
	appearance.setResources(pattern.getResources());
	appearance.setBBox(pattern.getBBox());

	PDPageContentStream imageContentStream = new PDPageContentStream(document, appearance,
			((COSStream) pattern.getCOSObject()).createOutputStream());
	BufferedImage texturePaintImage = texturePaint.getImage();
	PDImageXObject imageXObject = imageEncoder.encodeImage(document, imageContentStream, texturePaintImage);

	float ratioW = (float) ((anchorRect.getWidth()) / texturePaintImage.getWidth());
	float ratioH = (float) ((anchorRect.getHeight()) / texturePaintImage.getHeight());
	float paintHeight = (texturePaintImage.getHeight()) * ratioH;
	imageContentStream.drawImage(imageXObject, (float) anchorRect.getX(), (float) (paintHeight + anchorRect.getY()),
			texturePaintImage.getWidth() * ratioW, -paintHeight);
	imageContentStream.close();

	PDColorSpace patternCS1 = new PDPattern(null, imageXObject.getColorSpace());
	COSName tilingPatternName = resources.add(pattern);
	PDColor patternColor = new PDColor(tilingPatternName, patternCS1);

	contentStream.setNonStrokingColor(patternColor);
	contentStream.setStrokingColor(patternColor);
}
 
Example 6
Project: BoxTable   File: ImageCell.java   Source Code and License Vote up 4 votes
@Override
public void render(final PDDocument document, final PDPageContentStream stream, final float left, final float top, final float width, final float height)
		throws IOException {
	final BufferedImage image = ImageIO.read(file);

	float contentWidth = width - (leftBorder + rightBorder) / 2 - leftPadding - rightPadding;
	float contentHeight = height - (topBorder + bottomBorder) / 2 - topPadding - bottomPadding;

	float imgWidth = image.getWidth();
	float imgHeight = image.getHeight();
	final float ratio = imgWidth / imgHeight;
	if (imgWidth > contentWidth) {
		imgWidth = contentWidth;
		imgHeight = 1 / ratio * imgWidth;
	}
	if (imgHeight > contentHeight) {
		imgHeight = contentHeight;
		imgWidth = ratio * imgHeight;
	}

	super.render(document, stream, left + (width - imgWidth) * hAlign, top - (height - imgHeight) * vAlign, imgWidth, imgHeight);

	final PDImageXObject imageObject = JPEGFactory.createFromImage(document, image);
	stream.drawImage(imageObject, left + (width - imgWidth) * hAlign + leftBorder / 2 + leftPadding,
			top - height + (height - imgHeight) * (1 - vAlign) - topBorder / 2 - topPadding,
			imgWidth, imgHeight);
}
 
Example 7
Project: AwesomeJavaLibraryExamples   File: ExampleAddImage.java   Source Code and License Vote up 4 votes
private static void createFinalDocument(File pdfFile, File overlayPdf) throws IOException
{
   PDDocument doc = new PDDocument();

   try
   {
      PDPage page = new PDPage();

      doc.addPage(page);
      PDImageXObject pdImage =JPEGFactory.createFromStream(doc, new AutoCloseInputStream(ExampleAddImage.class.getClassLoader().getResourceAsStream("death-star-plans.jpg")));
      PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true);
      Map<Integer, String> overlayGuide = new HashMap<>();

      contentStream.drawImage(pdImage, 100, 300, pdImage.getWidth()*scale, pdImage.getHeight()*scale);

      overlayGuide.put(1, overlayPdf.getAbsolutePath());
      Overlay overlay = new Overlay();
      overlay.setInputPDF(doc);
      overlay.setOverlayPosition(Overlay.Position.FOREGROUND);
      overlay.overlay(overlayGuide);

      contentStream.close();
      doc.save(pdfFile);
   }
   finally
   {
      doc.close();
   }
}
 
Example 8
Project: cat-boot   File: ReportImage.java   Source Code and License Vote up 4 votes
/**
 * <p>Call this method to print images. <b>Make sure that the streams are closed before calling this method </b></p>
 * <p>Normal print method doesn't work since: http://stackoverflow.com/questions/9326245/how-to-exactly-position-an-image-inside-an-existing-pdf-page-using-pdfbox</p>
 *
 * @param document   the pdDocument.
 * @param pageNumber page of image
 * @param x          location of image
 * @param y          location of image
 * @throws java.io.IOException in case there are problems at reading or writing the image
 */
public void printImage(PDDocument document, int pageNumber, float x, float y) throws IOException {
    PDImageXObject obj = JPEGFactory.createFromImage(document, img);

    PDPageContentStream currentStream = new PDPageContentStream(document,
            document.getDocumentCatalog().getPages().get(pageNumber), PDPageContentStream.AppendMode.APPEND, false);

    currentStream.drawImage(obj, x, y - height, width, height);
    currentStream.close();
}