Java:pdf //free\\ -
// Write text contents.beginText(); contents.newLineAtOffset(100, 700); contents.showText(text); contents.endText(); }
import java.io.File; import java.io.IOException;
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.23</version> </dependency> If you're using Gradle, add the following dependency to your build.gradle : java:pdf
implementation 'org.apache.pdfbox:pdfbox:2.0.23'
public class PdfGenerator {
// Save the PDF document document.save(new File(fileName)); } }
/** * Generate a PDF document with a single page containing text. * * @param fileName the output PDF file name * @param text the text to be written in the PDF * @throws IOException if an I/O error occurs */ public void generatePdf(String fileName, String text) throws IOException { // Create a new PDF document try (PDDocument document = new PDDocument()) { // Create a new page PDPage page = new PDPage(); document.addPage(page); // Write text contents
// Create a content stream and write text try (PDPageContentStream contents = new PDPageContentStream(document, page)) { // Set font and font size contents.setFont(PDType1Font.HELVETICA_BOLD, 24);