Create PDF in android with read-only permission

397 Views Asked by At

In my android application if i click on the "Generate PDF" button then pdf is created in the sd card .If i open this pdf , i can copy text but i dont won't this . Mean i want Read-Only with that generated pdf file.

Following is the code ,

public void createPDF()
{
    Document doc = new Document();


     try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";

            File dir = new File(path);
                if(!dir.exists())
                    dir.mkdirs();

            Log.d("PDFCreator", "PDF Path: " + path);
            System.out.println("PDF Path:    "+ path);

            File file = new File(dir, "sample.pdf");
            FileOutputStream fOut = new FileOutputStream(file);

            PdfWriter.getInstance(doc, fOut);

            //open the document
            doc.open();


            Paragraph p1 = new Paragraph("Hi! I am generating my first PDF using DroidText");
            Font paraFont= new Font(Font.COURIER);
            p1.setAlignment(Paragraph.ALIGN_CENTER);
            p1.setFont(paraFont);

             //add paragraph to document    
             doc.add(p1);


     } catch (DocumentException de) {
             Log.e("PDFCreator", "DocumentException:" + de);
     } catch (IOException e) {
             Log.e("PDFCreator", "ioException:" + e);
     } 
     finally
     {
             doc.close();
     }

}    
1

There are 1 best solutions below

0
On

You cannot prevent data stored on the external storage from being changed by the user, provided they find an application willing to aid in this - readily available file manager apps for example.

As long as the security model remains intact, data stored in the private storage area of an application can protected either from writing, or from both writing and reading by other applications.

Some applications however are reluctant to attempt to access a file pointed to by an Intent if it is located in a different app's private storage area, even if the file and directory permission bits have been set such that the Android system would allow them to. (For example, at least some versions of GMail refuse to attach a file that is not on the external storage, though they can be tricked into doing so provided the file is world readable.)

So save file into internal storage and open file bye the given code

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkToPDF);
setContentView(mWebView);