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();
Related
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.
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
I want to displaythe thumb images of pdf in my app. I am using this answer. But I don't know how to use it in android as Rectangle2D can't be userd in java. I tried using RectFand modified the code as:
File file = new File(arrayOfResults.get(arg0).filePath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
0, channel.size());
PDFFile pdffile = new PDFFile(buf);
// draw the first page to an image
PDFPage page = pdffile.getPage(0);
// get the width and height for the doc at the default zoom
RectF rect = new RectF(0, 0, (int) page.getWidth(),
(int) page.getHeight());
But the app crashed with the following error:
java.lang.NoClassDefFoundError: java.awt.geom.Rectangle2D$Double
at com.sun.pdfview.PDFFile.parseNormalisedRectangle(PDFFile.java:1874)
Please help.
Android added a PdfRenderer class that can do that in API 21.
If you need to go below that, you need to ship with your own PDF render engine. We've looked at the marketplaces and found that the options are all lacking in one way or another, and created PSPDFKit for Android. It's a commercial PDF framework and (full disclaimer) I'm part of the team that builds it.
In PSPDFKit for Android, you can use renderPageToBitmap to render the PDF into a bitmap.
If your project is personal or you are an indie, try http://plugpdf.com/
They offer their Android PDF SDK free for you. You can also check out their blog article about rendering a PDF page to bitmap image on Android. http://plugpdf.com/how-to-render-a-pdf-document-to-bitmap-image-on-android/
Disclaimer: I am the Founder & CEO at ePapyrus Inc. which runs plugpdf.com.
I have seen code here which will open a PDF file in a PDF viewer. Is it possible to modify the code to make the PDF viewer open the PDF at a specific page?
No. You cannot control 3rd party application unless it gives this possibility.
Yes there is a way to open a specific page in a pdf file
Inside Main Activity
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), pdfActivity.class);
intent.putExtra("pageno", 5); // add this to pass the page number
startActivity(intent);
}
});
Inside Pdf Activity
pdfView.fromAsset("SAMPLE_FILE.pdf")
.defaultPage(getIntent().getIntExtra("pageno",0)) // this is used to scroll to the page required
.enableAnnotationRendering(true)
.scrollHandle(new DefaultScrollHandle(this))
.load();
If you don't want it to pass through intent
pdfView.fromAsset("SAMPLE_FILE.pdf")
.defaultPage(65) // Write the number of the page
.enableAnnotationRendering(true)
.scrollHandle(new DefaultScrollHandle(this))
.load();
Yes You can open a Specific page in pdf file.
first of all create a global varble like
// give a defualt value
int pageNumber=0;
// secondly create a button in search dilaoge to find exact page in activty.
btn.setOnClickListner(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = 1;
String input = String.valueOf(textSearch.getText());
if (input != null && !input.equals("")) {
position =
Integer.valueOf(String.valueOf(textSearch.getText()));
}
// assign globar var pageNumber to finding position
pageNumber = position-1;
textSearch.setText("");
alertDialog1.dismiss();
}
// in Third Step just passed the pageNumber in defualt function.
pdfViewr.from("Your uri String ").default(pageNumber).load();
for ezpdf reader:
add intent.putExtra("page", 110);
But ezpdf is quite old. So I recommend you to checkout my new opensource pdf viewer and annotator project:
https://github.com/KnIfER/PolymPic
Intent it = new Intent(Intent.ACTION_VIEW)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("page", yourPageIndex)
.setDataAndType(yourUri, "application/pdf");
startActivity(it);
It's based on PDFium, which is fast and can even run pretty well on the android 4.4.
Andrejs has recommended that I should modify an open source pdf viewer app. When I become more confident at Android programming I may attempt that. Meanwhile, I am following a different tack. I am storing images of the individual PDF pages as separate jpg files and using WebView / WebViewClient to display the individual pages. These can be zoomed and panned (as in a PDF Viewer) and it is easy to make my app move to the next or previous page when I touch the screen near the edge of the page.
Open a PDF file to a specific page
To target an HTML link to a specific page in a PDF file, add #page=[page number] to the end of the link's URL.
For example, this HTML tag opens page 4 of a PDF file named myfile.pdf:
<A HREF="http://www.example.com/myfile.pdf#page=4">
I have a one question, because I`m not a programmer, I use only tutorials for teaching Flex and programming in his.
Is possible to make new render file for s:Label or s:Text property? Because I make a small RSS application for mobile and I have this code for showing images from description of RSS items (this code is located on DetailsView.mxml): var descArr:Array = rdesc.split("src="); var resArr:Array = descArr[1].split(".jpg"); var resArr2:Array = resArr[0].split("\""); rimage = resArr2[1] + ".jpg";
In new renderer file I will hide html tags, because if I use this code: var p:RegExp = /(,
So, I need render on new .mxml document with same code of DetailsView, but in new render (if it possible) I use this RegExp for hiding html text… How I set s:Label or s:Text to show renderer from new document?
Thanks for any help
Is possible to make new render file for s:Label or s:Text property?
No! Renderers only exist in list based classes or with DataGroups. You can read more about itemRenderers and what they do here.
Now if you want to make a component that includes text and images and can be used as a renderer, that is possible. Read more about creating components.