I generated a pdf, but when saving it's updating the same pdf instead of the new one

I want to create a new document. Each time current behavior updates old document rather than creating a new doc.

This is my code:

public void createPDF() throws FileNotFoundException, DocumentException {
    //create a new document file
    Document doc = new Document(PageSize.A4,36,36,40,40);
    try {

        Log.e("PDFCreator", "PDF Path: " + path);
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
        file = new File(dir, "Trinity PDF" + sdf.format(Calendar.getInstance().getTime()) + ".pdf");
        FileOutputStream fOut = new FileOutputStream(file);
        PdfWriter writer = PdfWriter.getInstance(doc, fOut);
        writer.setPageEvent(new Watermark());
        //opening the document using doc
        doc.open();
    }
}

Upvotes: 0

Views: 66

Answers (1)

SpiritCrusher
SpiritCrusher

Reputation: 21053

Thats probably because you are using same FILE name Each time .

SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy")

this will give you same String for a day. So you just need to get a unique name each time. If you want to show date in file name then you can use then you can use this format.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

This will give a very big name You can make it as per your need. main thing is its should be unique each time.

Other then that you can also use java.util.UUID or simply System.currentTimeMillis()

Upvotes: 2

Related Questions