Skip to content
This repository was archived by the owner on Nov 29, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bin
classes
target
pdfstamp-*.jar
pdfstamp*.jar
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# pdfstamp
Stamp a PDF with an image and clickable URL, AND text strings.

Code assumes 72dpi if it can't find out from file, then scales to 300dpi unless otherwise specified.

This is a work-in-progress. Needs configuration for text font (currently white).

Stamp a PDF with an image and clickable URL, AND text strings.

Code assumes 72dpi if it can't find out from file, then scales to 300dpi unless otherwise specified.

This is a work-in-progress. Needs configuration for text font (currently white).

```
java -jar ./pdfstamp.jar
Usage: pdfstamp [options] <PDF-FILEs> | <DIR>
-d N : Optional. Target DPI. Defaults to 300.
-e EXT : Optional. Extension appended to the PDF filename.
-i FILE : Required. Image file containing image of the stamp.
-l X,Y : Required. Location on page to apply stamp.
-o FILE : Optional. Output directory.
-p P1,P2... : Optional. Page numbers to stamp. -1 is the last page.
-r : Optional. Descend recursively into directories.
-t X,Y,TEXT ... : Optional. Text to stamp. (Multiple values allowed.)
-u URL : Optional. Target URL of the stamp.
-v : Optional. Verbose output.
```

New -t argument:

Separate multiple values by a space, e.g. -t 1,1,Hello 2,2,World.
To use the value of the -u argument, use "=URL" as the value for TEXT.

Original project: https://github.com/CrossRef/pdfstamp

Text stamping added by http://www.appazur.com
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</target>

<target name="package" depends="build">
<jar destfile="pdfstamp-${DSTAMP}.jar">
<jar destfile="pdfstamp.jar">
<fileset dir="${classes.dir}"/>
<zipfileset src="${lib.dir}/args4j-2.0.11.jar" excludes="META-INF"/>
<zipfileset src="${lib.dir}/iText-5.0.3.jar" excludes="META-INF"/>
Expand Down
54 changes: 54 additions & 0 deletions src/com/appazur/pdfstamp/TextStampTuple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
The MIT License (MIT)

Copyright (c) 2015 Appazur Solutions Inc.

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.
*/

package com.appazur.pdfstamp;

import org.kohsuke.args4j.CmdLineException;

public class TextStampTuple {
public float x;
public float y;
public String text;

public TextStampTuple(String paramValue) throws CmdLineException {
final String[] components = paramValue.split(",");

if (components.length != 3) {
/* Each TextStampTuple should have x,y,text . */
throw new CmdLineException("Must specify X,Y,TEXT for text stamp.");
}

try {
this.x = Float.parseFloat(components[0]);
this.y = Float.parseFloat(components[1]);

// Since spaces are used to separate arguments to -t,
// need to provide an alternate way to provide spaces.
this.text = components[2].replace('_',' ');

} catch (NumberFormatException e) {
throw new CmdLineException("X and Y must be specified as rational numbers.");
}
}
}
71 changes: 69 additions & 2 deletions src/org/crossref/pdfstamp/Main.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**

Changes to the original file, https://github.com/CrossRef/pdfstamp/blob/master/src/org/crossref/pdfstamp/Main.java
are released under the the MIT License (MIT)
and are copyright (c) 2015 Appazur Solutions Inc.

*/

package org.crossref.pdfstamp;

import com.itextpdf.text.DocumentException;
Expand All @@ -16,13 +24,26 @@
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.spi.StringArrayOptionHandler;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Element;

import com.appazur.pdfstamp.TextStampTuple;



// -u "http://blah.com" -i somefile.jpeg -l 1,44.5,22.3,3,22.2,22.2 some/dir
// or some.file

Expand Down Expand Up @@ -65,7 +86,14 @@ public class Main {
@Option(name="-d", usage="Optional. Target DPI. Defaults to 300.",
required=false, multiValued=false)
private int targetDpi = 300;


// Note: separate multiple values by a space, e.g. -t 1,1,Hello 2,2,World.
// To use the value of the -u argument, use "=URL" as the value for TEXT.
@Option(name="-t", usage="Optional. Text to stamp. (Multiple values allowed.)",
handler = StringArrayOptionHandler.class,
required=true, multiValued=false, metaVar="X,Y,TEXT ...")
String[] textStampList;

@Argument
private List<String> paths = new ArrayList<String>();

Expand Down Expand Up @@ -132,7 +160,7 @@ private static PdfStamper openStamper(File f, PdfReader r)
* at the same location. The action area covers the the image.
*/
private void stampPdf(PdfStamper s, Image i, float x, float y, int page)
throws DocumentException {
throws DocumentException, CmdLineException {
/* Assume 72 DPI images if not specified. */
final float scaleFactorX = (i.getDpiX() == 0 ? 72f : i.getDpiX()) / targetDpi;
final float scaleFactorY = (i.getDpiY() == 0 ? 72f : i.getDpiY()) / targetDpi;
Expand All @@ -152,6 +180,37 @@ private void stampPdf(PdfStamper s, Image i, float x, float y, int page)
x + scaledImgWidth,
y);
}

for(String tss: this.textStampList) {
if(verbose) {
System.err.println(tss);
}
TextStampTuple tst = new TextStampTuple(tss);
ColumnText ct = new ColumnText( content );

// These are the coordinates where you want to add text.
// If the text does not fit inside it will be cropped.
ct.setSimpleColumn(tst.x, tst.y, tst.x+300, tst.y+50);
Phrase p;
// special case: if text is "=URL", use url as text.
if(tst.text.equals("=URL")) {
tst.text = url;
}

if(tst.text.startsWith("http")) {
p = new Phrase(tst.text);
}
else {
p = new Phrase(tst.text,
FontFactory.getFont("Trebuchet MS", 16,
Font.BOLD, BaseColor.WHITE));
ct.setAlignment(Element.ALIGN_RIGHT);
}

ct.setText(p);
ct.go();
}

content.restoreState();
}
}
Expand Down Expand Up @@ -196,6 +255,9 @@ private void addStampsToFile(File in, File out) {
} catch (Exception e) {
System.err.println("Failed on " + in.getPath() + " because of:");
System.err.println(e);
if(verbose) {
e.printStackTrace();
}
} finally {
try {
if (s != null) {
Expand Down Expand Up @@ -277,6 +339,11 @@ private void doMain(String... args) {
} catch (CmdLineException e) {
System.err.println(e.getMessage());
parser.printUsage(System.err);
} catch (Exception e) {
System.err.println(e.getMessage());
if(verbose) {
e.printStackTrace();
}
}
}

Expand Down