How to create pdf files in memory

2019-09-11 06:24发布

问题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pdftest);
    Button Btgerar = (Button) findViewById(R.id.gerarpdf);
    Btgerar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CriarPDF();
        }
    });
}
public void CriarPDF() {
    String FILE = Environment.getExternalStorageDirectory().toString() + "/PDF/" + "Ficha.pdf";
    EditText txt = (EditText) findViewById(R.id.editText);
    // Create New Blank Document
    Document document = new Document(PageSize.A4);

    // Create Directory in External Storage
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/PDF");
    myDir.mkdirs();

    // Create Pdf Writer for Writting into New Created Document
    try {
        PdfWriter.getInstance(document, new FileOutputStream(FILE));

    // Open Document for Writting into document
        document.open();
        document.add(new Paragraph(txt.getText().toString()));


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    // Close Document after writting all content
    document.close();
    Toast.makeText(this, "PDF File is Created. Location : " + FILE,
            Toast.LENGTH_LONG).show();

    // Create new Page in PDF
    document.newPage();


}
}

I have declared in android manifest

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE

and build gradle version is :

compileSdkVersion 24
buildToolsVersion '24.0.0'
minSdkVersion 8
targetSdkVersion 24

The application can run normally, but can't create pdf file and I can't find.

回答1:

If I look at the subject of your question, then the answer is simple. You are creating a PDF on the file system using a FileOutputStream. In the subject you ask: "How to create pdf files in memory?" That is simple. iText accepts any OutputStream, so the answer is: use a ByteArrayOutputStream instead of a FileOutputStream.

See Using iText, generate on memory a PDF that is generated on disk instead

Document document = new Document();
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(documento, out);
(...)
byte[] pdf = out.getBytes();

However, why would you want to create a PDF in memory on an Android device? What are you going to do with those bytes? You can't show the PDF if you only have a byte[]. That is explained in my answer to this question: Display PDF document stored in a ByteArrayOutputStream (not in a file)

You will also notice that the following question remained unanswered (because there is no answer): How to visualize a ByteArrayOutputStream as PDF on Android?

Your question changes subject towards the end, where you write: "The application can run normally, but can't create pdf file and I can't find." This sounds as if you don't know how to write a PDF to the file system on Android. That's a different question than what you mention in the subject. That question has been answered many times before:

  • Creating PDFs in android
  • how to solve read only file system error in android programming with eclipse
  • How to save PDF file on Sdcard created using iText?
  • Not able to run simple program of iText library in android
  • Save iText created PDF to internal memory as MODE_WORLD_READABLE