I am Printing Pdf using Google Cloud Printing from my Storage of my phone. i want to use pdf URL by replacing this. How to use URL?
For Example: i want to replace /print/test.pdf" to "www.example.com/print/test.pdf"
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/print/test.pdf");
Intent printIntent = new Intent(MainActivity.this,PrintDialogActivity.class);
printIntent.setDataAndType(Uri.fromFile(file),"application/pdf");
printIntent.putExtra("title", "Android print demo");
startActivity(printIntent);
Download http://central.maven.org/maven2/commons-io/commons-io/2.4/commons-io-2.4.jar and paste it to your libs folder in Android Studio. Open your Gradle file and inside the dependencies add this "compile files('libs/commons-io-1.3.2.jar')"
URL url = null;
try {
url = new URL("http://www.cbu.edu.zm/downloads/pdf-sample.pdf");
} catch (MalformedURLException e) {
e.printStackTrace();
}
String tDir = System.getProperty("java.io.tmpdir");
String path = tDir + "tmp" + ".pdf";
File file = new File(path); file.deleteOnExit();
try {
FileUtils.copyURLToFile(url, file);
} catch (IOException e) {
e.printStackTrace();
}
Intent printIntent = new Intent(MainActivity.this,
PrintDialogActivity.class);
printIntent.setDataAndType(Uri.fromFile(file),
"application/pdf");
printIntent.putExtra("title", "Android print demo");
startActivity(printIntent);
Related
I am trying to create an PDF using android.graphics.pdf. My issue is with multiple pages. I can give android.graphics.pdf html which could be then printed to a PDF. Now that doesn't work if text overflows the set page size. Is it possible to give it all the html and it would create multiple pages according to the content with respect to the page size? As does TCPDF :)
Note. I am trying to avoid creating separate multiple pages by calculating the height of the content.
For this you'll need to add the jar of iTextG to your project:
public void createandDisplayPdf(String text) {
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "newFile.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
//open the document
doc.open();
Paragraph p1 = new Paragraph(text);
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();
}
viewPdf("newFile.pdf", "Dir");
}
// Method for opening a pdf file
private void viewPdf(String file, String directory) {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/" + directory + "/" + file);
Uri path = Uri.fromFile(pdfFile);
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(TableActivity.this, "Can't read pdf file", Toast.LENGTH_SHORT).show();
}
}
I just started using text for an app I am working on to further my android knowledge. However what I cannot figure out is how to get the pdf which was filled and then email it using the intent. I have been googling and researching everywhere but I cannot find anything. Does anyone know how to go about it?
declaration of my file before my onCreate;
File file = new File(getExternalFilesDir(null),"pvgform.pdf");
This is part of my code to add info to the fillable pdf text
OutputStream output = null;
try {
output = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
reader = new PdfReader(getResources().openRawResource(R.raw.form));
} catch (IOException e) {
e.printStackTrace();
}
try {
PdfStamper stamper = new PdfStamper(reader, output);
AcroFields acroFields = stamper.getAcroFields();
acroFields.setField("fullname", editText.getText().toString());
acroFields.setField("agedob", editText2.getText().toString());
acroFields.setField("description", editText3.getText().toString() + editText4.getText().toString() + editText5.getText().toString());
acroFields.setField("duration", editText6.getText().toString());
acroFields.setField("brandname", editText8.getText().toString());
acroFields.setField("genericname", editText9.getText().toString());
stamper.setFormFlattening(true);
stamper.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
reader.close();
and this is my attempt on the email intent for the pdf form:
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, "My form");
email.putExtra(Intent.EXTRA_TEXT, "Here is a form");
Log.d("file",file.getAbsolutePath());
Uri uri = Uri.fromFile(file);
email.putExtra(Intent.EXTRA_STREAM,uri);
email.setType("application/pdf");
email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
when I run this, there is no attachment to the email to be sent.
This is part of my code to add info to the fillable pdf text
You do not appear to be writing the PDF to a file.
and this is my attempt on the email intent for the pdf form:
Uri uri=Uri.parse("file://"+ "form.pdf");
This is not a valid Uri on any platform.
Write your PDF to a file, such as on external storage. Then, use a Uri that resolves to that file, using Uri.fromFile() to convert your File object into the appropriate Uri.
I try to open a PDF in my application.
First, I create the PDF like that :
String filename = Environment.getExternalStorageDirectory().toString()+"/mypdf.pdf";
File file = new File(filename);
try {
FileOutputStream bos = new FileOutputStream(file);
bos.write(Base64.decode(base64, 0));
bos.flush();
bos.close();
} catch (IOException e) {
Log.e(TAG, "IOError with PDF");
e.printStackTrace();
}
Intent intent = new Intent(this, PdfActivity.class);
intent.putExtra("file", filename);
startActivity(intent);
The file is well created and readable, I can open this with ESExplorer application.
This file is located in /storage/emulated/0/myfile.pdf
in the PdfActivity I try to open the PDF :
Bundle extras = getIntent().getExtras();
String url = extras.getString("file");
File file = new File(url);
try {
if (file.exists()) {
Uri path = Uri.parse(url);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);
} else {
Toast.makeText(this, "File NotFound", Toast.LENGTH_SHORT).show();
}
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Viewer Application Found", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
file.exists() return true, Intent start, but my PDF reader says : "File not found"
I've added read and write permissions on external storage.
Does someone have any idea why it can't access to my file ?
objIntent.setDataAndType(url, "application/pdf");
use above line in your second snippest also declare String url as a global variable hope it will help you if it will not work try to use hardcode value of file path and see is it working or not ?
and are you sure file is exist? check this scenario tooo :)
I found the solution.
I've replace the Uri like that:
Uri path = Uri.fromFile(file);
And it works!
In my android, I have Adobe PDF reader installed. Now what I am trying to do is that when my PDF is downloaded and show PDF is clicked I am trying to open the PDF in Adobe PDF reader.
Following is the Intent but no option on Adobe PDF Reader Comes:
public String showPdf(String fileName) {
File file = new File(fileName);
Log.i("PdfViewer", "open file "+ fileName);
// if (file.exists()) {
Log.i("PdfViewer", "file exist");
try {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//intent.setData(Uri.parse(fileName));
this.ctx.startActivity(intent);
return "";
} catch (android.content.ActivityNotFoundException e) {
System.out.println("PdfViewer: Error loading url "+fileName+":"+ e.toString());
return e.toString();
}
Can you please suggest an intent to use?
Thanks,
Ankit.
I created a class openPDF which takes a byte array as input and displays the PDF file with Adobe Reader. Code:
private void openPDF(byte[] PDFByteArray) {
try {
// create temp file that will hold byte array
File tempPDF = File.createTempFile("temp", ".pdf", getCacheDir());
tempPDF.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempPDF);
fos.write(PDFByteArray);
fos.close();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(tempPDF);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (IOException ex) {
String s = ex.toString();
ex.printStackTrace();
}
}
When I pass the intend , the error from adobe reader is "Invalid file path". I read all other posts related to downloading and viewing PDF in android but dint help much. Any suggestions?
I think the issue is that other apps have no access to the files in your app's private data area (like the cache dir).
Candidate solutions:
changing the file's mode to MODE_WORLD_READABLE so that it can be read by other apps
...
String fn = "temp.pdf";
Context c = v.getContext();
FileOutputStream fos = null;
try {
fos = c.openFileOutput(fn, Context.MODE_WORLD_READABLE);
fos.write(PDFByteArray);
} catch (FileNotFoundException e) {
// do something
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
String filename = c.getFilesDir() + File.separator + fn;
File file = new File(filename);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
...
or write the pdf file to the /sdcard partition.
you can use android.os.Environment API to get the path, and remember to add the permission to your app's AndroidManifest.xml file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Regards
Ziteng Chen
I made this code to open an especific .pdf file existing in Dowloads folder with Adobe's application
File folder = new File(Environment.getExternalStorageDirectory(), "Download");
File pdf = new File(folder, "Test.pdf");
Uri uri = Uri.fromFile(pdf);
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.adobe.reader");
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
It works for me. So i guess your problem can be the temprorary file. Try to write the file to sdcard. To do this you will need add android.permission.WRITE_EXTERNAL_STORAGE to your AndroidManifest.xml.