iOS Tutorial Reading and Writing PDF Documents

Discussion in 'iOS Tutorials' started by Erel, Dec 17, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    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:
    Dim pdf As PDFDocument
    pdf.Initialize(
    File.DirAssets, "example.pdf")
    The next step is to draw one of the pages with Canvas.DrawPDF:

    Code:
    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:
    Dim pcvs As Canvas
    pcvs.InitializePDF(
    File.DirDocuments, "1.pdf"612792)
    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.
     

    Attached Files:

    GudEvil, inakigarm, billzhan and 2 others like this.
  2. Allan Cameron

    Allan Cameron Member Licensed User

    I tried to run the reader but I get an error message indicating that I am missing a library reference.
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. Allan Cameron

    Allan Cameron Member Licensed User

    This writer is wonderful for creating professional looking PDFs. Many thanks!
     
    Erel likes this.
  5. Allan Cameron

    Allan Cameron Member Licensed 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.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Try to put the text in a Label and then draw the label with cvs.DrawView.
     
  7. Allan Cameron

    Allan Cameron Member Licensed 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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Please post it in B4A forum.
     
  9. highflyer

    highflyer Member Licensed User

    Is there any way to jump to a specific page ?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    The second parameter of cvs.DrawPdf method determines the page.
     
  11. highflyer

    highflyer Member Licensed User

    Thank You Erel....
     
  12. highflyer

    highflyer Member Licensed User

    ...and jumping to a specific page when using
    Code:
    WebView1.LoadUrl("file://" & File.Combine(File.DirAssets, "myfile.pdf"))
    ??
     
  13. Erel

    Erel Administrator Staff Member Licensed User

  14. highflyer

    highflyer Member Licensed User

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

    Erel Administrator Staff Member Licensed User

    You need to wait to the PageFinished event.
     
  16. highflyer

    highflyer Member Licensed User

    No success with the following code...
    Code:
    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
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Have you tried to use a timer to call it a second or two after PageFinished fired?
     
  18. highflyer

    highflyer Member Licensed User

    The timer trick did not worked out. I'm wondering if the problem is the javascript command or something else.....
     
  19. borofan

    borofan Member Licensed User

    Has anyone been able to wrap text by drawing a label with cvs.DrawView?
     
  20. zity

    zity Member Licensed 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: Aug 16, 2016
Loading...