java.lang.Object
jku.se.export.PdfExporter
Utility class for generating PDF documents using Apache PDFBox.
Supports adding titles, paragraphs, tables, and images, and saving the result as a file.
-
Constructor Summary
ConstructorsConstructorDescriptionInitializes a new PDF document and sets the starting Y-position. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addImage
(BufferedImage image, float x, float y, float width, float height) Insert an image into the PDF.void
addImageAndMovePosition
(BufferedImage image, float x, float width, float height, float gap) Helper method: Insert image and shift yPosition down so that the following text is underneath.void
addParagraph
(String text) Adds a simple paragraph of text to the PDF using a regular font.void
Adds a table with headers and rows to the PDF.void
Adds a title to the PDF using a bold font and larger font size.void
end()
Finalizes the current content stream (e.g., before saving or starting a new page).org.apache.pdfbox.pdmodel.PDPageContentStream
Returns the current content stream used for writing to the PDF.org.apache.pdfbox.pdmodel.PDDocument
Returns the internal PDF document.int
Returns the current vertical writing position on the page.void
saveToFile
(String baseFileName) Saves the current PDF document to a file with a timestamped name in the Downloads folder.void
setYPosition
(int yPosition) Sets the current vertical writing position.void
Starts a new page in the document and initializes a new content stream.
-
Constructor Details
-
PdfExporter
public PdfExporter()Initializes a new PDF document and sets the starting Y-position.
-
-
Method Details
-
getDocument
public org.apache.pdfbox.pdmodel.PDDocument getDocument()Returns the internal PDF document.- Returns:
- the PDDocument instance.
-
getContentStream
public org.apache.pdfbox.pdmodel.PDPageContentStream getContentStream()Returns the current content stream used for writing to the PDF.- Returns:
- the PDPageContentStream instance.
-
getYPosition
public int getYPosition()Returns the current vertical writing position on the page.- Returns:
- the Y-position.
-
setYPosition
public void setYPosition(int yPosition) Sets the current vertical writing position.- Parameters:
yPosition
- the Y-position to set.
-
startPage
Starts a new page in the document and initializes a new content stream.- Throws:
IOException
- if the page or stream cannot be created.
-
end
Finalizes the current content stream (e.g., before saving or starting a new page).- Throws:
IOException
- if closing the stream fails.
-
addTitle
Adds a title to the PDF using a bold font and larger font size.- Parameters:
title
- the text to display as the title.- Throws:
IOException
- if writing fails.
-
addParagraph
Adds a simple paragraph of text to the PDF using a regular font.- Parameters:
text
- the paragraph content.- Throws:
IOException
- if writing fails.
-
addTable
Adds a table with headers and rows to the PDF. Handles page breaks automatically if the table is too long for one page.- Parameters:
headers
- List of column headers.rows
- List of rows, each being a list of cell values.- Throws:
IOException
- if writing fails.
-
addImage
public void addImage(BufferedImage image, float x, float y, float width, float height) throws IOException Insert an image into the PDF.- Parameters:
image
- The BufferedImage, which should be insertedx
- X-position (from bottom left)y
- Y-position (from bottom left)width
- Width of the image in PDF pointsheight
- Height of the image in PDF points- Throws:
IOException
- if insertion fails
-
addImageAndMovePosition
public void addImageAndMovePosition(BufferedImage image, float x, float width, float height, float gap) throws IOException Helper method: Insert image and shift yPosition down so that the following text is underneath.- Parameters:
image
- BufferedImagex
- x-positionwidth
- Width of the imageheight
- Height of the imagegap
- Distance down to the next element- Throws:
IOException
-
saveToFile
Saves the current PDF document to a file with a timestamped name in the Downloads folder.- Parameters:
baseFileName
- Base name of the file (without extension or timestamp).- Throws:
IOException
- if saving fails.
-