0

I have a String that i want to attach either to the pdf raw code somehow or, what i'm trying here is adding the string as a text file to the pdf, this is what i've come up with but it does not work.

Document document = new Document();

String directoryPath = "/storage/emulated/0/Download";

PdfWriter.getInstance(document, new FileOutputStream(directoryPath+"/Aa.pdf"));
String data = PaintView.full_string_return();
System.out.println(data);
PdfReader reader = new PdfReader(directoryPath+"/Aa.pdf");
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(directoryPath+"/Aa.pdf") );
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(stamper.getWriter(), null, String.format("data.txt",data),String.format("data",data).getBytes(StandardCharsets.UTF_8));
stamper.addFileAttachment(String.format("data",data),fs);
stamper.close();

I know i'm not using the pdfWriter here but i use it elsewhere, just added it in case it is messing up something.

  • Maybe i didn't explain myself too well, also because i'm new to pdf manipulation but i don't want the string Data to be visible on the pdf per say but i want to attach it to pdf and i guess the easiest way to do so is by using a textfile. I will check the link you sent, thanks in advance! – Pedro Carvalho Nov 17 '21 at 23:18
  • Exactly. I couldn't find much about it online, but i was trying to use this but just adding one text file https://stackoverflow.com/questions/27823189/add-multiple-attachments-in-a-pdf-using-itext-pdf-stamper (Bruno Lowagie's answer). – Pedro Carvalho Nov 17 '21 at 23:25
  • You are aware that by calling `new FileOutputStream(directoryPath+"/Aa.pdf")` you truncate the existing file at its start? Thus, the following `new PdfReader(directoryPath+"/Aa.pdf")` won't find a PDF there at all... or do you by *"but i use it elsewhere"* mean that before constructing the reader you fill and close `document`? Probably you should explain what you mean by *"it does not work"*. – mkl Nov 21 '21 at 09:36

0 Answers0