1

I need to append a Watermark to a PDF file, but I would like to avoid creating a new file. The following code will not work unless I give the outputstream a different name. How can the syntax and/or order here be adjusted to get the result I am after?

public class AppendHistoricalWatermark {

public AppendHistoricalWatermark(SmbFile smbfile){

    System.out.println("AppendHistoricalWatermark: " + smbfile.getPath());

    try{
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(smbfile.getInputStream()), new PdfWriter(smbfile.getOutputStream()));
        Document document = new Document(pdfDoc);
        Rectangle pageSize;
        PdfCanvas canvas;
        int n = pdfDoc.getNumberOfPages();
        System.out.println("AppendHistoricalWatermark: NumberOfPages: " + n);

        for (int i = 1; i <= n; i++) {
            PdfPage page = pdfDoc.getPage(i);
            pageSize = page.getPageSize();
            canvas = new PdfCanvas(page);

            Paragraph p = new Paragraph("HISTORICAL").setFontSize(60);
            canvas.saveState();
            PdfExtGState gs1 = new PdfExtGState().setFillOpacity(0.2f);
            canvas.setExtGState(gs1);
            document.showTextAligned(p, pageSize.getWidth() / 2, pageSize.getHeight() / 2, pdfDoc.getPageNumber(page), 
                TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
            canvas.restoreState();


        }
        pdfDoc.close();



    }
    catch(Exception e){
        System.out.println("AppendHistoricalWatermark: " + e);
        e.printStackTrace();
    }
}

}
DwB
  • 37,124
  • 11
  • 56
  • 82
Steve Dyke
  • 155
  • 3
  • 14
  • 1
    You are using a PDF library. Consider including a statement like "I am using the PDF blammy library" – DwB Jan 09 '18 at 19:15
  • 1
    Would you consider it cheating if you: 1. rename the current file, 2. create a new file with the old name, 3. delete the old file (using its new name)? – Jongware Jan 09 '18 at 19:26
  • 1
    It doesn't matter which PDF library the OP uses. It doesn't even matter which structured file format the OP uses. It might as well have been a PNG image or a Word document. The answer will always be the same: you cannot write to the same file as you are reading from. You *must* do what usr2564301 writes. That is not cheating but The Right Way to do it. – Amedee Van Gasse Jan 09 '18 at 19:45
  • 1
    Consider this. You have a bug in your code and you end up with a corrupted PDF file. If you overwrite the original file then it will be lost forever. Do you really want to take that risk? Do you always write perfect code on the first attempt? I don't! – Amedee Van Gasse Jan 09 '18 at 19:47
  • 1
    This is what the iText documentation says: https://developers.itextpdf.com/question/how-update-pdf-without-creating-new-pdf – Amedee Van Gasse Jan 09 '18 at 19:50
  • 1
    I am also sure that this question has been asked many times before and will soon be closed as a duplicate. – Amedee Van Gasse Jan 09 '18 at 19:53
  • Done! And @DwB: most file manipulation tools (Office, image libraries,...) create temporary files. Why do you consider that being a problem? The opposite would be an example of bad design. – Bruno Lowagie Jan 09 '18 at 20:25
  • But the PDF format **does** allow for pasting data on at the end: incremental updates! The ISO specification "PDF 32000-1:2008" describes this process in **7.5.6 Incremental Updates**: "The contents of a PDF file can be updated incrementally without rewriting the entire file. When updating a PDF file incrementally, *changes shall be appended to the end of the file, leaving its original contents intact*." (my emph.). – Jongware Jan 09 '18 at 23:19
  • That's a very ambiguous interpretation of the ISO spec. Bruno can you explain to usr12345 why their interpretation is not correct? – Amedee Van Gasse Jan 09 '18 at 23:29
  • @AmedeeVanGasse: I see no double entendres in that description. Here is another: [The Incremental Update Mechanism](http://www.mactech.com/articles/mactech/Vol.15/15.09/PDFIntro/index.html) (the subheading is about halfway that page). Perhaps you are referring to a specific toolchain? Which does not support it? – Jongware Jan 10 '18 at 23:09
  • I am referring to the standard Java file I/O API that lies one level below the iText API, and which does not allow to read from and write to the same file at the same time. `FileInputStream` and `FileOutputStream` are separate classes. What the ISO spec talks about, is at a higher abstraction level, at the level of the PDF document, but that is not what the argument is about. – Amedee Van Gasse Jan 11 '18 at 00:14
  • The word "rewriting" does not refer to file I/O operations, but to semantically reconstruct the document structure. – Amedee Van Gasse Jan 11 '18 at 00:17

0 Answers0