本文整理汇总了Java中org.apache.pdfbox.pdmodel.PDPageContentStream.drawImage方法的典型用法代码示例。如果您正苦于以下问题:Java PDPageContentStream.drawImage方法的具体用法?Java PDPageContentStream.drawImage怎么用?Java PDPageContentStream.drawImage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.PDPageContentStream
的用法示例。
在下文中一共展示了PDPageContentStream.drawImage方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createBookPage
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
/**
* 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;
}
示例2: drawAgeImageFront
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
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();
}
示例3: drawAgeImageBack
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
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();
}
示例4: drawImage
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
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);
}
示例5: applyTexturePaint
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
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);
}
示例6: createFinalDocument
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
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();
}
}
示例7: render
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
@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);
}
示例8: printImage
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
/**
* <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();
}