Create, write and display PDF file in android app
Do it this way:
import com.cete.dynamicpdf.*;
import com.cete.dynamicpdf.pageelements.Label;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
public class DynamicPDFHelloWorld extends Activity {
private static String FILE = Environment.getExternalStorageDirectory()
+ "/HelloWorld.pdf";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a document and set it's properties
Document objDocument = new Document();
objDocument.setCreator("DynamicPDFHelloWorld.java");
objDocument.setAuthor("Your Name");
objDocument.setTitle("Hello World");
// Create a page to add to the document
Page objPage = new Page(PageSize.LETTER, PageOrientation.PORTRAIT,
54.0f);
// Create a Label to add to the page
String strText = "Hello World...\nFrom DynamicPDF Generator "
+ "for Java\nDynamicPDF.com";
Label objLabel = new Label(strText, 0, 0, 504, 100,
Font.getHelvetica(), 18, TextAlign.CENTER);
// Add label to page
objPage.getElements().add(objLabel);
// Add page to document
objDocument.getPages().add(objPage);
try {
// Outputs the document to file
objDocument.draw(FILE);
Toast.makeText(this, "File has been written to :" + FILE,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this,
"Error, unable to write to file\n" + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
Also check these links. They will help you to fulfill your requirement.
http://www.dynamicpdf.com/Blog/post/2012/06/15/Generating-PDFs-Dynamically-on-Android.aspx
https://github.com/JoanZapata/android-pdfview
How to create PDFs in an Android app?
Render a PDF file using Java on Android
you'll find the correct code that actually works right.for creating a pdf file, putting some content in it, saving in and the opening the newly created file.
For this you'll need to add the jar of iTextG to your project:
- OR
if you WANT TO convert your layout or view into pdf then you have to create image from your layout and then add into pdf.Perfect tutorial of that Go Through this Link . Hope this will help you guys.Thank you.
For Simple Create and Open pdf:
// Method for creating a pdf file from text, saving it then opening it for display
public void createandDisplayPdf(String text) {
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "mypdffile.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.FontFamily.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("mypdffile.pdf", "PDF");
}
// 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(this, "Can't read pdf file", Toast.LENGTH_SHORT).show();
}
}