简体   繁体   中英

how to set attributes for existing pdf that contains only images using java itext?

I would like to set attributes to pdf before uploading it into a server.

Document document = new Document();
try
{
    OutputStream file = new FileOutputStream({Localpath});
    PdfWriter.getInstance(document, file);
    document.open();

    //Set attributes here
    document.addTitle("TITLE");


    document.close();
    file.close();           
} catch (Exception e)
{
    e.printStackTrace();
} 

But its not working. The file is getting corrupted

In a comment to another answer the OP clarified:

I want to set attributes to an existing pdf(not to create new pdf)

Obviously, though, his code creates a new document from scratch (as is obvious from the fact that a mere FileOutputStream is used to access the file, no reading, only writing).

To manipulate an existing PDF, one has to use a PdfReader / PdfWriter couple. Bruno Lowagie provided an example for that in his answer to the stack overflow question "iText setting Creation Date & Modified Date in sandbox.stamper.SuperImpose.java" :

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    Map info = reader.getInfo();
    info.put("Title", "New title");
    info.put("CreationDate", new PdfDate().toString());
    stamper.setMoreInfo(info);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmpWriter xmp = new XmpWriter(baos, info);
    xmp.close();
    stamper.setXmpMetadata(baos.toByteArray());
    stamper.close();
    reader.close();
}

( ChangeMetadata.java )

As you see the code sets the metadata both in the ol'fashioned PDF info dictionary ( stamper.setMoreInfo ) and in the XMP metadata ( stamper.setXmpMetadata ).

Obviously src and dest should not be the same here.

Without a second file

In yet another comment the OP clarified that he had already tried a similar solution but that he wants to prevent the

Temporary existence of second file

This can easily be prevented by first reading the original PDF into a byte[] and then stamping to it as the target file. Eg if File singleFile references the original file which is also to be the target file, you can implement:

byte[] original = Files.readAllBytes(singleFile.toPath());

PdfReader reader = new PdfReader(original);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(singleFile));
Map<String, String> info = reader.getInfo();
info.put("Title", "New title");
info.put("CreationDate", new PdfDate().toString());
stamper.setMoreInfo(info);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XmpWriter xmp = new XmpWriter(baos, info);
xmp.close();
stamper.setXmpMetadata(baos.toByteArray());
stamper.close();
reader.close();

( UpdateMetaData test testChangeTitleWithoutTempFile )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM