Skip to content

Commit 85cb624

Browse files
committed
itext create pdf
itext create pdf
1 parent b945e99 commit 85cb624

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

itext-create-pdf/.classpath

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
4+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
5+
<classpathentry kind="output" path="target/classes"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
7+
<classpathentry kind="var" path="M2_REPO/com/itextpdf/itextpdf/5.4.2/itextpdf-5.4.2.jar"/>
8+
</classpath>

itext-create-pdf/helloworld.pdf

938 Bytes
Binary file not shown.

itext-create-pdf/pagesettings.pdf

937 Bytes
Binary file not shown.

itext-create-pdf/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.hmkcode</groupId>
6+
<artifactId>itext-create-pdf</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>itext-create-pdf</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.itextpdf</groupId>
20+
<artifactId>itextpdf</artifactId>
21+
<version>5.4.2</version>
22+
</dependency>
23+
</dependencies>
24+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.hmkcode;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.FileOutputStream;
5+
6+
import com.itextpdf.text.Document;
7+
import com.itextpdf.text.DocumentException;
8+
import com.itextpdf.text.Paragraph;
9+
import com.itextpdf.text.pdf.PdfWriter;
10+
11+
12+
public class HelloWorld
13+
{
14+
public static void main( String[] args )
15+
{
16+
try {
17+
18+
// 1. create the document
19+
Document document = new Document();
20+
// 2. get PdfWriter
21+
PdfWriter.getInstance(document, new FileOutputStream("helloworld.pdf"));
22+
// 3. open the document
23+
document.open();
24+
// 4. add the content
25+
document.add(new Paragraph("Hello World!"));
26+
// 5. close the document
27+
document.close();
28+
29+
System.out.println("Document created!");
30+
31+
} catch (FileNotFoundException e) {
32+
e.printStackTrace();
33+
} catch (DocumentException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.hmkcode;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.FileOutputStream;
5+
6+
import com.itextpdf.text.Document;
7+
import com.itextpdf.text.DocumentException;
8+
import com.itextpdf.text.PageSize;
9+
import com.itextpdf.text.Paragraph;
10+
import com.itextpdf.text.Rectangle;
11+
import com.itextpdf.text.pdf.PdfWriter;
12+
13+
14+
public class PageSettings
15+
{
16+
public static void main( String[] args )
17+
{
18+
try {
19+
20+
// 1. create the document page size: A4, margins: left:20 right:20 top:40 bottom:40
21+
Document document = new Document(PageSize.A4, 20f,20f,40f,40);
22+
23+
//for custom pagesize
24+
//Rectangle pagesize = new Rectangle(216f, 720f);
25+
26+
// 2. get PdfWriter
27+
PdfWriter.getInstance(document, new FileOutputStream("pagesettings.pdf"));
28+
// 3. open the document
29+
document.open();
30+
// 4. add the content
31+
document.add(new Paragraph("Hello World!"));
32+
// 5. close the document
33+
document.close();
34+
35+
System.out.println("Document created!");
36+
37+
} catch (FileNotFoundException e) {
38+
e.printStackTrace();
39+
} catch (DocumentException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)