iOS Tutorial Reading and Writing PDF Documents

SS-2014-12-17_14.51.55.png


iOS has built-in support for reading and writing PDF documents.

The Canvas object allows us to display existing PDF documents and to create new PDF documents.

Displaying a PDF document

First we need to load the PDF file. This is done with PDFDocument object:
B4X:
Dim pdf As PDFDocument
pdf.Initialize(File.DirAssets, "example.pdf")

The next step is to draw one of the pages with Canvas.DrawPDF:

B4X:
cvs.DrawPDF(pdf, 1, cvs.TargetRect)
The line above draws the first page of the document we previously loaded into the specified rectangle. In this case we draw it to cvs.TargetRect which is a rectangle with the same size as the view's size.
Note that the first page number is 1.

That's it. A simple PDF reader example is attached. You need to add a PDF file to the Files folder in order to run it.

Creating a new PDF document

The Canvas object also allows us to create new PDF documents. Instead of drawing to a View, the Canvas draws to a PDF file.
B4X:
Dim pcvs As Canvas
pcvs.InitializePDF(File.DirDocuments, "1.pdf", 612, 792)
The line above creates a new canvas object that will draw to the specified file with the given width and height. Note that 612 x 792 is the standard document size.

You can now use the standard Canvas methods to draw to the file.

There are two important points:
1. You can create new pages by calling Canvas.NextPDFPage.
2. You must call Canvas.Release when you are done drawing.
 

Attachments

  • PDFReader.zip
    2.5 KB · Views: 1,878

Allan Cameron

Member
Licensed User
Longtime User
Question 1. Has anyone written some smart code which takes a string of text such as:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vestibulum sit amet eros eu dignissim. Maecenas sit amet scelerisque lorem, sed finibus nisi. Pellentesque quis arcu eget justo viverra vulputate et vel mi. Aliquam nec semper tortor. Donec consectetur erat metus, vel tempor libero eleifend vitae. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque eleifend auctor diam id vehicula. Curabitur dignissim, risus in varius hendrerit, justo elit gravida ipsum, at molestie tellus est id felis. Vestibulum viverra lectus nec diam iaculis, ut aliquet turpis consequat. Donec fringilla placerat massa ut eleifend. Mauris vel turpis in felis maximus varius."

and chunks it into appropriately sized lengths for writing using Canvas.Drawtext, because as I understand things I need to insert CRLFs into the appropriate positions for this to print as a para? Or have I misunderstood things?

Question 2. In the above example, if I draw a long string of text, the text is vertically aligned bottom rather than aligned top. Is there a way to change it so it is aligned top.
 

Allan Cameron

Member
Licensed User
Longtime User
I have completed my B4i app which creates a PDF file and it all works very well.

I now want to update my B4A app so that it resembles the B4i app and I am struggling to see the best way to write and read PDFs in B4a.

I would be very grateful for advice.
 

highflyer

Member
Licensed User
Longtime User
...and jumping to a specific page when using
B4X:
WebView1.LoadUrl("file://" & File.Combine(File.DirAssets, "myfile.pdf"))
??
 

highflyer

Member
Licensed User
Longtime User
Unfortunately the following code does not seem to be working..
B4X:
WebView1.LoadUrl("file://" & File.Combine(File.DirAssets, "ejets_cap12.pdf"))
    no.RunMethod("stringByEvaluatingJavaScriptFromString:", Array("window.scroll(10,3000)"))
 

highflyer

Member
Licensed User
Longtime User
No success with the following code...
B4X:
Sub WebView1_PageFinished(Success As Boolean, Url As String)
    Log("webview page finished")
    Dim no As NativeObject = WebView1
    no.RunMethod("stringByEvaluatingJavaScriptFromString:", Array("window.scroll(0,500)"))
End Sub
 

highflyer

Member
Licensed User
Longtime User
The timer trick did not worked out. I'm wondering if the problem is the javascript command or something else.....
 

zity

Member
Licensed User
Longtime User
I'm creating PDFs this way and would like to set the document metadata (author, title, etc.).
Would it be possible to add a second initialization function, that also takes a map with these settings and passes them as a dictionary to the corresponding iOS API?

It would be also nice to have the CGPDFContextSetURLForRect / CGPDFContextSetDestinationForRect functions wrapped, or at least have an example of using them with inline Obj-C code.

Thank you!
 
Last edited:
Top