I am adding text to an existing pdf.
My code so far will add the text to the file but it deletes the original content that was on the pdf before, does anyone know how to fix this? So that the added text is on a new page and the original content of the pdf is on another page.
String field1 = ("/Users/Desktop/") + selectedFile.getName();
System.out.println(field1);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(field1));
PdfPage page = pdfDoc.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(1,1, 600, 843);
pdfCanvas.rectangle(rectangle);
pdfCanvas.stroke();
Canvas canvas = new Canvas( pdfCanvas, pdfDoc, rectangle);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter text to add");
String addText = myObj.nextLine(); // Read user input
Paragraph p = new Paragraph(addText);
Scanner myObj1 = new Scanner(System.in); // Create a Scanner object
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
p.setFont(font);
canvas.add(p);
pdfDoc.close();
canvas.close();