1

I am using ITextPDF to create a PDF.The PDF is created in android. I would NOT like to save the pdf to the local storage of the android device, instead I would like to save it to a variable, like File file; or something similar, or directly convert it to a string and then save that value to a database.

This is where I declare the File variable(this can change)

File file;

This is the button that executes the pdf creation.

PDFTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            PDF();
        }
    });

This is the method to create the PDF

public void PDF()
    {
        try {
            Image img = Image.getInstance(IMAGES[0]);
            Document document = new Document(img);
            PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();
            document.add(new Paragraph("Example"));
            document.close();
        }catch (DocumentException e)
        {
            e.printStackTrace();
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
    }

The FileOutputStream requires a location which I just made file. If the file is correct, how can I display the values from that file object.

L.Lieb
  • 21
  • 2
  • 5

1 Answers1

1

In the iText 7 API documentation of PdfWriter, you can read that the constructor takes an OutputStream. This can be any OutputStream, like a FileOutputStream or a ByteArrayOutputStream.

See also the duplicate question How to create pdf files in memory.

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101