Show PDF File--android/ios

Source: Internet
Author: User

Tag: ninm causes log doc to page the MST with double upper

Years later, the company's projects are tight, resulting in the plan at the beginning of the year is not well implemented.
The more recently stayed up late, I hope to be able to finish the first goal before June.

Today talk about loading PDF files directly in the code (company's new requirements, locally sourced).

Background of my needs:
1. First, as the mobile side continues to encroach on the PC scene, the mobile phone read more and more common, and some of the existing PDF text is expected to be loaded into the mobile phone to do a friendly display
2. Secondly, the idea of the company's new project is to display type, minimize the network interaction (that is, as little as possible to obtain data from the interface), in addition to user feedback, detection updates, etc., the whole project only uses two interfaces, a large number of business logic is to download the database on-premises operation. As a result, the local display of PDF files also conforms to the

the difference between the mobile side
The difference between the Android and IOS side is that
1. Advantages:
Android: Provides a rich interface that is sufficient for good user interaction
iOS: Simple to call, there are roughly three implementations of the method, the simplest is to use UIWebView direct display

2. Disadvantages:
Android: Call trouble, Google related services can not be invoked, forcing Android to use a third-party library to load (or write your own ...) ), and because the projects that integrate third-party libraries become quite large, the size of the package increases by around 15M.
iOS: Custom hassle, want friendly interactions need to be manually modified, especially want to pagecontroller the page effect, need to package themselves

Enter the code topic below

1. First on the Android side:

There are more star Androidpdfviewer on GitHub, so choose this
Project Address:

Https://github.com/barteksc/AndroidPdfViewer

First, add the dependency in the Gradle

‘com.github.barteksc:android-pdf-viewer:2.5.0‘

Second, the XML layout file

  <com.github.barteksc.pdfviewer.PDFView        android:id="@+id/pdf_fragment"        android:layout_width="match_parent"        android:layout_height="match_parent" />

Finally, the code calls:

Pdfview Mpdfview = (pdfview) view. Findviewbyid(R. ID. pdf_fragment);Mpdfview. Fromasset("Sample.pdf"). Defaultpage(0)//Default page. Enableswipe(true)//Allow dragging. Swipehorizontal(false)//The vertical axis is the default direction, and true changes to horizontal. Enabledoubletap(true)//Allow double-click to indent. Onpagechange(this)//Set page number Toggle Monitor. Load();

Listen for the callback code, (here I used to record the page the user last viewed)

    Private intPdf_book_mark =0;//define the default page number    @Override     Public void OnStop() {Super. OnStop ();Save this page number to sharepreference when//stop life cyclePreferenceutil.save (Getactivity (),"Pdf_book_mark", Pdf_book_mark); }@Override     Public void onpagechanged(intPageintPageCount) {logutil.i ("page ="+ page +", PageCount ="+ PageCount);    Pdf_book_mark = page; }//life cycle recovery display, the default load page number is removed from the sharepreference  Private void Lastbrowe() {.... mpdfview.defaultpage (Preferenceutil.getsharepre (Getactivity ()). GetInt ("Pdf_book_mark",0))      ...    }

Simply written, crossing can try more UI interactions on its own

All the calling interfaces are as follows:

@Override public void onpagechanged (int page, int pagecount) {Pdfview. Fromuri(Uri)orPdfview. FromFile(File)orPdfview. Frombytes(byte[])orPdfview. FromStream(InputStream)//stream is written-bytearray-native code cannot use Java StreamsorPdfview. Fromsource(DocumentSource)orPdfview. Fromasset(String). Pages(0,2,1,3,3,3)//All pages is displayed by default. Enableswipe(true)//allows to block changing pages using swipe. Swipehorizontal(false). Enabledoubletap(true). Defaultpage(0). OnDraw(Ondrawlistener)//allows to draw something on a provided canvas, above the current page. OnLoad(Onloadcompletelistener)//called after document is loaded andStarts to be rendered. Onpagechange(Onpagechangelistener). Onpagescroll(Onpagescrolllistener). OnError(Onerrorlistener). OnRender(Onrenderlistener)//called after document was rendered for the first time. enableannotationrendering(false)//render annotations (such as comments, colorsorForms. Password(NULL). Scrollhandle(NULL). enableantialiasing(true)//improve rendering a little bit on low-res screens. Load();}

Onpagescroll/onpagechange/onrender/password/ondraw
These few are enough to customize the rich scene. See Https://github.com/barteksc/AndroidPdfViewer for details.

2.IOS Terminal
I've only implemented two simple ways,
The third need to be customized, recommended reference
http://blog.csdn.net/qq_24901135/article/details/46438781

Implementation method One UIWebView:

- (void) Viewdidload {[SuperViewdidload];//Get sample.pdf file path (local)    NSString*path = [[NSBundleMainbundle] pathforresource:@"Sample"oftype:@"PDF"];general usage of//uiwebview    Nsurl*url = [NsurlFileurlwithpath:path];nsurlrequest*request = [nsurlrequestRequestwithurl:url]; [ Self. WebViewLoadrequest:request];}

This way can only be browsed, Safari is not going to let you have more play space

Implementation Method Two

The protocol is implemented in the. h file first.

Uidocumentinteractioncontrollerdelegate

Second. m file

#import "PDFViewController.h"  @interface pdfviewcontroller ()@property(Strong,nonatomic) Uidocumentinteractioncontroller *documentinteractioncontroller;-(ibaction) Open: (ID) sender;-(ibaction) Preview: (ID) sender;@end @implementation pdfviewcontroller - (void) Viewdidload {[SuperViewdidload]; }- (ibaction) Open: (ID) Sender {UIButton*button = (UIButton*) sender;Nsurl*url = [[NSBundleMainbundle] urlforresource:@"Sample"withextension:@"PDF"];if(URL) {//Initialize Document Interaction Controller         Self. Documentinteractioncontroller= [Uidocumentinteractioncontroller Interactioncontrollerwithurl:url];//Configure Document Interaction Controller[ Self. DocumentinteractioncontrollerSetdelegate: Self];//Present Open in Menu[ Self. DocumentinteractioncontrollerPresentopeninmenufromrect:[button Frame] InView: Self. ViewAnimated:YES]; }}- (ibaction) Preview: (ID) Sender {Nsurl*url = [[NSBundleMainbundle] urlforresource:@"Sample"withextension:@"PDF"];if(URL) {//Initialize Document Interaction Controller         Self. Documentinteractioncontroller= [Uidocumentinteractioncontroller Interactioncontrollerwithurl:url];//Configure Document Interaction Controller[ Self. DocumentinteractioncontrollerSetdelegate: Self];//Preview PDF[ Self. DocumentinteractioncontrollerPresentpreviewanimated:YES]; }}#pragma document-(Uiviewcontroller*) Documentinteractioncontrollerviewcontrollerforpreview: (Uidocumentinteractioncontroller *) controller{return  Self;}@end

This way you can open a layer of display PDFs on top of the view, with some UI interaction to work with.

Roughly so. Sneak in and stick it out at night.
Tomorrow fools, you must not believe the company said overtime trick.
Wish, April happy!

Show PDF File--android/ios

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.