Yesterday at 09:19 AM1 day We have been using GraphicsMagick functions for years to draw some text, but we can do the same in DynaPDF. We draw text in a new PDF document and then render it as image without ever saving the PDF document. We can even use transparency and render the PDF with alpha channel and save as PNG image. Our first example places some text on the PDF page using the DynaPDF.WriteStyledTextEx function. This function can take text with styles from a field in FileMaker (or variable) and draw it into the PDF. The styles are converted for DyynaPDF's format commands. The text is wrapped to the desired width of 300 points and we use justify for alignment. After the text is drawn, we use DynaPDF.GetLastTextPosY to query how big the text block is. Passing -1 as height for the text box allows us to have a variable height. This provides you the information on where to place another text or where to draw additional text. In our example we use the text position and the page height to calculate a new media box. This limits the visible part of the page to only the size of our text box. Then we close the page and render the 1st page of the PDF document as an image. We pass the page number and the request resolution with 150 dpi. The default format is a RGB image stored as JPEG. In the script you can decide whether you like to save as PDF or render the image.Here is the complete script except the DynaPDF initialization: # Start with a new PDF documentSet Variable [ $pdf ; Value: MBS("DynaPDF.New") ]# Add pageSet Variable [ $r ; Value: MBS("DynaPDF.AppendPage"; $pdf) ]Set Variable [ $r ; Value: MBS("DynaPDF.SetPageCoords"; $pdf; "topdown") ]Set Variable [ $r ; Value: MBS("DynaPDF.SetFont"; $pdf; "Helvetica"; 0; 12) ]# Write some textSet Variable [ $text ; Value: "The award winning MBS Plugin is easily the most powerful plug-in available..." ]Set Variable [ $r ; Value: MBS("DynaPDF.SetFillColor"; $pdf; 0; 0; 0) ]Set Variable [ $r ; Value: MBS("DynaPDF.WriteStyledTextEx"; $pdf; 10; 10; 300; -1; "justify"; $Text) ]# now figure out how much space we needed to make the page the right sizeSet Variable [ $height ; Value: MBS("DynaPDF.GetLastTextPosY"; $pdf) ]Set Variable [ $ph ; Value: MBS("DynaPDF.GetPageHeight"; $pdf) ]Set Variable [ $r ; Value: MBS( "DynaPDF.SetBBox"; $PDF; "Media"; 0; $ph; 320; $height - 10 ) ]# close page, document and save PDFSet Variable [ $r ; Value: MBS("DynaPDF.EndPage"; $pdf) ]If [ 0 ] # save as PDF Set Variable [ $Result ; Value: MBS("DynaPDF.Save"; $pdf; "hello.pdf") ]Else # or render a picture Set Variable [ $Result ; Value: MBS("DynaPDF.RenderPage"; $pdf; 1; 150) ]End IfSet Variable [ $r ; Value: MBS("DynaPDF.Release"; $pdf) ]# Put in ContainerSet Field [ Create Text::PDF ; $Result ] Let's make a change and calculate the size upfront. This allows use to draw a round rectangle before we draw the text. For this we first convert the styled text to DynaPDF's format commands. Then we calculate the height of the text box with the desired width of 300 points. Since we know the size of the textbox, we can change the page size to match what we need. We leave 10 points margin on all sizes and define the page size accordingly. On that small page, we can draw the round rectangle with a 5 point margin. You usually don't draw something round directly on the edge of the paper. It doesn't look nice in the viewers later with anti-aliased drawing. Then we draw the text. The new script looks like this: # Start with a new PDF documentSet Variable [ $pdf ; Value: MBS("DynaPDF.New") ]# Add pageSet Variable [ $r ; Value: MBS("DynaPDF.AppendPage"; $pdf) ]Set Variable [ $r ; Value: MBS("DynaPDF.SetFont"; $pdf; "Helvetica"; 0; 12) ]# Write some textSet Variable [ $text ; Value: "The award winning MBS Plugin is easily the most powerful plug-in available…" ]Set Variable [ $ftext ; Value: MBS( "DynaPDF.ConvertStyledText"; $PDF; $Text) ]# calculate heightSet Variable [ $width ; Value: 300 ]Set Variable [ $height ; Value: MBS( "DynaPDF.GetFTextHeightEx"; $PDF; $width; "justify"; $ftext ) ]# resize pageSet Variable [ $r ; Value: MBS( "DynaPDF.SetBBox"; $PDF; "Media"; 0; 0; $width + 20; $height + 20 ) ]Set Variable [ $r ; Value: MBS("DynaPDF.SetPageCoords"; $pdf; "topdown") ]# draw round rectSet Variable [ $r ; Value: MBS("DynaPDF.SetFillColor"; $pdf; 1; 1; 1) ]Set Variable [ $r ; Value: MBS("DynaPDF.SetStrokeColor"; $pdf; 1; ,8; ,8) ]Set Variable [ $r ; Value: MBS("DynaPDF.SetLineWidth"; $pdf; 3) ]Set Variable [ $r ; Value: MBS( "DynaPDF.RoundRectEx"; $PDF; 5; 5; $width + 10; $Height + 10; 10; 10; "FillStroke" ) ]# and draw textSet Variable [ $r ; Value: MBS("DynaPDF.SetFillColor"; $pdf; 0; 0; 0) ]Set Variable [ $r ; Value: MBS("DynaPDF.WriteFTextEx"; $pdf; 10; 10; $width; -1; "justify"; $ftext) ]# close page, document and save PDFSet Variable [ $r ; Value: MBS("DynaPDF.EndPage"; $pdf) ]If [ 0 ] # save as PDF Set Variable [ $Result ; Value: MBS("DynaPDF.Save"; $pdf; "hello.pdf") ]Else # or render a picture, PNG with transparency Set Variable [ $Result ; Value: MBS("DynaPDF.RenderPage"; $pdf; 1; 150; 0; 0; ""; "RGBA"; "Flate"; "PNG"; "image.png") ]End IfSet Variable [ $r ; Value: MBS("DynaPDF.Release"; $pdf) ]# Put in ContainerSet Field [ Create Text::PDF ; $Result ] The sample scripts are included with the Create Text as PDFA.fmp12 example file for the next version.
Create an account or sign in to comment