HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Stamp PDF file with name and company

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
filewithcompanystampnameandpdf

Problem

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");

Solution

[I'll update my answer until it is done.]

Good start and welcome to Java programming!

General observations

You have a lot of repeating code. This is an indicator that something can be abstracted away, put in a method or other solution to prevent repeating yourself. Don't Repeat Yourself. The main reason to prevent repetition is that repetition leads to mistakes (like forgetting to edit one repitition, etc)

Main

  • You should check the arguments, and print some message when they are not ok.



  • Don't just throw error from main, catch them and handle them.



Helper

  • Give your variables sensible names; so you can read them and know what they do.



  • Use native types whenever possible, so please user float instead of Float.



  • Decide what to do if the float's don't parse.



  • You can re-write your switch loop to a method that gets the report as parameter.



  • You set a lot of variables that belong together, just to pass them around. You can put those in a class. As I do not yet get what your variables do, I'd call it StamperProperties. It would look somewhat like this:



Here:

class StamperProperties
{
    String hxStr, hyStr, fxStr, fyStr, fhStr, ffStr;
    float hx, hy, fx, fy, fh, ff;
}


I would also make this StamperProperties be able to load itself from the config.

You can then pass an instance of these properties to your stamper, which will be:

StamperProperties stamperProperties = new StamperProperties();
stamperProperties.loadFromConfig(...)

Stamper.putStamp(name, company, originalpdf, newpdf, rgb, stamperProperties)


Stamper

To prevent repetition, you can use methods. For example:

for (int i = 1; i <= pages; i++) {
    PdfContentByte pageContentByte = pdfStamper.getOverContent(i);
    String waterMark = "This report belongs to " + name
            + " from " + company;
    putWaterMark(waterMark, hx, hy);
    putWaterMark(waterMark, fx, fy);

}

public void putWaterMark(PdfContentByte pageContentByte, String text, float x, float y)
{
    pageContentByte.beginText();
    pageContentByte.setFontAndSize(bf, fh);
    pageContentByte.setRGBColorFill(rgb[0], rgb[1], rgb[2]);
    pageContentByte.setTextMatrix(x, y);
    pageContentByte.showText(text);
    pageContentByte.endText();

}

Code Snippets

class StamperProperties
{
    String hxStr, hyStr, fxStr, fyStr, fhStr, ffStr;
    float hx, hy, fx, fy, fh, ff;
}
StamperProperties stamperProperties = new StamperProperties();
stamperProperties.loadFromConfig(...)

Stamper.putStamp(name, company, originalpdf, newpdf, rgb, stamperProperties)
for (int i = 1; i <= pages; i++) {
    PdfContentByte pageContentByte = pdfStamper.getOverContent(i);
    String waterMark = "This report belongs to " + name
            + " from " + company;
    putWaterMark(waterMark, hx, hy);
    putWaterMark(waterMark, fx, fy);

}

public void putWaterMark(PdfContentByte pageContentByte, String text, float x, float y)
{
    pageContentByte.beginText();
    pageContentByte.setFontAndSize(bf, fh);
    pageContentByte.setRGBColorFill(rgb[0], rgb[1], rgb[2]);
    pageContentByte.setTextMatrix(x, y);
    pageContentByte.showText(text);
    pageContentByte.endText();

}

Context

StackExchange Code Review Q#104129, answer score: 3

Revisions (0)

No revisions yet.