/*
* $Id: Addripversion.java 55 2007-05-22 22:01:53Z chammer $
* $Name: $
*
* Copyright 2005 by Carsten Hammer.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Does not work for all printers. Do not try to spool-in printing from within Acrobat
* even with "postscript passthrough" enabled it might not survive spool file creation. Instead
* try to directly print to printer via lpr or socket printing.
*
* To view the Rip version using Ghostscript use
* gswin32.exe -dDOPS outputfile.pdf
* or use -dDOPS as ghostscript option in your ghostview
*/
package com.lowagie.tools.plugins;
import java.io.*;
import javax.swing.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.lowagie.tools.arguments.*;
import com.lowagie.tools.AbstractTool;
import com.lowagie.tools.arguments.filters.PdfFilter;
/**
* This tool lets you add a embedded postscript sequence to all pages of a document.
*/
public class Addripversion
extends AbstractTool {
static {
addVersion("$Id: Addripversion.java 55 2007-05-22 22:01:53Z chammer $");
}
FileArgument destfile = null;
static String psscript = "/Helvetica findfont 15 scalefont setfont\n" +
"10 10 moveto\n" +
"product show\n" +
"( ) show\n" +
"version show\n" +
"mark\n" +
"(\n)\n" +
"revision 10 mod\n" +
"revision 100 mod 10 idiv (.)\n" +
"revision 100 idiv\n" +
"(Revision: )\n" +
"(\n)\n" +
"counttomark\n" +
"{ 17 string cvs show\n" +
"} repeat pop\n" +
"";
/**
* This tool lets you add a embedded postscript sequence to all pages of a document.
*/
public Addripversion() {
super();
FileArgument inputfile = new FileArgument(this, "srcfile",
"The file you want to add the RIP Version", false,
new PdfFilter());
arguments.add(inputfile);
destfile = new FileArgument(this, "destfile",
"The file to which the Addripversion PDF has to be written",
true, new PdfFilter());
arguments.add(destfile);
inputfile.addPropertyChangeListener(destfile);
}
/**
* Creates the internal frame.
*
*/
protected void createFrame() {
internalFrame = new JInternalFrame("Addripversion", true, true, true);
internalFrame.setSize(300, 80);
internalFrame.setJMenuBar(getMenubar());
System.out.println("=== Addripversion OPENED ===");
}
/**
* Executes the tool (in most cases this generates a PDF file).
*
*/
public void execute() {
try {
if (getValue("srcfile") == null) {
throw new InstantiationException(
"You need to choose a sourcefile");
}
if (getValue("destfile") == null) {
throw new InstantiationException(
"You need to choose a destination file");
}
PdfReader reader = new PdfReader( ( (File) getValue("srcfile"))
.getAbsolutePath());
int pagecount = reader.getNumberOfPages();
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
(File) getValue("destfile")));
for (int i = 1; i <= pagecount; i++) {
PdfContentByte seitex = stamp.getOverContent(i);
PdfPSXObject postscriptidentifyrip = new PdfPSXObject(stamp.getWriter());
postscriptidentifyrip.setLiteral(psscript);
seitex.addPSXObject(postscriptidentifyrip);
}
stamp.close();
}
catch (Exception e) {
JOptionPane.showMessageDialog(internalFrame, e.getMessage(), e
.getClass().getName(),
JOptionPane.ERROR_MESSAGE);
System.err.println(e.getMessage());
}
}
/**
* Gets the PDF file that should be generated (or null if the output isn't a
* PDF file).
*
* @return the PDF file that should be generated
* @throws InstantiationException
*/
protected File getDestPathPDF() throws InstantiationException {
return (File) getValue("destfile");
}
/**
* Indicates that the value of an argument has changed.
*
* @param arg
* the argument that has changed
*/
public void valueHasChanged(StringArgument arg) {
if (internalFrame == null) {
// if the internal frame is null, the tool was called from the
// commandline
return;
}
if (destfile.getValue() == null && arg.getName().equalsIgnoreCase("srcfile")) {
String filename = arg.getValue();
String filenameout = filename.substring(0, filename.indexOf(".",
filename.length() - 4)) + "_out.pdf";
destfile.setValue(filenameout);
}
}
/**
* This methods helps you running this tool as a standalone application.
* @param args the srcfile and destfile
*/
public static void main(String[] args) {
Addripversion addripversion = new Addripversion();
if (args.length != 2) {
System.err.println(addripversion.getUsage());
}
addripversion.setMainArguments(args);
addripversion.execute();
}
}