0% found this document useful (0 votes)
7 views

New Text Document

code of java

Uploaded by

cdtmyf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

New Text Document

code of java

Uploaded by

cdtmyf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

import com.itextpdf.kernel.pdf.

PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

import java.util.Scanner;

public class UserInputToPdf {


public static void main(String[] args) {
// Scanner to take input from the user
Scanner scanner = new Scanner(System.in);

// Get user input for the note


System.out.println("Enter the title of the note:");
String title = scanner.nextLine();

System.out.println("Enter the content of the note:");


String content = scanner.nextLine();

// Set up PDF file path


String pdfFilePath = "UserNote.pdf";

try {
// Creating PdfWriter object that writes to the output file
PdfWriter writer = new PdfWriter(pdfFilePath);

// Creating PdfDocument object which represents the PDF


document
PdfDocument pdf = new PdfDocument(writer);

// Creating Document object which allows adding content to


the PDF
Document document = new Document(pdf);

// Add title and content to the document


document.add(new Paragraph("Note Title: " +
title).setBold());
document.add(new Paragraph("Content:"));
document.add(new Paragraph(content));

// Close the document to save it


document.close();

System.out.println("PDF generated successfully at: " +


pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
}

You might also like