Picture on PDF

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:

[code] // 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[/code]

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

this is from project “Create PDF with Picture.rbp”:

[code] 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=fp.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[/code]

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?

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

[code] 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[/code]

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

[quote=175184:@Peter Truskier]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 [/quote]

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.

[quote=175193:@Peter Truskier]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.[/quote]

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

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)

[code]//In the save PDF code,
dim p as picture
p = new picture (canvas1.width,canvas1.height,32)

//Then call

DoThePaint(p.graphics)[/code]

[quote=175200:@Jeff Tullin]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)

[code]//In the save PDF code,
dim p as picture
p = new picture (canvas1.width,canvas1.height,32)

//Then call

DoThePaint(p.graphics)[/code][/quote]

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

[code] dim p as picture = render

g.drawpicture(p,0,0)[/code]

Method is set up as follows:

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

What am I jacking up now?!

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?

[quote=175205:@Christian Schmitz]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?[/quote]

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?

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.

[quote=175209:@Jeff Tullin]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.[/quote]

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:

[code] 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[/code]

DoPaint Method:

[code] Chart = render

g.drawpicture(chart,0,0)[/code]

Chart is a property for the picture.

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

[code]dim p as picture
p = new picture (canvas1.width,canvas1.height,32)

  //Then call
  
  DoThePaint(p.graphics)[/code]

with this line

dim p as picture = self.chart

and maybe it works?

[quote=175225:@Christian Schmitz]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

[code]dim p as picture
p = new picture (canvas1.width,canvas1.height,32)

  //Then call
  
  DoThePaint(p.graphics)[/code]

with this line

dim p as picture = self.chart

and maybe it works?[/quote]

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

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

[quote=175229:@Christian Schmitz]did you debug it? is chart variable filled in that code?
What does the math give on coordinates?[/quote]

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

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

[quote=175310:@LangueR]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[/quote]

That worked with a little tweeking. Thank you so much. Now it is just adding additional text to the file. How would I accomplish that?