I wrote some code to put stamps on all pages of a PDF file with the name and company passed as parameters. It works as intended, but as I am a beginner in Java, I am sure there are some best practices that I am not following.
I'm using the iText library.
Main.java:
public class Main {
public static void main(String[] args) throws IOException, DocumentException {
Helper.sortposition(args[0], args[1], args[2], args[3], args[4]);
}
}
Helper.java:
public class Helper {
public static void sortposition(String name, String company, String originalpdf, String newpdf, String report) throws IOException {
Properties prop = new Properties();
InputStream in = null;
String hxStr, hyStr, fxStr, fyStr, fhStr, ffStr;
Float hx, hy, fx, fy, fh, ff;
/* hx= Header "x" position
hy= Header "y" position
fx= Footer "x" position
fy= Footer "y" position
fh= Font size on the Header (Font Header)
ff= Font size on the Footer (Font Footer) */
String colour;
try {
in = Helper.class.getResourceAsStream("files/config.properties");
prop.load(in);
switch (report) {
case "1":
fhStr = prop.getProperty("fh1");
ffStr = prop.getProperty("ff1");
colour = prop.getProperty("type1rgb");
hxStr = prop.getProperty("type1hx");
hyStr = prop.getProperty("type1hy");
fxStr = prop.getProperty("type1fx");
fyStr = prop.getProperty("type1fy");
fh = Float.parseFloat(fhStr);
ff = Float.parseFloat(ffStr);
hx = Float.parseFloat(hxStr);
hy = Float.parseFloat(hyStr);
fx = Float.parseFloat(fxStr);
fy = Float.parseFloat(fyStr);
break;
case "2":
fhStr = prop.getProperty("fh2");
ffStr = prop.getProperty("ff2");
colour = prop.getProperty("type2rgb");
hxStr = prop.getProperty("type2hx");
hyStr = prop.getProperty("type2hy");
fxStr = prop.getProperty("type2fx");
fyStr = prop.getProperty("type2fy");
fh = Float.parseFloat(fhStr);
ff = Float.parseFloat(ffStr);
hx = Float.parseFloat(hxStr);
hy = Float.parseFloat(hyStr);
fx = Float.parseFloat(fxStr);
fy = Float.parseFloat(fyStr);
break;
case "3":
fhStr = prop.getProperty("fh3");
ffStr = prop.getProperty("ff3");
colour = prop.getProperty("type3rgb");
hxStr = prop.getProperty("type3hx");
hyStr = prop.getProperty("type3hy");
fxStr = prop.getProperty("type3fx");
fyStr = prop.getProperty("type3fy");
fh = Float.parseFloat(fhStr);
ff = Float.parseFloat(ffStr);
hx = Float.parseFloat(hxStr);
hy = Float.parseFloat(hyStr);
fx = Float.parseFloat(fxStr);
fy = Float.parseFloat(fyStr);
break;
case "4":
fhStr = prop.getProperty("fh4");
ffStr = prop.getProperty("ff4");
colour = prop.getProperty("type4rgb");
hxStr = prop.getProperty("type4hx");
hyStr = prop.getProperty("type4hy");
fxStr = prop.getProperty("type4fx");
fyStr = prop.getProperty("type4fy");
fh = Float.parseFloat(fhStr);
ff = Float.parseFloat(ffStr);
hx = Float.parseFloat(hxStr);
hy = Float.parseFloat(hyStr);
fx = Float.parseFloat(fxStr);
fy = Float.parseFloat(fyStr);
break;
default:
fhStr = prop.getProperty("fh5");
ffStr = prop.getProperty("ff5");
colour = prop.getProperty("type5rgb");
hxStr = prop.getProperty("type5hx");
hyStr = prop.getProperty("type5hy");
fxStr = prop.getProperty("type5fx");
fyStr = prop.getProperty("type5fy");
fh = Float.parseFloat(fhStr);
ff = Float.parseFloat(ffStr);
hx = Float.parseFloat(hxStr);
hy = Float.parseFloat(hyStr);
fx = Float.parseFloat(fxStr);
fy = Float.parseFloat(fyStr);
}
String delims = "[,]+";
String[] colourstr = colour.split(delims);
int[] rgb = new int[colourstr.length];
for (int i = 0; i < colourstr.length; ) {
rgb[i] = Integer.parseInt(colourstr[i]);
i++;
Stamper.putStamp(name, company, originalpdf, newpdf, rgb, hx, hy, fx, fy, fh, ff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Stamper.java:
public class Stamper {
public static void putStamp(String name, String company, String originalpdf, String newpdf, int[] rgb, Float hx, Float hy, Float fx, Float fy, Float fh, Float ff) {
PdfReader pdfReader;
PdfStamper pdfStamper = null;
Properties prop = new Properties();
InputStream in = null;
try {
in = Helper.class.getResourceAsStream("files/config.properties");
prop.load(in);
pdfReader = new PdfReader(originalpdf);
pdfStamper = new PdfStamper(pdfReader,
new FileOutputStream(newpdf));
Font font = FontFactory.getFont("files/LTe50327.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.BOLD);
BaseFont bf = font.getBaseFont();
int pages = pdfReader.getNumberOfPages();
for (int i = 1; i <= pages; i++) {
PdfContentByte pageContentByte = pdfStamper.getOverContent(i);
pageContentByte.beginText();
pageContentByte.setFontAndSize(bf, fh);
pageContentByte.setRGBColorFill(rgb[0], rgb[1], rgb[2]);
pageContentByte.setTextMatrix(hx, hy);
pageContentByte.showText("This report belongs to " + name
+ " from " + company);
pageContentByte.endText();
}
for (int i = 1; i <= pages; i++) {
PdfContentByte pageContentByte = pdfStamper.getOverContent(i);
pageContentByte.beginText();
pageContentByte.setFontAndSize(bf, ff);
pageContentByte.setRGBColorFill(rgb[0], rgb[1], rgb[2]);
pageContentByte.setTextMatrix(fx, fy);
pageContentByte.showText("This report belongs to " + name
+ " from " + company);
pageContentByte.endText();
}
} catch (IOException e) {
System.out.println("File does not exist");
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
try {
if (pdfStamper != null) {
pdfStamper.close();
}
if (in != null) {
in.close();
}
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
}
}
}