本文整理汇总了Java中org.apache.pdfbox.pdmodel.edit.PDPageContentStream.drawXObject方法的典型用法代码示例。如果您正苦于以下问题:Java PDPageContentStream.drawXObject方法的具体用法?Java PDPageContentStream.drawXObject怎么用?Java PDPageContentStream.drawXObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.edit.PDPageContentStream
的用法示例。
在下文中一共展示了PDPageContentStream.drawXObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: overlayWithDarkenBlendMode
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
void overlayWithDarkenBlendMode(PDDocument document, PDDocument overlay) throws IOException
{
PDXObjectForm xobject = importAsXObject(document, (PDPage) overlay.getDocumentCatalog().getAllPages().get(0));
PDExtendedGraphicsState darken = new PDExtendedGraphicsState();
darken.getCOSDictionary().setName("BM", "Darken");
List<PDPage> pages = document.getDocumentCatalog().getAllPages();
for (PDPage page: pages)
{
Map<String, PDExtendedGraphicsState> states = page.getResources().getGraphicsStates();
if (states == null)
states = new HashMap<String, PDExtendedGraphicsState>();
String darkenKey = MapUtil.getNextUniqueKey(states, "Dkn");
states.put(darkenKey, darken);
page.getResources().setGraphicsStates(states);
PDPageContentStream stream = new PDPageContentStream(document, page, true, false, true);
stream.appendRawCommands(String.format("/%s gs ", darkenKey));
stream.drawXObject(xobject, 0, 0, 1, 1);
stream.close();
}
}
示例2: testDrunkenFistSample
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
/**
* Applying the code to the OP's sample file from a former question.
*
* @throws IOException
* @throws CryptographyException
* @throws COSVisitorException
*/
@Test
public void testDrunkenFistSample() throws IOException, CryptographyException, COSVisitorException
{
try ( InputStream resource = getClass().getResourceAsStream("sample.pdf");
InputStream left = getClass().getResourceAsStream("left.png");
InputStream right = getClass().getResourceAsStream("right.png");
PDDocument document = PDDocument.load(resource) )
{
if (document.isEncrypted())
{
document.decrypt("");
}
PDJpeg leftImage = new PDJpeg(document, ImageIO.read(left));
PDJpeg rightImage = new PDJpeg(document, ImageIO.read(right));
ImageLocator locator = new ImageLocator();
List<?> allPages = document.getDocumentCatalog().getAllPages();
for (int i = 0; i < allPages.size(); i++)
{
PDPage page = (PDPage) allPages.get(i);
locator.processStream(page, page.findResources(), page.getContents().getStream());
}
for (ImageLocation location : locator.getLocations())
{
PDRectangle cropBox = location.getPage().findCropBox();
float center = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2.0f;
PDJpeg image = location.getMatrix().getXPosition() < center ? leftImage : rightImage;
AffineTransform transform = location.getMatrix().createAffineTransform();
PDPageContentStream content = new PDPageContentStream(document, location.getPage(), true, false, true);
content.drawXObject(image, transform);
content.close();
}
document.save(new File(RESULT_FOLDER, "sample-changed.pdf"));
}
}
示例3: drawImageWithScale
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
private static void drawImageWithScale(PDDocument doc, PDPageContentStream content, String imgPath,
int bufferedImageType, float scale) throws IOException {
BufferedImage bufferedImage = ImageIO.read(new File(imgPath));
BufferedImage image = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImageType);
image.createGraphics().drawRenderedImage(bufferedImage, null);
PDXObjectImage xImage = new PDPixelMap(doc, image);
content.drawXObject(xImage, 100, 100, xImage.getWidth() * scale, xImage.getHeight() * scale);
}
示例4: drawImage
import org.apache.pdfbox.pdmodel.edit.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 {
PDXObjectImage cachedImage = getCachedImage(document, image);
float x = upperLeft.getX();
float y = upperLeft.getY() - height;
contentStream.drawXObject(cachedImage, x, y, width, height);
}
示例5: overlayWithDarkenBlendMode
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
public static void overlayWithDarkenBlendMode(PDDocument document, PDDocument overlay) throws IOException
{
PDXObjectForm xobject = importAsXObject(document, (PDPage) overlay.getDocumentCatalog().getAllPages().get(0));
PDExtendedGraphicsState darken = new PDExtendedGraphicsState();
darken.getCOSDictionary().setName("BM", "Darken");
List<PDPage> pages = document.getDocumentCatalog().getAllPages();
for (PDPage page: pages)
{
if (page.getResources() == null) {
page.setResources(page.findResources());
}
if (page.getResources() != null) {
Map<String, PDExtendedGraphicsState> states = page.getResources().getGraphicsStates();
if (states == null) {
states = new HashMap<String, PDExtendedGraphicsState>();
}
String darkenKey = MapUtil.getNextUniqueKey(states, "Dkn");
states.put(darkenKey, darken);
page.getResources().setGraphicsStates(states);
PDPageContentStream stream = new PDPageContentStream(document, page, true, false, true);
stream.appendRawCommands(String.format("/%s gs ", darkenKey));
stream.drawXObject(xobject, 0, 0, 1, 1);
stream.close();
}
}
}
示例6: doSignOneStep
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
/**
* {@link #doSignOriginal(InputStream, InputStream, File)} changed to add the image and sign at the same time.
*/
public void doSignOneStep(InputStream inputStream, InputStream logoStream, File outputDocument) throws Exception
{
byte inputBytes[] = IOUtils.toByteArray(inputStream);
PDDocument pdDocument = PDDocument.load(new ByteArrayInputStream(inputBytes));
PDJpeg ximage = new PDJpeg(pdDocument, ImageIO.read(logoStream));
PDPage page = (PDPage) pdDocument.getDocumentCatalog().getAllPages().get(0);
PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page, true, true);
contentStream.drawXObject(ximage, 50, 50, 356, 40);
contentStream.close();
page.getResources().getCOSObject().setNeedToBeUpdate(true);
page.getResources().getCOSDictionary().getDictionaryObject(COSName.XOBJECT).setNeedToBeUpdate(true);
ximage.getCOSObject().setNeedToBeUpdate(true);
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
signature.setName("signer name");
signature.setLocation("signer location");
signature.setReason("reason for signature");
signature.setSignDate(Calendar.getInstance());
pdDocument.addSignature(signature, new TC3());
FileOutputStream fos = new FileOutputStream(outputDocument);
fos.write(inputBytes);
FileInputStream is = new FileInputStream(outputDocument);
pdDocument.saveIncremental(is, fos);
}
示例7: addImageToPage
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
public void addImageToPage(PDDocument document, int pdfpage, int x, int y, float scale, BufferedImage tmp_image)
throws IOException {
// Convert the image to TYPE_4BYTE_ABGR so PDFBox won't throw exceptions
// (e.g. for transparent png's).
BufferedImage image = new BufferedImage(tmp_image.getWidth(), tmp_image.getHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
image.createGraphics().drawRenderedImage(tmp_image, null);
PDXObjectImage ximage = new PDPixelMap(document, image);
PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(pdfpage);
PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);
contentStream.drawXObject(ximage, x, y, ximage.getWidth() * scale, ximage.getHeight() * scale);
contentStream.close();
}
示例8: appendContent
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
@Override
protected void appendContent(int start, int pages, int page, PDPageContentStream contentStream,
PDRectangle rect) throws IOException {
PDXObjectImage image = getResource().getImage();
if (image != null) {
float width = image.getWidth();
float height = image.getHeight();
AffineTransform transform = new AffineTransform(width, 0, 0, height, getCornerX(
rect.getWidth(), width), getCornerY(rect.getHeight(), height));
transform.rotate((float) ((getRotate() * Math.PI) / 180), 0.5, 0.5);
contentStream.drawXObject(image, transform);
}
}
示例9: appendContent
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
@Override
protected void appendContent(int end, int start, int pages, int page,
PDPageContentStream contentStream, PDRectangle rect) throws IOException {
PDXObjectImage image = getResource().getImage();
if (image != null) {
float width = image.getWidth();
float height = image.getHeight();
AffineTransform transform = new AffineTransform(width, 0, 0, height, getCornerX(
rect.getWidth(), width), getCornerY(rect.getHeight(), height));
transform.rotate((float) ((getRotate() * Math.PI) / 180), 0.5, 0.5);
contentStream.drawXObject(image, transform);
}
}
示例10: createPDF
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
private void createPDF(Patient patient, Image image) throws IOException, COSVisitorException{
PDDocumentInformation pdi = new PDDocumentInformation();
Mandant mandant = (Mandant) ElexisEventDispatcher.getSelected(Mandant.class);
pdi.setAuthor(mandant.getName() + " " + mandant.getVorname());
pdi.setCreationDate(new GregorianCalendar());
pdi.setTitle("Impfausweis " + patient.getLabel());
PDDocument document = new PDDocument();
document.setDocumentInformation(pdi);
PDPage page = new PDPage();
page.setMediaBox(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDRectangle pageSize = page.findMediaBox();
PDFont font = PDType1Font.HELVETICA_BOLD;
PDFont subFont = PDType1Font.HELVETICA;
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(font, 14);
contentStream.moveTextPositionByAmount(40, pageSize.getUpperRightY() - 40);
contentStream.drawString(patient.getLabel());
contentStream.endText();
String dateLabel = sdf.format(Calendar.getInstance().getTime());
String title = Person.load(mandant.getId()).get(Person.TITLE);
String mandantLabel = title + " " + mandant.getName() + " " + mandant.getVorname();
contentStream.beginText();
contentStream.setFont(subFont, 10);
contentStream.moveTextPositionByAmount(40, pageSize.getUpperRightY() - 55);
contentStream.drawString("Ausstellung " + dateLabel + ", " + mandantLabel);
contentStream.endText();
BufferedImage imageAwt = convertToAWT(image.getImageData());
PDXObjectImage pdPixelMap = new PDPixelMap(document, imageAwt);
contentStream.drawXObject(pdPixelMap, 40, 30, pageSize.getWidth() - 80,
pageSize.getHeight() - 100);
contentStream.close();
String outputPath =
CoreHub.userCfg.get(PreferencePage.VAC_PDF_OUTPUTDIR, CoreHub.getWritableUserDir()
.getAbsolutePath());
if (outputPath.equals(CoreHub.getWritableUserDir().getAbsolutePath())) {
SWTHelper
.showInfo(
"Kein Ausgabeverzeichnis definiert",
"Ausgabe erfolgt in: "
+ outputPath
+ "\nDas Ausgabeverzeichnis kann unter Einstellungen\\Klinische Hilfsmittel\\Impfplan definiert werden.");
}
File outputDir = new File(outputPath);
File pdf = new File(outputDir, "impfplan_" + patient.getPatCode() + ".pdf");
document.save(pdf);
document.close();
Desktop.getDesktop().open(pdf);
}
示例11: renderImage
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
public static void renderImage(PDPageContentStream contentStream, PDFJpeg ximage, float x, float y, Alignment align) throws IOException {
if (ximage != null && contentStream != null) {
float imageHeight = ximage.getHeightPoints();
float imageWidth = ximage.getWidthPoints();
float left = x;
float bottom = y - imageHeight;
if (align != null) {
switch (align) {
case TOP_CENTER:
left = x - imageWidth / 2.0f;
bottom = y - imageHeight;
break;
case TOP_RIGHT:
left = x - imageWidth;
bottom = y - imageHeight;
break;
case MIDDLE_LEFT:
left = x;
bottom = y - imageHeight / 2.0f;
break;
case MIDDLE_CENTER:
left = x - imageWidth / 2.0f;
bottom = y - imageHeight / 2.0f;
break;
case MIDDLE_RIGHT:
left = x - imageWidth;
bottom = y - imageHeight / 2.0f;
break;
case BOTTOM_LEFT:
left = x;
bottom = y;
break;
case BOTTOM_CENTER:
left = x - imageWidth / 2.0f;
bottom = y;
break;
case BOTTOM_RIGHT:
left = x - imageWidth;
bottom = y;
break;
default: // topleft else...
}
}
// http://stackoverflow.com/questions/15789954/pdfbox-pdjpeg-gets-drawn-in-wrong-colors-when-resized-adobe-reader-window
contentStream.drawXObject(ximage, left, bottom, imageWidth, imageHeight);
}
}
示例12: doSign
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
public void doSign() throws Exception
{
byte inputBytes[] = IOUtils.toByteArray(new FileInputStream("resources/rooster.pdf"));
PDDocument pdDocument = PDDocument.load(new ByteArrayInputStream(inputBytes));
PDJpeg ximage = new PDJpeg(pdDocument, ImageIO.read(new File("resources/logo.jpg")));
PDPage page = (PDPage) pdDocument.getDocumentCatalog().getAllPages().get(0);
PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page, true, true);
contentStream.drawXObject(ximage, 50, 50, 356, 40);
contentStream.close();
ByteArrayOutputStream os = new ByteArrayOutputStream();
pdDocument.save(os);
os.flush();
pdDocument.close();
inputBytes = os.toByteArray();
pdDocument = PDDocument.load(new ByteArrayInputStream(inputBytes));
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
signature.setName("signer name");
signature.setLocation("signer location");
signature.setReason("reason for signature");
signature.setSignDate(Calendar.getInstance());
pdDocument.addSignature(signature, this);
File outputDocument = new File("resources/signed.pdf");
ByteArrayInputStream fis = new ByteArrayInputStream(inputBytes);
FileOutputStream fos = new FileOutputStream(outputDocument);
byte[] buffer = new byte[8 * 1024];
int c;
while ((c = fis.read(buffer)) != -1)
{
fos.write(buffer, 0, c);
}
fis.close();
FileInputStream is = new FileInputStream(outputDocument);
pdDocument.saveIncremental(is, fos);
pdDocument.close();
}
示例13: doSignOriginal
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
/**
* Essentially the original code from {@link TC3#doSign()} with dynamic input streams and output file.
*/
public void doSignOriginal(InputStream inputStream, InputStream logoStream, File outputDocument) throws Exception
{
byte inputBytes[] = IOUtils.toByteArray(inputStream);
PDDocument pdDocument = PDDocument.load(new ByteArrayInputStream(inputBytes));
PDJpeg ximage = new PDJpeg(pdDocument, ImageIO.read(logoStream));
PDPage page = (PDPage) pdDocument.getDocumentCatalog().getAllPages().get(0);
PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page, true, true);
contentStream.drawXObject(ximage, 50, 50, 356, 40);
contentStream.close();
ByteArrayOutputStream os = new ByteArrayOutputStream();
pdDocument.save(os);
os.flush();
pdDocument.close();
inputBytes = os.toByteArray();
pdDocument = PDDocument.load(new ByteArrayInputStream(inputBytes));
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
signature.setName("signer name");
signature.setLocation("signer location");
signature.setReason("reason for signature");
signature.setSignDate(Calendar.getInstance());
pdDocument.addSignature(signature, new TC3());
ByteArrayInputStream fis = new ByteArrayInputStream(inputBytes);
FileOutputStream fos = new FileOutputStream(outputDocument);
byte[] buffer = new byte[8 * 1024];
int c;
while ((c = fis.read(buffer)) != -1)
{
fos.write(buffer, 0, c);
}
fis.close();
FileInputStream is = new FileInputStream(outputDocument);
pdDocument.saveIncremental(is, fos);
pdDocument.close();
}
示例14: doSignRemoveType
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
/**
* {@link #doSignOriginal(InputStream, InputStream, File)} changed to remove the Type entry from the trailer.
*/
public void doSignRemoveType(InputStream inputStream, InputStream logoStream, File outputDocument) throws Exception
{
byte inputBytes[] = IOUtils.toByteArray(inputStream);
PDDocument pdDocument = PDDocument.load(new ByteArrayInputStream(inputBytes));
PDJpeg ximage = new PDJpeg(pdDocument, ImageIO.read(logoStream));
PDPage page = (PDPage) pdDocument.getDocumentCatalog().getAllPages().get(0);
PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page, true, true);
contentStream.drawXObject(ximage, 50, 50, 356, 40);
contentStream.close();
ByteArrayOutputStream os = new ByteArrayOutputStream();
pdDocument.save(os);
os.flush();
pdDocument.close();
inputBytes = os.toByteArray();
pdDocument = PDDocument.load(new ByteArrayInputStream(inputBytes));
pdDocument.getDocument().getTrailer().removeItem(COSName.TYPE);
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
signature.setName("signer name");
signature.setLocation("signer location");
signature.setReason("reason for signature");
signature.setSignDate(Calendar.getInstance());
pdDocument.addSignature(signature, new TC3());
ByteArrayInputStream fis = new ByteArrayInputStream(inputBytes);
FileOutputStream fos = new FileOutputStream(outputDocument);
byte[] buffer = new byte[8 * 1024];
int c;
while ((c = fis.read(buffer)) != -1)
{
fos.write(buffer, 0, c);
}
fis.close();
FileInputStream is = new FileInputStream(outputDocument);
pdDocument.saveIncremental(is, fos);
pdDocument.close();
}
示例15: doSignTwoRevisions
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
/**
* {@link #doSignOriginal(InputStream, InputStream, File)} changed to also save the first change as new revision.
*/
public void doSignTwoRevisions(InputStream inputStream, InputStream logoStream, File intermediateDocument, File outputDocument) throws Exception
{
FileOutputStream fos = new FileOutputStream(intermediateDocument);
FileInputStream fis = new FileInputStream(intermediateDocument);
byte inputBytes[] = IOUtils.toByteArray(inputStream);
PDDocument pdDocument = PDDocument.load(new ByteArrayInputStream(inputBytes));
PDJpeg ximage = new PDJpeg(pdDocument, ImageIO.read(logoStream));
PDPage page = (PDPage) pdDocument.getDocumentCatalog().getAllPages().get(0);
PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page, true, true);
contentStream.drawXObject(ximage, 50, 50, 356, 40);
contentStream.close();
pdDocument.getDocumentCatalog().getCOSObject().setNeedToBeUpdate(true);
pdDocument.getDocumentCatalog().getPages().getCOSObject().setNeedToBeUpdate(true);
page.getCOSObject().setNeedToBeUpdate(true);
page.getResources().getCOSObject().setNeedToBeUpdate(true);
page.getResources().getCOSDictionary().getDictionaryObject(COSName.XOBJECT).setNeedToBeUpdate(true);
ximage.getCOSObject().setNeedToBeUpdate(true);
fos.write(inputBytes);
pdDocument.saveIncremental(fis, fos);
pdDocument.close();
pdDocument = PDDocument.load(intermediateDocument);
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
signature.setName("signer name");
signature.setLocation("signer location");
signature.setReason("reason for signature");
signature.setSignDate(Calendar.getInstance());
pdDocument.addSignature(signature, new TC3());
fos = new FileOutputStream(outputDocument);
fis = new FileInputStream(outputDocument);
inputBytes = IOUtils.toByteArray(new FileInputStream(intermediateDocument));
fos.write(inputBytes);
pdDocument.saveIncremental(fis, fos);
pdDocument.close();
}