Jj Calaguian Jj Calaguian - 5 days ago 5
Java Question

Is it possible to add a constructor to a PDF generator instead of manually typing down the string?

Hi guys so i'm currently trying out the PDF generator for Java and was wondering if it was possible to add a constructor from a different class instead of manually typing down the string.
For example instead of paragraph.add("Your String Here");
Will it be possible to type down paragraph.add(account.welcome());
I tried doing this but it returned an error.

The PDF generator code that i'm using right now is:

package demo;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.*;

public class PDFGenerateDemo {

public static void main(String[] args)
throws Exception {

Document document = new Document();
PdfWriter.getInstance(document,
new FileOutputStream("/Users/PandaFunk/Desktop/images/sample.pdf"));

document.open();
Paragraph paragraph = new Paragraph();

paragraph.add("Hello World\n");
paragraph.add("PDF sample generation demo\n");
paragraph.add("programmed by Mon Zalameda\n");
paragraph.add("DLS-CSB is a wonderful school. "
+ "I will miss you and hope "
+ "to see you again.\n\n");

//adding Image
Image csbImage =
Image.getInstance("/Users/PandaFunk/Desktop/images/DLS-CSB_Seal.png");

document.add(paragraph);
document.add(csbImage);
document.close();
}
}


UPDATE: Here's the class i'm trying to use

package model;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class SECB extends BankBSP implements
PSERequirement{

public String name = "Security Bank";

public void welcome() {
System.out.println("Maligayang pagdating sa " + name);

}

public void depositAmountRequirement() {
System.out.println("To open an account, "
+ "minimum deposit amount is P3,000.00");
}

public void depositPromo() {
System.out.println("Minimum opening amount of at least "
+ "Php500,000.00 will get you a dinner date with "
+ "2014 Ms. World Megan Young.");
}

public void maintainingBalance() {
System.out.println("Maintaining balance of "
+ "at least Php1,000.00 is required");
}

public void interestRate() {
System.out.println("Minimum deposit amount of P3,000.00 "
+ "to earn an interest rate of 5% per annum.");
}

public void displayFinancialStatement() {
System.out.println("PSE Requirement: "
+ MIN_LARGE_CAPITALIZATION);
System.out.println(name + " 2015 "
+ "Financial Assets: Php141,740,664,644.70");
}

public void displayTotalMarketShares() {
System.out.println("PSE Required Shares: "
+ MIN_SHARES_SOLD);
System.out.println(name + "total shares: " +
"753,538,887");
}


public void GeneratePDF() throws DocumentException, MalformedURLException, IOException{

Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream("/Users/PandaFunk/Desktop/images/sample.pdf"));

document.open();
Paragraph paragraph = new Paragraph();
paragraph.add(welcome.toString());
paragraph.add("Hello World\n");
paragraph.add("PDF sample generation demo\n");
paragraph.add("programmed by Mon Zalameda\n");
paragraph.add("DLS-CSB is a wonderful school. "
+ "I will miss you and hope "
+ "to see you again.\n\n");

//adding Image
Image csbImage = Image.getInstance("/Users/PandaFunk/Desktop/images/DLS-CSB_Seal.png");

document.add(paragraph);
document.add(csbImage);
document.close();
}
}

Answer

Yes. It's possible. The following will work for sure:

paragraph.add(account.welcome());

In case welcome is public String welcome().

You have to pass a String to the constructor. It doesn't matter what String it is. If you have an Object which has a method which returns a String, you can pass the result of that method which is done as shown above (toString returns String).

The method also must be public or package public if you're in the same package.

So summarizing: In Java, you can pass as parameters not only copy of reference to an object identified by a alphanumeric token, but also to an object which returned by a method. You only need to remember that types must match i.e. you have to pass the object of the same class or of the class below in extending tree.