Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Platforms to show: All Mac Windows Linux Cross-Platform

/DynaPDF/Create PDF with Annotations
Function:
Required plugins for this example: MBS DynaPDF Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Create PDF with Annotations
This example is the version from Sun, 23th Sep 2017.
Project "Create PDF with Annotations.rbp"
FileTypes
Filetype text
End FileTypes
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
EventHandler Sub Open() dim pdf as new MyDynapdfMBS dim d as new date dim f as FolderItem = SpecialFolder.Desktop.Child("Create PDF with Annotations.pdf") pdf.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License call pdf.CreateNewPDF f call pdf.SetDocInfo pdf.kdiSubject, "My first Xojo output" call pdf.SetDocInfo pdf.kdiProducer, "Xojo test application" call pdf.SetDocInfo pdf.kdiTitle, "My first Xojo output" // We want to use top-down coordinates call pdf.SetPageCoords pdf.kpcTopDown call pdf.Append dim y as Double = 50 call pdf.SetFont( "Helvetica", pdf.kfsRegular, 20.0, false, pdf.kcp1252) AddHighlightAnnot pdf, pdf.katHighlight, pdf.kPDF_YELLOW, 50.0, y, "Highlight Annotation", "Highlight Annotations", "This is a highlight annotation" AddHighlightAnnot pdf, pdf.katSquiggly, pdf.kPDF_RED, 300.0, y, "Squiggly Annotation", "Highlight Annotations", "This is a squiggly annotation" y = y + 30.0 AddHighlightAnnot pdf, pdf.katStrikeOut, pdf.kPDF_RED, 50.0, y, "Strikeout Annotation", "Highlight Annotations", "This is a strikeout annotation" AddHighlightAnnot pdf, pdf.katUnderline, pdf.kPDF_RED, 300.0, y, "Underline Annotation", "Highlight Annotations", "This is a underline annotation" y = y + 40.0 call pdf.CircleAnnot( 50.0, y, 200.0, 100.0, 1.0, pdf.kPDF_CREAM, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Circle Annotations", "This is a circle annotation") call pdf.SquareAnnot(300.0, y, 200.0, 100.0, 1.0, pdf.kPDF_CREAM, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Square Annotations", "This is a square annotation") y = y + 130.0 call pdf.ChangeFontSize(12.0) call pdf.WriteFTextEx(50.0, y, pdf.GetPageWidth - 100.0, -1.0, pdf.ktaLeft, _ "The icon color of text and file attachment annotations can be changed if "+_ "necessary with SetAnnotColor(). The background color must be set.\n\nText Annotations:") y = pdf.GetPageHeight - pdf.GetLastTextPosY + 10.0 dim a as integer // The default icon color can be changed if necessary call pdf.TextAnnot( 50.0, y, 200.0, 100.0, "Test app", "This is a text annotation", pdf.kaiComment, false) a = pdf.TextAnnot(100.0, y, 200.0, 100.0, "Test app", "This is a text annotation", pdf.kaiHelp, false) call pdf.SetAnnotColor(a, pdf.kfcBackColor, pdf.kcsDeviceRGB, PDF.RGB(200, 20, 30)) call pdf.TextAnnot(150.0, y, 200.0, 100.0, "Test app", "This is a text annotation", pdf.kaiInsert, false) a = pdf.TextAnnot(200.0, y, 200.0, 100.0, "Test app", "This is a text annotation", pdf.kaiKey, false) call pdf.SetAnnotColor(a, pdf.kfcBackColor, pdf.kcsDeviceRGB, PDF.RGB(50, 200, 30)) call pdf.TextAnnot(250.0, y, 200.0, 100.0, "Test app", "This is a text annotation", pdf.kaiNewParagraph, false) a = pdf.TextAnnot(300.0, y, 200.0, 100.0, "Test app", "This is a text annotation", pdf.kaiNote, false) call pdf.SetAnnotColor(a, pdf.kfcBackColor, pdf.kcsDeviceRGB, PDF.RGB(70, 120, 210)) call pdf.TextAnnot( 350.0, y, 200.0, 100.0, "Test app", "This is a text annotation", pdf.kaiParagraph, false) y = y + 50.0 call pdf.WriteText(50.0, y, "File Attachment Annotations:") y = y + 20.0 dim path as FolderItem = FindFile("gdi.emf") call pdf.FileAttachAnnot( 50.0, y, pdf.kfaiGraph, "Test app", "An example attachment", path, true) call pdf.FileAttachAnnot(100.0, y, pdf.kfaiPaperClip, "Test app", "An example attachment", path, true) a = pdf.FileAttachAnnot(150.0, y, pdf.kfaiPushPin, "Test app", "An example attachment", path, true) call pdf.SetAnnotColor(a, pdf.kfcBackColor, pdf.kcsDeviceRGB, PDF.RGB(70, 120, 210)) call pdf.FileAttachAnnot(200.0, y, pdf.kfaiTag, "Test app", "An example attachment", path, true) y = y + 60.0 a = pdf.FreeTextAnnot(50.0, y, 300.0, 80.0, "Test app", "This is a FreeText Annotation.", pdf.ktaCenter) call pdf.SetAnnotBorderWidth(a, 3.0) call pdf.SetAnnotColor(a, pdf.kfcBorderColor, pdf.kcsDeviceRGB, pdf.kPDF_GRAY) y = y + 120.0 call pdf.WriteText(50.0, y, "Line Annotations:") y = y + 30.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleNone, pdf.kleNone, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") y = y + 20.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleButt, pdf.kleButt, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") y = y + 20.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleCircle, pdf.kleCircle, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") y = y + 20.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleClosedArrow, pdf.kleClosedArrow, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") y = y + 20.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleRClosedArrow, pdf.kleRClosedArrow, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") y = y + 20.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleDiamond, pdf.kleDiamond, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") y = y + 20.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleOpenArrow, pdf.kleOpenArrow, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") y = y + 20.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleROpenArrow, pdf.kleROpenArrow, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") y = y + 20.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleSlash, pdf.kleSlash, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") y = y + 20.0 call pdf.LineAnnot(50.0, y, 350.0, y, 1.0, pdf.kleSquare, pdf.kleSquare, pdf.kPDF_RED, pdf.kPDF_BLACK, pdf.kcsDeviceRGB, "Test app", "Line Annotations", "This is a line annotation") call pdf.EndPage call pdf.Append dim points() as DynapdfPointMBS points.Append new DynapdfPointMBS(50.0, 200.0) points.Append new DynapdfPointMBS(50.0, 100.0) points.Append new DynapdfPointMBS(150.0, 50.0) points.Append new DynapdfPointMBS(250.0, 100.0) points.Append new DynapdfPointMBS(250.0, 200.0) dim count as integer = points.Ubound + 1 call pdf.PolyLineAnnot(points, 1.0, pdf.kleDiamond, pdf.kleCircle, pdf.kPDF_WHITE, pdf.kPDF_RED, pdf.kcsDeviceRGB, "Test app", "Polyline Annotations", "This is a polyline annotation") for i as integer = 0 to points.Ubound points(i).x = points(i).x + 250.0 next call pdf.PolygonAnnot(points, 1.0, pdf.kPDF_CREAM, pdf.kPDF_RED, pdf.kcsDeviceRGB, "Test app", "Polygon Annotations", "This is a polygon annotation") for i as integer = 0 to points.Ubound points(i).x = points(i).x - 250.0 points(i).y = points(i).x + 230.0 next y = 230.0 call pdf.SetFont("Helvetica", pdf.kfsRegular, 12.0, false, pdf.kcp1252) call pdf.WriteFTextEx(50.0, y, pdf.GetPageWidth - 100.0, -1.0, pdf.ktaLeft, "The points of an Ink Annotation will be approximated with bezier curves." +_ "The result is a smooth path. A polyline annotation is almost the same annotation type without the approximation with bezier curves.") call pdf.InkAnnot(points, 1.0, pdf.kPDF_BLUE, pdf.kcsDeviceRGB, "Test app", "Ink Annotations", "This is an ink annotation") y = y + 230.0 call pdf.WriteText(50.0, y, "Stamp Annotations:") y = y + 20.0 call pdf.WriteFTextEx(50.0, y, pdf.GetPageWidth - 100.0, -1.0, pdf.ktaLeft, "The default color of a stamp annotation can be changed with SetAnnotColor(). The border or text color must be set.") y = pdf.GetPageHeight - pdf.GetLastTextPosY + 10.0 call pdf.StampAnnot(pdf.krsApproved, 50.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") call pdf.StampAnnot(pdf.krsAsIs, 220.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") call pdf.StampAnnot(pdf.krsConfidential, 390.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") y = y + 60.0 call pdf.StampAnnot(pdf.krsDepartmental, 50.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") call pdf.StampAnnot(pdf.krsDraft, 220.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") call pdf.StampAnnot(pdf.krsExperimental, 390.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") y = y + 60.0 call pdf.StampAnnot(pdf.krsExpired, 50.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") a = pdf.StampAnnot(pdf.krsFinal, 220.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") call pdf.SetAnnotColor(a, pdf.kfcBorderColor, pdf.kcsDeviceRGB, PDF.RGB(170, 10, 160)) call pdf.StampAnnot(pdf.krsForComment, 390.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") y = y + 60.0 call pdf.StampAnnot(pdf.krsForPublicRelease, 50.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") call pdf.StampAnnot(pdf.krsNotApproved, 220.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") call pdf.StampAnnot(pdf.krsNotForPublicRelease, 390.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") y = y + 60.0 call pdf.StampAnnot(pdf.krsSold, 50.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") call pdf.StampAnnot(pdf.krsTopSecret, 220.0, y, 150.0, 50.0, "Test app", "Stamp Annotations", "This is a stamp annotation") call pdf.EndPage call pdf.CloseFile f.Launch quit End EventHandler
Sub AddHighlightAnnot(pdf as DynaPDFMBS, Type as Integer, ColorValue as UInt32, x as Double, y as Double, Text as string, subject as string, comment as string) dim w as Double = pdf.GetTextWidth(text) call pdf.WriteText x, y, text call pdf.HighlightAnnot(type, x, y + pdf.GetDescent, w, 20.0, ColorValue, "Test app", Subject, Comment) End Sub
Function FindFile(name as string) As FolderItem // Look for file in parent folders from executable on dim parent as FolderItem = app.ExecutableFile.Parent while parent<>Nil dim file as FolderItem = parent.Child(name) if file<>Nil and file.Exists then Return file end if parent = parent.Parent wend End Function
End Class
Class MyDynaPDFMBS Inherits DynaPDFMBS
EventHandler Function Error(ErrorCode as integer, ErrorMessage as string, ErrorType as integer) As integer // output all messages on the console: System.DebugLog str(ErrorCode)+": "+ErrorMessage // and display dialog: Dim d as New MessageDialog //declare the MessageDialog object Dim b as MessageDialogButton //for handling the result d.icon=MessageDialog.GraphicCaution //display warning icon d.ActionButton.Caption="Continue" d.CancelButton.Visible=True //show the Cancel button // a warning or an error? if BitAnd(ErrorType, me.kE_WARNING) = me.kE_WARNING then // if user decided to ignore, we'll ignore if IgnoreWarnings then Return 0 d.Message="A warning occurred while processing your PDF code." // we add a third button to display all warnings d.AlternateActionButton.Caption = "Ignore warnings" d.AlternateActionButton.Visible = true else d.Message="An error occurred while processing your PDF code." end if d.Explanation = str(ErrorCode)+": "+ErrorMessage b=d.ShowModal //display the dialog Select Case b //determine which button was pressed. Case d.ActionButton Return 0 // ignore Case d.AlternateActionButton IgnoreWarnings = true Return 0 // ignore Case d.CancelButton Return -1 // stop End select End EventHandler
Property IgnoreWarnings As Boolean
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS DynaPDF Plugin.




Links
MBS Xojo PDF Plugins