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:
Code: Copy
Dim pdf As PDFDocument
pdf.Initialize(File.DirAssets, "example.pdf")

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

Code: Copy
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.
Code: Copy
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

Kind of embarrassed for this question but, how could I zoom in and out the pdf file read in example?
 
Thanks Erel. Now, with webview I cannot see the page. A very, very simple example of pdf webview is available?

Just Sorry for my lack of knowledge but...I'm learning. ha ha ha
 
Last edited:
Has someone an example of how to create a pdf file from a layout??
I'm becoming mad without success...
Thank you in advance.
 
After 3 hours working on it, finally i got it.
I share my solution if someone find it helpful.
Code: Copy
Dim PDFPanel As Panel
PDFPanel.Initialize("PDFPanel")
PDFPanel.Width=612dip
PDFPanel.Height=792dip
PDFPanel.LoadLayout("PDFLayout")
Dim PDFCanvas As Canvas
PDFCanvas.InitializePDF(File.DirDocuments,"test.pdf",612,792)
For Each v As View In PDFPanel.GetAllViewsRecursive
    Dim Rect As Rect
    Select True
        Case v Is ImageView
            Dim iv As ImageView
            iv=v
            iv.ContentMode=iv.MODE_FIT
            Rect.Initialize(iv.Left,iv.Top,iv.Left+315,iv.Top+75) 'The size i need
            PDFCanvas.DrawView(iv,Rect)
        Case v Is Label
            Dim lab As Label
            lab=v
            Rect.Initialize(lab.Left,lab.Top,lab.Left+lab.Width,lab.Top+lab.Height)
            PDFCanvas.DrawView(lab,Rect)
            Log(lab.Text&"-"&lab.Top&"-"&lab.Left)
    End Select
Next
PDFCanvas.Release

Thank you anyway.
 
Back
Top