001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.examples.pdmodel;
031:
032: import java.io.IOException;
033: import java.util.List;
034:
035: import org.pdfbox.exceptions.COSVisitorException;
036:
037: import org.pdfbox.pdmodel.PDDocument;
038: import org.pdfbox.pdmodel.PDPage;
039:
040: import org.pdfbox.pdmodel.common.PDRectangle;
041: import org.pdfbox.pdmodel.edit.PDPageContentStream;
042:
043: import org.pdfbox.pdmodel.font.PDFont;
044: import org.pdfbox.pdmodel.font.PDType1Font;
045:
046: /**
047: * This is an example of how to add a message to every page
048: * in a pdf document.
049: *
050: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
051: * @version $Revision: 1.3 $
052: */
053: public class AddMessageToEachPage {
054: /**
055: * Constructor.
056: */
057: public AddMessageToEachPage() {
058: super ();
059: }
060:
061: /**
062: * create the second sample document from the PDF file format specification.
063: *
064: * @param file The file to write the PDF to.
065: * @param message The message to write in the file.
066: * @param outfile The resulting PDF.
067: *
068: * @throws IOException If there is an error writing the data.
069: * @throws COSVisitorException If there is an error writing the PDF.
070: */
071: public void doIt(String file, String message, String outfile)
072: throws IOException, COSVisitorException {
073: // the document
074: PDDocument doc = null;
075: try {
076: doc = PDDocument.load(file);
077:
078: List allPages = doc.getDocumentCatalog().getAllPages();
079: PDFont font = PDType1Font.HELVETICA_BOLD;
080: float fontSize = 12.0f;
081:
082: for (int i = 0; i < allPages.size(); i++) {
083: PDPage page = (PDPage) allPages.get(i);
084: PDRectangle pageSize = page.findMediaBox();
085: float stringWidth = font.getStringWidth(message);
086: float centeredPosition = (pageSize.getWidth() - (stringWidth * fontSize) / 1000f) / 2f;
087: PDPageContentStream contentStream = new PDPageContentStream(
088: doc, page, true, true);
089: contentStream.beginText();
090: contentStream.setFont(font, fontSize);
091: contentStream.moveTextPositionByAmount(
092: centeredPosition, 30);
093: contentStream.drawString(message);
094: contentStream.endText();
095: contentStream.close();
096: }
097:
098: doc.save(outfile);
099: } finally {
100: if (doc != null) {
101: doc.close();
102: }
103: }
104: }
105:
106: /**
107: * This will create a hello world PDF document.
108: * <br />
109: * see usage() for commandline
110: *
111: * @param args Command line arguments.
112: */
113: public static void main(String[] args) {
114: AddMessageToEachPage app = new AddMessageToEachPage();
115: try {
116: if (args.length != 3) {
117: app.usage();
118: } else {
119: app.doIt(args[0], args[1], args[2]);
120: }
121: } catch (Exception e) {
122: e.printStackTrace();
123: }
124: }
125:
126: /**
127: * This will print out a message telling how to use this example.
128: */
129: private void usage() {
130: System.err.println("usage: " + this .getClass().getName()
131: + " <input-file> <Message> <output-file>");
132: }
133: }
|