0

I'm trying to fill acrofields of existing pdf via android application. when I run this code app doesn't create new file, or at least I don't see it. Where I make mistakes? I put my existing pdf in res/raw folder of the app

public class Set1 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set1);
    try {
        PdfReader reader = new PdfReader(getResources().openRawResource(R.raw.liste));
        PdfStamper stamper=new PdfStamper(reader,new FileOutputStream("\\res\\raw\\newfile.pdf");
        AcroFields acroFields=stamper.getAcroFields();
        acroFields.setField("pp","Zagreb");
        stamper.close();
                }
    catch (IOException e) {
        e.printStackTrace();
    }
    catch (DocumentException e) {
        e.printStackTrace();
    }
}

}
  • well, with an `OutputStream output=null` this obviously will crash (`NullPointerException`, I'd guess), but what do you expect. What *exactly* happens if you use a `FileOutputStream` instead? – mkl Jul 12 '17 at 04:35
  • nothing happens. it doesn't crash but nothing else whatsoever – PostarLakogSna Jul 12 '17 at 05:16
  • It probably just prints a stack trace to some output stream you aren't aware of. Your allegation that `PdfReader` and `PdfStamper` don't work can easily be contradicted by the hundreds of thousands of examples where those classes *do* work. I suggest that you change your question (starting by making the `output` different from `null`). – Bruno Lowagie Jul 12 '17 at 06:23
  • How can I define that it prints it to the res folder for example? new FileOutputStream("\\res\\newpdfname.pdf")? – PostarLakogSna Jul 12 '17 at 06:26
  • @BrunoLowagie take a look now. I edited it as different situation that I've already tried. I still can't see file. I've also tried to see if a file in raw folder (liste.pdf) is used in application and it is recognizable in code – PostarLakogSna Jul 12 '17 at 06:36
  • (1.) Are you using iTextG (the iText version created for Android) or are you using iText (the iText version for Java). You should use iTextG if you are developing for Android. (2.) Have you tried running the code on Java (not on Android). It works on Java, doesn't it? If it doesn't, tell us what is written to the stack trace. If it does, look for the difference with Android. A misunderstanding about the file system is the usual suspect. – Bruno Lowagie Jul 12 '17 at 06:44
  • this [link](https://sourceforge.net/projects/itextg/) is the itext i've downloaded and I run the code in android studio. – PostarLakogSna Jul 12 '17 at 06:55
  • I am starting being pretty desperate. I've also entered to AndroidManifest.xml. In stacktrace there are only permissions I've mentioned. Once more, when I run app, nothing happens. I've also tried with pdfwriter only to create file but with no success- @BrunoLowagie – PostarLakogSna Jul 12 '17 at 20:34
  • @PostarLakogSna Are you sure it is appropriate to use backslashes as path separators? Android is Linux based, so you should replace "\\res\\raw\\newfile.pdf" by "/res/raw/newfile.pdf". (I'm not really into Android development, so there might be other issues in you code I do not recognize...) – mkl Jul 13 '17 at 14:30

0 Answers0