Picture on PDF

  1. 4 years ago

    This is the LAST part of my project. I am saving an image as a PDF using MBS DynaPDF. I am attempting to make the image come from a canvas. The canvas size can be adjusted, so keeping it non-reliant on the canvases size is a must. This is the hack-job that I have so far:

          // get picture
          dim p as picture=Canvas1.Backdrop
          
          dim x,y,w,h as Double
          dim f as Double
          dim dw as integer = d.GetPageWidth
          dim dh as integer = d.GetPageHeight
          
          // scale to fit the page size
          
          f=min(dw/p.Width, dh/p.Height)
          
          w=f*p.Width
          h=f*p.Height
          
          x=(dw-500)
          y=(dh-2600)
          
          // add image to pdf
          call d.InsertPicture(p, x, y, w, h)
          call 
          
          call d.EndPage
          call d.CloseFile
          
          file.Launch

    I have come to the conclusion that I suck at manipulating the plug ins.

  2. Christian S

    19 Mar 2015 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    this is from project "Create PDF with Picture.rbp":

     dim d as new MyDynapdfMBS
      dim file as FolderItem
      
      d.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License
      
      file=SpecialFolder.Desktop.Child("Create PDF with Picture.pdf")
      
      call d.CreateNewPDF file
      call d.Append
      
      call d.SetSaveNewImageFormat(false) // if possible pass through
      call d.SetUseTransparency(false) // no transparent color
      call d.SetCompressionFilter(d.kcfFlate) // no compression
      call d.SetResolution(300) // max resolution
      
      // get picture
      dim p as Picture = LogoMBS(500)
      
      dim x,y,w,h as integer
      dim f as Double
      dim dw as integer = d.GetPageWidth-100
      dim dh as integer = d.GetPageHeight-100
      
      // scale to fit the page size
      f=min(dw/p.Width, dh/p.Height)
      
      w=f*p.Width
      h=f*p.Height
      
      x=50+(dw-w)/2
      y=50+(dh-h)/2
      
      // add image to pdf
      call d.InsertPicture(p, x, y, w, h)
      
      call d.EndPage
      call d.CloseFile
      
      file.Launch
  3. Christian S

    19 Mar 2015 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    as you see it starts with a new dynapdf object. We call CreateNewPDF with a folderitem to start a new PDF with output to this file. Next we append a page to the PDF.
    For best image compression quality, we allow pass through, set the compression to no compression (flate), limit resolution to 300 dpi and disable any transparency.

    Now we get a picture, in this case form the LogoMBS function. You could of course use your canvas here (query backdrop).

    Next we do some math to scale it down/up to have the size of the page minus a 100 points. This keeps a border. Now we center picture and use InsertPicture function to put it on the page.

    Finally the page is closed as well as the file and we can launch it in pdf viewer.

    Questions?

  4. When I try to execute the pdf, I get NilObjectException error.

          dim d as new MyDynapdfMBS
          dim e as MyDynapdfMBS
          dim file as FolderItem
          
          d.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License
          
          file=SaveLocation.Child(Savename1+".pdf")
          
          call d.CreateNewPDF file
          call d.Append
          call d.SetResolution(300) // max resolution
          
          // no recompressing
          call d.SetSaveNewImageFormat(false)
          
          // get picture
          dim p as picture=Canvas1.Backdrop
          
          dim x,y,w,h as Double
          dim f as Double
          dim dw as integer = d.GetPageWidth
          dim dh as integer = d.GetPageHeight
          
          // scale to fit the page size
          
          f=min(dw/p.Width, dh/p.Height) //This is where I am getting the NilObjectError
          
          w=f*p.Width
          h=f*p.Height
          
          x=(dw-500)
          y=(dh-2600)
          
          // add image to pdf
          call d.InsertPicture(p, x, y, w, h)
          
          
          call d.EndPage
          call d.CloseFile
          
          file.Launch
  5. Peter T

    19 Mar 2015 Pre-Release Testers, Xojo Pro Oakland, CA USA

    When I try to execute the pdf, I get NilObjectException error.

    Where?

    Also, one thing I sometimes find problematic in starting out with the DynaPDFMBS example projects is the fact that they tend to ignore the boolean result values of the DynaPDF functions (by invoking "call"). This can make it harder to tell which function call is causing a problem in our code.

    I find it helpful to replace this: call pdf.yaddayadda() with something like this: if NOT pdf.yaddayadda() then break

  6. @Peter T Where?

    Also, one thing I sometimes find problematic in starting out with the DynaPDFMBS example projects is the fact that they tend to ignore the boolean result values of the DynaPDF functions (by invoking "call"). This can make it harder to tell which function call is causing a problem in our code.

    I find it helpful to replace this: call pdf.yaddayadda() with something like this: if NOT pdf.yaddayadda() then break

    I marked it in my code. It is as the f=min(dw/p.Width, dh/p.Height) line.

  7. Peter T

    19 Mar 2015 Pre-Release Testers, Xojo Pro Oakland, CA USA

    @Meade L I marked it in my code. It is as the f=min(dw/p.Width, dh/p.Height) line.

    Doh! I totally skipped over that. Sorry...

    So it looks like perhaps your canvas doesn't have a backdrop? When it breaks in the debugger, is p NIL? Unless you've explicitly assigned a picture to be the canvas' backdrop, it will be. The contents of a canvas (as drawn by your paint event code) will not create a backdrop picture.

  8. @Peter T Doh! I totally skipped over that. Sorry...

    So it looks like perhaps your canvas doesn't have a backdrop? When it breaks in the debugger, is p NIL? Unless you've explicitly assigned a picture to be the canvas' backdrop, it will be. The contents of a canvas (as drawn by your paint event code) will not create a backdrop picture.

    So it is drawn by the paint event. What is the remedy?

  9. Jeff T

    19 Mar 2015 Pre-Release Testers Midlands of England, Europe

    Take all the PAINT code from the canvas and put it into a new method called DoThePaint (g as graphics)

    In the PAINT event, call

    DoThePaint(g)
    //In the save PDF code, 
    dim p as picture
    p = new picture (canvas1.width,canvas1.height,32)
    
    //Then call  
    
    DoThePaint(p.graphics)
  10. @Jeff T Take all the PAINT code from the canvas and put it into a new method called DoThePaint (g as graphics)

    In the PAINT event, call

    DoThePaint(g)

    //In the save PDF code, dim p as picture p = new picture (canvas1.width,canvas1.height,32) //Then call DoThePaint(p.graphics)

    So I believe I wrote the method in correctly. I have the following:

      dim p as picture = render
      
      g.drawpicture(p,0,0)

    Method is set up as follows:

    Method name: DoThePaint
    Parameters: g as graphics
    Return Type: Picture

    What am I jacking up now?!

  11. Christian S

    19 Mar 2015 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    is that for the chart project you have?
    maybe you could store the picture from the chart in a property on the window, so you have it later for creating the PDF?

  12. Edited 4 years ago

    @ChristianSchmitz is that for the chart project you have?
    maybe you could store the picture from the chart in a property on the window, so you have it later for creating the PDF?

    It is for the chart project. So you suggest I store it as a picture property?

    Now that it is a picture property "chart" how should I implement that into my code above?

  13. Jeff T

    19 Mar 2015 Pre-Release Testers Midlands of England, Europe

    What am I jacking up now?!

    Need more information than that.
    The method doesnt need a return type.

    Whats the problem/symptom?

    That method draws the image called render onto the graphics passed into the method.
    As a parameter not as a return type.

  14. @Jeff T Need more information than that.
    The method doesnt need a return type.

    Whats the problem/symptom?

    That method draws the image called render onto the graphics passed into the method.
    As a parameter not as a return type.

    Okay, so now it is not filling in the pdf at all. Just a blank form with the DynaPDF stamp over it.

    The question at this point it why am I seeing nothing?

    code is as follows:

          dim d as new MyDynapdfMBS
          dim e as MyDynapdfMBS
          dim file as FolderItem
          
          d.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License
          
          file=SaveLocation.Child(Savename1+".pdf")
          
          call d.CreateNewPDF file
          call d.Append
          call d.SetResolution(300) // max resolution
          
          // no recompressing
          call d.SetSaveNewImageFormat(false)
          
          //In the save PDF code,
          dim p as picture
          p = new picture (canvas1.width,canvas1.height,32)
          
          //Then call
          
          DoThePaint(p.graphics)
          
          dim x,y,w,h as Double
          dim f as Double
          dim dw as integer = d.GetPageWidth
          dim dh as integer = d.GetPageHeight
          
          // scale to fit the page size
          
          f=min(dw/p.Width, dh/p.Height)
          
          w=f*p.Width
          h=f*p.Height
          
          x=(dw-500)
          y=(dh-2600)
          
          // add image to pdf
          call d.InsertPicture(p, x, y, w, h)
          
          
          call d.EndPage
          call d.CloseFile
          
          file.Launch

    DoPaint Method:

      Chart = render
      
      g.drawpicture(chart,0,0)

    Chart is a property for the picture.

  15. Christian S

    19 Mar 2015 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    so you have a property "chart as picture" on the window and fill it when you update the chart in the canvas?
    great, so next

    you replace the lines

    dim p as picture
          p = new picture (canvas1.width,canvas1.height,32)
          
          //Then call
          
          DoThePaint(p.graphics)


    with this line

    dim p as picture = self.chart

    and maybe it works?

  16. @ChristianSchmitz so you have a property "chart as picture" on the window and fill it when you update the chart in the canvas?
    great, so next

    you replace the lines

    dim p as picture
          p = new picture (canvas1.width,canvas1.height,32)
          
          //Then call
          
          DoThePaint(p.graphics)


    with this line

    dim p as picture = self.chart

    and maybe it works?

    That did not seem to improve anything as the pdf is still empty other than the DynaPDF watermark.

  17. Christian S

    19 Mar 2015 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    did you debug it? is chart variable filled in that code?
    What does the math give on coordinates?

  18. Edited 4 years ago

    @ChristianSchmitz did you debug it? is chart variable filled in that code?
    What does the math give on coordinates?

    With the code, the p variable gives "Nil", so it is receiving nothing.

    With Jeff's code, the p variable returned a picture.

    Height adjusts based on the width and height of the window.

    In the standard setup, the height is 304 and the width is 861

  19. The following is a crude example following Jeff's and Christian's suggestions. It is probably not the greatest since I still struggle with the Canvas and Picture objects, but it does work.

    https://dl.dropboxusercontent.com/u/42062295/RealSoftware/forum%20-%20canvas%20picture%20to%20pdf.rbp

  20. Newer ›

or Sign Up to reply!