Skip to content

Commit 451169a

Browse files
authored
Create PDFExporter.java
1 parent 9b2dcf4 commit 451169a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/utils/PDFExporter.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package utils;
2+
3+
import org.apache.pdfbox.pdmodel.PDDocument;
4+
import org.apache.pdfbox.pdmodel.PDPage;
5+
import org.apache.pdfbox.pdmodel.PDPageContentStream;
6+
import org.apache.pdfbox.pdmodel.font.PDType1Font;
7+
8+
public class PDFExporter implements Runnable
9+
{
10+
private static final String FILEPATH = "/Downloads/chat.pdf";
11+
12+
private String chatText;
13+
14+
public PDFExporter(String chatText) {
15+
this.chatText = chatText;
16+
}
17+
18+
@Override
19+
public void run()
20+
{
21+
try
22+
{
23+
PDDocument doc = new PDDocument();
24+
PDPage myPage = new PDPage();
25+
doc.addPage(myPage);
26+
27+
PDPageContentStream content = new PDPageContentStream(doc, myPage);
28+
29+
content.beginText();
30+
31+
content.setFont(PDType1Font.TIMES_ROMAN, 12);
32+
content.setLeading(14.5f);
33+
content.newLineAtOffset(25, 700);
34+
35+
String[] splitText = chatText.split("\n");
36+
37+
for (int i = 0; i < splitText.length; i++)
38+
{
39+
content.showText( splitText[i] );
40+
content.newLine();
41+
}
42+
43+
content.endText();
44+
content.close();
45+
46+
doc.save(System.getProperty("user.home") + FILEPATH);
47+
doc.close();
48+
}
49+
catch (Exception e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)