PDFBox zum pdf bearbeiten ignoriere hier alle Exceptions abgeändert aus zwei anderen Beispielen aus dem SVN

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.examples.pdmodel;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;

public class AddMessageToEachPage {

    public void doIt(String file, String header, String outfile)
	    throws IOException, COSVisitorException {

	PDDocument doc = PDDocument.load(file);
	List<?> allPages = doc.getDocumentCatalog().getAllPages();
	PDFont font = PDType1Font.HELVETICA_BOLD;
	float fontSize = 12.0f;

	//

	// doc.addPage(page);
	//
	PDPage pagex = new PDPage(PDPage.PAGE_SIZE_A4);

	BufferedImage image = new BufferedImage((int) pagex.getMediaBox()
		.getWidth(), 100, BufferedImage.TYPE_INT_RGB);
	Graphics2D g = image.createGraphics();
	g.setColor(Color.red);
	g.setBackground(Color.white);
	g.setFont(g.getFont().deriveFont(50f));
	g.clearRect(0, 0, image.getWidth(), image.getHeight());
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
		RenderingHints.VALUE_ANTIALIAS_OFF);

	g.drawString("Hallo PDFBox", 100, 100);
	g.dispose();

	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	ImageIO.write(image, "jpeg", bos);

	PDJpeg ximage = new PDJpeg(doc, new ByteArrayInputStream(
		bos.toByteArray()));

	PDPageContentStream contentStream1 = new PDPageContentStream(doc, pagex);

	contentStream1.beginText();
	contentStream1.setFont(font, 12);
	contentStream1.moveTextPositionByAmount(100, 700);
	contentStream1.drawString("Hello World !!!");
	contentStream1.endText();

	contentStream1.drawImage(ximage, 20, 20);
	contentStream1.close();

	// zur richtigen ausgabe (reihenfolge)
	PDDocument dx = new PDDocument();
	dx.addPage(pagex);

	for (int i = 0; i < allPages.size(); i++) {
	    PDPage page = (PDPage) allPages.get(i);
	    PDRectangle pageSize = page.findMediaBox();
	    PDRectangle x = new PDRectangle(pageSize.getWidth(),
		    pageSize.getHeight() + fontSize);
	    // hier wird die seite vergrößert um nichts zu übermalen
	    page.setMediaBox(x);

	    float stringWidth = font.getStringWidth(header) * fontSize / 1000f;
	    // calculate to center of the page
	    int rotation = page.findRotation();
	    boolean rotate = rotation == 90 || rotation == 270;
	    float pageWidth = rotate ? pageSize.getHeight() : pageSize
		    .getWidth();
	    float pageHeight = rotate ? pageSize.getWidth() : pageSize
		    .getHeight();
	    double centeredXPosition = rotate ? pageHeight / 2f
		    : (pageWidth - stringWidth) / 2f;
	    double centeredYPosition = rotate ? (pageWidth - stringWidth) / 2f
		    : pageHeight / 2f;
	    // append the content to the existing stream
	    PDPageContentStream contentStream = new PDPageContentStream(doc,
		    page, true, true, true);
	    contentStream.beginText();
	    // set font and font size
	    contentStream.setFont(font, fontSize);
	    // set text color to red
	    contentStream.setNonStrokingColor(255, 0, 0);
	    if (rotate) {
		// rotate the text according to the page rotation
		contentStream.setTextRotation(Math.PI / 2, centeredXPosition,
			centeredYPosition);
	    } else {
		contentStream.setTextTranslation(0, pageHeight);
	    }
	    contentStream.drawString(header);
	    contentStream.endText();
	    contentStream.close();

	    // import nicht addpage
	    // da die sachen aus einem anderem PDF stammen
	    dx.importPage(page);
	}

	// doc.getDocumentCatalog().getAllPages().add(0, pagex);
	dx.save(outfile);
	dx.close();
	doc.close();
    }

    public static void main(String[] args) {
	AddMessageToEachPage app = new AddMessageToEachPage();
	try {
	    app.doIt("store.pdf", "Was ein cooler Header", "m2e.pdf");
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

}


Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-1) was last changed on 04-Jun-2012 22:51 by JensKapitza