How to create PDF of using content of activity in android? - android

I have create an app of payroll system in android and it needs to be save the salary slip in PDF format that's why i need to create pdf of using content from SalarySlip.java.
Thanks.

For devices with API level 19 or more you can use the built in PrintedPdfDocument.
This class is a helper for creating a PDF file for given print attributes. It is useful for implementing printing via the native Android graphics APIs.
// open a new document
PrintedPdfDocument document = new PrintedPdfDocument(context,printAttributes);
// start a page
Page page = document.startPage(0);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());
//close the document
document.close();
For more advance Features use iText

Related

All webview content printed in one page

Case: User should be able to view and print pdf
My solution: I am opening PDF inside Webview with the help of docs.google.com/gview. Below is my code
Set up Webview
string url = "http://www.africau.edu/images/default/sample.pdf";
string gview = $"https://docs.google.com/gview?embedded=true&url={url}";
mWebView.LoadUrl(gview);
Print PDF
var printMgr = (PrintManager)GetSystemService(PrintService);
printMgr.Print("print", mWebView.CreatePrintDocumentAdapter("print"), null);
Below is the screenshot. As you can see PDF loads just fine
Problem
When I want to print PDF, all the PDF pages are printed in one paper which you can see below
I would appreciate any suggestion, including different library for displaying/printing pdf or suggestion in Java or Kotlin, I can convert them to C#.
I would not print the web page but print the PDF directly as when printing the web page it just sees it as a longer web page and knows nothing about the content.
Use a custom print adapter instead, but instead of drawing a PDF to print you can just use the existing PDF you already have.
See for details https://developer.android.com/training/printing/custom-docs.html

Where to start: Creating a PDF in my Android application

I'm creating an android app with the ultimate goal of being able to generate a PDF from the data collected from the user. Where is a good place to start my research? Is there an API or studio plugin that I should be researching?
Thanks and have a great weekend!
You need android.graphics.pdf library reference page is https://developer.android.com/reference/android/graphics/pdf/package-summary.html
and u can use below codes
// create a new document
PdfDocument mydocument = new PdfDocument();
// crate a page description
PageInfo mypageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();
// start a page
Page mypage = mydocument.startPage(mypageInfo);
// draw something on the page
View content = getContentView();
content.draw(mypage.getCanvas());
// finish the page
mydocument.finishPage(mypage);
. . .
// add more pages
. . .
// write the document content
mydocument.writeTo(getOutputStream());
// close the document
mydocument.close();

What is the function getContentView() in PrintedPdfDocument of Android

I am working on the printing custom file of Android, and I have some doubts in developer website of PrintedPdfDocument
// open a new document
PrintedPdfDocument document = new PrintedPdfDocument(context,
printAttributes);
// start a page
Page page = document.startPage(0);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
What does the getContentView mean? I think I need to write the function body by myself, but I am not sure what is view mean? Can anyone help?
Yeah. This method needs to be written unless you're working within a PopupWindow or GuidedActionsStylist.ViewHolder. These two has their own getContentView method supplied.
Basically, what this method does or intending to do is to provide you the root(or content) view of window.
Refer to this if you're writing this code under an Activity and implement your getContentView accordingly. Refer to this if you're using Fragment.

How to get the link coordinates in EBookDroid of Android for opening the links programmatically

I'm developing a EBookreading application, EBookDroid is a library which we are using for the PDF reading. If we have any links in the PDF like www.stackoverflow.com, when the user clicks on it, it supposed to open the link, for that i need to find out the coordinates of that link in the Document.
I am writing like the below.
final RectF linkRect = page.getLinkSourceRect(pageBounds, link);
But it's always giving the null back.
Try this may be use full
THIS EXAMPLE

Open URL or PDFreader in custom screen Android

I am working on application where I need to open inbuilt apps in a custom screen like in Popup window
but when I invoke the inbuilt app the screen size is full screen while I need customize screen size. The inbuilt app can be anything like browser or pdfreader.
Here is my code :-
String strurl = "/sdcard/download/28889.pdf";
File file = new File(strurl);
if (file.exists()) {
Uri path = Uri.fromFile(file);
// Log.e("path of the file",path.toString());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(),
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
This opens up new window in my app but I need Popup kind of window where I need to show this pdfreader to get display.
By design, Android does not allow one app to run inside another (among other things, for obvious security reasons). So if that is what you want to do, you are out of luck.
BUT you can use a WebView (I let you consult the doc on this class) to display a webpage inside your app.
There are also some pdf libraries out there. A word of caution though, I worked on a pdf reader on Android 1 year ago and implementing a pdf reader was just not the best solution. Handling the pdf decoding on server side and serving a view of this image (or conversion to html) was the best solution.
This way you don't use too much computing power to read the pdf (surprisingly you need some serious cpu to read a pdf).
Things may have changed since I made that development (I was really constrained by the time factor) though but I still don't think that unless you explicitely need to display a pdf document and not just a document (for example a legitimate reason would be that you need to check the signature of the pdf on the Android device), you should not use the pdf format on a phone.
For web URL You can use Webview
For PDF
You need to make your pdf reader or you can use any open source pdf reader to make your own customized version of the pdf reader which you can use in your app.
Some of the them are
1) http://www.mupdf.com/
2) http://sourceforge.net/projects/pdfedit/
3) Search on Google/SO for other Links
Here is the Example code for pdf Reader:
http://lokeshatandroid.blogspot.in/2012/07/android-pdf-reader.html

Categories

Resources