-
Notifications
You must be signed in to change notification settings - Fork 1
Apache PDFBox MVN
Document Creation using PDFBox.
Create a Blank PDF This small sample shows how to create a new PDF document using PDFBox.
// Create a new empty document
PDDocument document = new PDDocument();
// Create a new blank page and add it to the document
PDPage blankPage = new PDPage();
document.addPage( blankPage );
// Save the newly created document
document.save("BlankPage.pdf");
// finally make sure that the document is properly closed.
document.close();public class Report_PDF {
// Create a new font object selecting one of the PDF base fonts
static PDFont fontPlain = PDType1Font.HELVETICA;
static PDFont fontBold = PDType1Font.HELVETICA_BOLD;
static String saveAs = "D:/Yash/docc1.pdf";
public static void main(String[] args)throws IOException {
// Create a new empty document
PDDocument document = new PDDocument();
// Create a new blank page and add it to the document
PDPage page = new PDPage(PDRectangle.A4);
document.addPage( page );
getPageSpaces(page);
// Start a new content stream which will "hold" the to be created content
PDPageContentStream pageCS = new PDPageContentStream(document, page);
try {
// Define a text content stream using the selected font.
pageCS.beginText();
pageCS.setFont( fontBold, 12 );
//setting the coordinates from where the text will be displayed on the page (first one is the column, second is for row)
pageCS.newLineAtOffset(15, 750);
//drawPageTitle();
pageCS.showText( "Hello World" );
//contentStream.newLine(); // Must call beginText() before newLine()
pageCS.endText(); // You must call beginText() before calling endText.
} finally {
// Make sure that the content stream is closed:
pageCS.close();
}
// drawBoundingBox(document, page); // https://stackoverflow.com/a/52969119/5081877
PDPage page2 = new PDPage();
page2 = customPaperSize(page2);
document.addPage( page2 );
// Save the results and ensure that the document is properly closed:
document.save( new File( saveAs ) );
document.close();
}
public static void getPageSpaces(PDPage page) {
float x_Spaces= page.getMediaBox().getWidth();
float y_Spaces = page.getMediaBox().getHeight();
System.out.format("Total Page Cells [X:Width=%f]:[Y:Height=%f]\n", x_Spaces, y_Spaces);
}
public static PDPage customPaperSize(PDPage page) { // PDRectangle.A4
PDRectangle rectangle = new PDRectangle();
rectangle.setLowerLeftX(20);
rectangle.setLowerLeftY(20);
rectangle.setUpperRightX(615); // PAGE_WIDTH
rectangle.setUpperRightY(815); // PAGE_HEIGHT
page.setMediaBox(rectangle);
page.setCropBox(rectangle);
page.setArtBox(rectangle);
return page;
}
}Boxable, A High Level API to Creates Table On Top of Apache PdfboxMaven, github
Example: https://ulfdittmer.com/view?PdfboxTable Jars required google.guava, SLF4J API
static PDFont fontBold = PDType1Font.HELVETICA_BOLD;
public static void tableContent(PDDocument document, PDPage page) throws IOException {
float margin = 50;
// starting y position is whole page height subtracted by top and bottom margin
float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
// we want table across whole page width (subtracted by left and right margin ofcourse)
float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
boolean drawContent = true;
float yStart = yStartNewPage;
float bottomMargin = 70;
// y position is your coordinate of top left corner of the table
float yPosition = 550;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
BaseTable table = new BaseTable(yPosition, yStartNewPage,
bottomMargin, tableWidth, margin, document, page, true, drawContent);
createRow(table,"Cell Name", "Cell Value");
createRow(table,"Data",dateFormat.format(Calendar.DATE));
table.draw();
}
public static void createRow(BaseTable table, String left, String right) {
// the parameter is the row height
be.quodlibet.boxable.Row<PDPage> row = table.createRow(20);
// the first parameter is the cell width
Cell<PDPage> cell = row.createCell(50, left);
cell.setFont(fontBold);
cell.setFontSize(20);
// vertical alignment
cell.setValign(VerticalAlignment.MIDDLE);
// border style
cell.setTopBorderStyle(new LineStyle(Color.BLACK, 10));
cell = row.createCell(50, right);
cell.setFontSize(15);
cell.setFont(fontBold);
table.addHeaderRow(row);
}In a class-based object-oriented language, in general, state is carried by instances, methods are carried by classes, and inheritance is only of structure and behavior. Basic, Refactoring Techniques
Method signature: It consists of method name and parameter list (number/type/order of the parameters). methodName(parametersList y). An instance method in a subclass with the same signature and return type as an instance method in the super-class overrides the super-class's method.
Java OOP concepts
Class - Collection of a common features of a group of object [static/instance Fields, blocks and Methods]
Object - Instance of a class (instance fields)
Abstraction - Process of hiding complex info and providing required info like API, Marker Interfaces ...
Encapsulation(Security) - Class Binding up with data members(fields) and member functions.
Inheritance (Reusability by placing common code in single class)
1. Multilevel - {A -> B -> C} 2. Multiple - Diamond problem {A <- (B) -> C} [Java not supports] 3. Cyclic {A <-> B} [Java not supports]
* Is-A Relation - Class A extends B
* Hash-A Relation - Class A { B obj = new B(); } - (Composition/Aggregation)
Polymorphism (Flexibility) 1. Compile-Time Overloading 2. Runtime Overriding [Greek - "many forms"]
int[] arr = {1,2,3}; int arrLength = arr.length; // Fixed length of sequential blocks to hold same data type
String str = "Yash"; int strLength = str.length(); // Immutable Object value can't be changed.
List<?> collections = new ArrayList<String>(); int collectionGroupSize = collections.size();
Map<?, ?> mapEntry = new HashMap<String, String>();
Set<?> keySet = mapEntry.keySet(); // Set of Key's
Set<?> entrySet = mapEntry.entrySet(); // Set of Entries [Key, Value]
// Immutable Objects once created they can't be modified. final class Integer/String/Employee
Integer val = Integer.valueOf("100"); String str2 = String.valueOf(100); // Immutable classes
final class Employee { // All Wrapper classes, java.util.UUID, java.io.File ...
private final String empName; // Field as Final(values can be assigned only once) Only getter functions.
public Employee(String name) { this.empName = name; }
} Native Java Code for Hashtable.h, Hashtable.cpp
SQL API.
You can check your current JDK and JRE versions on your command prompt respectively,
- JDK
javac -version [C:\Program Files\Java\jdk1.8.0_121\bin]o/p:javac 1.8.0_121 - JRE
java -version[C:\Program Files\Java\jdk1.8.0_121\bin]o/P:java version "1.8.0_102"
JAVA_HOME - Must be set to JDK otherwise maven projects leads to compilation error. [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? C:\Softwares\OpenJDK\, 7-zip
Fatal error compiling: invalid target release: JRE and JDK must be of same version
1.8.0.XXX
Disable TLS 1.0 and 1.1
security-libs/javax.net.ssl: TLS 1.0 and 1.1 are versions of the TLS protocol that are no longer considered secure and have been superseded by more secure and modern versions (TLS 1.2 and 1.3).
Core Java
-
Java Programming Language Basics
- Object, Class, Encapsulation, Interface, Inheritance, Polymorphism (Method Overloading, Overriding)
- JVM Architecture, Memory Areas
- JVM Class Loader SubSystem
- Core Java Interview Questions & Programs
- Interview Concepts
Stack Posts
- Comparable vs Comparator
- Collections and Arrays
-
String, StringBuffer, and StringBuilder
- String reverse
- Remove single char
- File data to String
- Unicode equality check Spacing entities
- split(String regex, int limit)
- Longest String of an array
-
Object Serialization
- Interface's Serializable vs Externalizable
- Transient Keyword
-
implements Runnablevsextends Thread - JSON
- Files,
Logging API- Append text to Existing file
- Counting number of words in a file
- Properties
- Properties with reference key