Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/DynaPDF/Optimize PDF
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/Optimize PDF
This example is the version from Thu, 20th Jul 2016.
Project "Optimize PDF.rbp"
FileTypes
Filetype text
Filetype application/pdf
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 NewDocument() dim f as FolderItem = GetOpenFolderItem(FileTypes.Pdf) if f<>nil then OpenDocument f end if End EventHandler
EventHandler Sub OpenDocument(item As FolderItem) process item End EventHandler
Sub Process(InputFile as FolderItem) if InputFile = nil then Return dim OutputFile as FolderItem = GetSaveFolderItem(FileTypes.Pdf, InputFile.name) if OutputFile = nil then Return dim pdf as new MyDynapdfMBS 'pdf.SetLicenseKey "Lite" // For this example you can use a Lite, Pro or Enterprise License call pdf.CreateNewPDF OutputFile /// If the imported document should be edited then avoid // the conversion of pages to templates (see SetImportFlags() // for further information). call pdf.SetImportFlags(bitwiseOr(pdf.kifImportAll, pdf.kifImportAsPage)) // import and remove duplicates in images, colorspaces, templates and pattern // The flag if2UseProxy reduces the memory usage. The duplicate check is optional but recommended. call pdf.SetImportFlags2(bitwiseOr(pdf.kif2UseProxy, pdf.kif2DuplicateCheck)) // Import the PDF file so that it can be edited call pdf.OpenImportFile(InputFile) dim pageCount as integer = pdf.ImportPDFFile(1, 1.0, 1.0) // If DestPage is 1, the return value is the new number of pages call pdf.CloseImportFile // It is not allowed to call this function twice for the same document in memory! // In addition, we can either optimize the entire PDF file or nothing. // Note that this function requires the DynaPDF Pro. dim flags as integer = Bitwise.BitOr(_ pdf.kofInMemory, _ // Optimize the file fully in memory. Only useful for small PDF files. pdf.kofScaleImages, _ // Scale images as specified in the DynaPDFOptimizeParamsMBS class. pdf.kofNewLinkNames, _ // If set, rename all object links to short names like F1, F2 etc. pdf.kofDeleteInvPaths, _ // Delete invisible paths. pdf.kofDeletePrivateData, _ // Delete private data objects from pages, templates, and images. pdf.kofIgnoreZeroLineWidth, _ pdf.kofDeleteAlternateImages, _ // If set, alternate images will be deleted. pdf.kofDeleteThumbnails) // Thumbnails can be deleted since PDF viewers can create thumbnails easily on demand. // pdf.kofInMemory // Optimize the file fully in memory. Only useful for small PDF files. // pdf.kofConvertAllColors // If set, Separation, DeviceN, and NChannel color spaces will be converted to the device space. // pdf.kofIgnoreICCBased // If set, ICCBased color spaces will be left unchanged. // pdf.kofScaleImages // Scale all images // pdf.kofNewLinkNames // If set, rename all object links to short names like F1, F2 etc. // pdf.kofDeleteInvPaths // Delete invisible paths. An invisible path is a path that was finished with the no-op operator "n". dim o as new DynaPDFOptimizeParamsMBS o.MinColorRes = 100 // scale images with more than 100 dpi o.ResColorImages = 150 // down to 150 dpi call pdf.Optimize(flags, o) call pdf.CloseFile dim InputLen as integer = InputFile.length dim OutputLen as integer = OutputFile.Length MsgBox str(InputLen,"0")+" before, "+str(OutputLen, "0")+" after, "+str(InputLen - OutputLen, "0")+ " saved." quit End Sub
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

Feedback, Comments & Corrections

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





Links
MBS Xojo blog