Platforms to show: All Mac Windows Linux Cross-Platform

/DynaPDF/PDFPreview Window


Required plugins for this example: MBS MacCG Plugin, MBS DynaPDF Plugin, MBS MacCF Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/PDFPreview Window

This example is the version from Sat, 20th Oct 2017.

Project "PDFPreview Window.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub NewDocument() dim f as FolderItem = GetOpenFolderItem(FileTypes1.Pdf) if f<>Nil then OpenDocument f end if End EventHandler
EventHandler Sub OpenDocument(item As FolderItem) PDFPreview.run item End EventHandler
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Class PDFPreview Inherits Window
Const StepSize = 5
Control Out Inherits Canvas
ControlInstance Out Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) // Calculate scale factor dim faktor as Double = min( g.Height / Pic.Height, g.Width / Pic.Width) if faktor>1 then faktor = 1 end if // Calculate new size dim w as integer = Pic.Width * faktor dim h as integer = Pic.Height * faktor dim x as integer = (g.Width - w)/2 dim y as integer = (g.height - h)/2 g.clearRect 0, 0, g.width, g.height g.DrawPicture Pic, x, y, w, h, 0, 0, Pic.Width, Pic.Height End EventHandler
End Control
Control SaveButton Inherits PushButton
ControlInstance SaveButton Inherits PushButton
EventHandler Sub Action() dim f as FolderItem = GetSaveFolderItem(FileTypes1.Pdf, "output.pdf") if f = nil then Return dim b as BinaryStream = BinaryStream.Create(f, true) b.Write PDFData b.Close Exception io as IOException MsgBox "Failed to write file." End EventHandler
End Control
Control PrintButton Inherits PushButton
ControlInstance PrintButton Inherits PushButton
EventHandler Sub Action() #if TargetMachO then PrintMac true #else MsgBox "Only for Mac in this demo." #endif End EventHandler
End Control
Control CloseButton Inherits PushButton
ControlInstance CloseButton Inherits PushButton
EventHandler Sub Action() close End EventHandler
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
EventHandler Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) As Boolean if row < me.ListCount then dim LineV as Variant = me.CellTag(row,0) dim PageIndex as integer = me.RowTag(row) // selected? if me.ListIndex >= 0 then dim SelectedPageIndex as integer = me.RowTag(me.ListIndex) if SelectedPageIndex = PageIndex then g.ForeColor = HighlightColor g.FillRect 0, 0, g.Width, g.Height else g.ForeColor = &cFFFFFF g.FillRect 0, 0, g.Width, g.Height end if end if // draw page if linev<>Nil then dim p as Picture = pages(PageIndex) if p<>Nil then dim w as integer = p.Width dim xx as integer = (g.Width -w)/2 g.DrawPicture p, xx, 0, w, StepSize, 0, LineV.IntegerValue, w, StepSize end if end if Return true end if End EventHandler
EventHandler Sub Change() if me.ListIndex >= 0 then dim v as Variant = me.RowTag(me.ListIndex) if v <> nil then RenderPage v+1 out.Invalidate end if '#if RBVersion < 2013 then // fix redrawing issues for i as integer = 0 to me.ListCount-1 me.InvalidateCell(i,0) next '#else 'me.Invalidate '#endif end if End EventHandler
End Control
Control UpdateTimer Inherits Timer
ControlInstance UpdateTimer Inherits Timer
EventHandler Sub Action() while UBound(NewPages)>=0 dim p as Picture = NewPages(0) NewPages.Remove 0 pages.Append p dim v as Variant = UBound(pages) List.AddRow "" List.RowTag(List.LastIndex) = v dim h as integer = p.Height-1 for i as integer = 0 to h step StepSize List.AddRow "" List.RowTag(List.LastIndex) = v List.CellTag(List.LastIndex,0) = i next List.AddRow "" List.RowTag(List.LastIndex) = v wend End EventHandler
End Control
Control Thread1 Inherits Thread
ControlInstance Thread1 Inherits Thread
EventHandler Sub Run() // render page previews dim c as integer = pdf.GetPageCount for i as integer = 1 to c me.Sleep(10) dim page as DynaPDFPageMBS = pdf.GetPage(i) dim mr as DynaPDFRectMBS = page.BBox(page.kpbMediaBox) dim w as integer = 148 dim h as integer = 198 dim pic as Picture = pdf.RenderPagePicture(i, w, h, pdf.kpsFitBest) newpages.Append pic next End EventHandler
End Control
EventHandler Sub Open() list.DefaultRowHeight = StepSize End EventHandler
Function FileClose() As Boolean close Return True End Function
Sub PrintMac(ShowDialog as Boolean) #if TargetMacOS then // Print PDF on Mac // requires MacOSXCG plugin dim p as CGPDFDocumentMBS if PDFFile <> nil then p = CGPDFDocumentMBS.CreateWithFile(PDFFile) else p = CGPDFDocumentMBS.CreateWithData(PDFData) end if if p=nil then MsgBox "Failed to read the PDF." Return end if dim g as Graphics if ShowDialog then g = OpenPrinterDialog else g = OpenPrinter end if if g<>Nil then dim c as integer=p.PageCount for i as integer=1 to c g.DrawCGPDFDocumentMBS p, p.CropBox(i), i if i<c then g.NextPage end if next end if #endif End Sub
Sub RenderPage(n as integer) dim page as DynaPDFPageMBS = pdf.GetPage(n) dim mr as DynaPDFRectMBS = page.BBox(page.kpbMediaBox) dim w as integer = mr.Width dim h as integer = abs(mr.Bottom-mr.top) pic = pdf.RenderPagePicture(n, w, h, pdf.kpsFitBest) End Sub
Function RenderPreview() As Boolean pdf = new MyDynaPDFMBS if not pdf.CreateNewPDF(nil) then // create pdf in memory Return false end if dim e as integer if PDFFile = nil then e = pdf.OpenImportBuffer(PDFData) // load from memory else e = pdf.OpenImportFile(PDFFile) // load from file end if if e<>0 then // failed Return false end if call pdf.SetImportFlags(pdf.kifImportAsPage) // important! Import as page makes the rendering faster. e = pdf.ImportPDFFile(1,1.0,1.0) if e < 0 then Return false end if RenderPage 1 Thread1.run Return true End Function
Shared Sub Run(file as FolderItem) dim p as new PDFPreview p.PDFFile = file if p.RenderPreview then p.show else p.close end if End Sub
Shared Sub Run(PDFData as string) dim p as new PDFPreview p.PDFData = PDFData if p.RenderPreview then p.show else p.close end if End Sub
Property NewPages() As Picture
Property PDFData As string
Property PDFFile As FolderItem
Property Pic As Picture
Property pages() As picture
Property pdf As MyDynaPDFMBS
End Class
FileTypes1
Filetype application/pdf
End FileTypes1
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 if ErrorCode = 116 then // WriteFText cancelled Return 0 end if if ErrorCode = 63 then // Type1 Font nicht unterstützt Return 0 end if // 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
EventHandler Function PageBreak(LastPosX as double, LastPosY as double, PageBreak as boolean) As integer Return -1 End EventHandler
Property IgnoreWarnings As Boolean
End Class
End Project

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


The biggest plugin in space...