Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/ZUGFeRD 2.0/Create PDF with existing pdf
Required plugins for this example: MBS DynaPDF Plugin, MBS Images Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/ZUGFeRD 2.0/Create PDF with existing pdf
This example is the version from Wed, 28th Dec 2021.
Project "Create PDF with existing pdf.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu5 = ""
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
MenuItem UntitledMenu4 = ""
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = ""
End MenuBar
Class App Inherits Application
EventHandler Sub Open()
// for testing we just create RGB profile
dim p1 as LCMS2ProfileMBS = LCMS2ProfileMBS.CreatesRGBProfile
dim RGBProfileFile as FolderItem = SpecialFolder.Desktop.Child("test rgb.icc")
call p1.SaveProfileToFile(RGBProfileFile)
// for testing we just create gray profile
dim p2 as LCMS2ProfileMBS = LCMS2ProfileMBS.CreateGrayProfile(nil, nil, nil)
dim GrayProfileFile as FolderItem = SpecialFolder.Desktop.Child("test gray.icc")
call p2.SaveProfileToFile(GrayProfileFile)
dim pdf as new MyDynapdfMBS
dim f as FolderItem = SpecialFolder.Desktop.Child("Create PDF.pdf")
pdf.SetLicenseKey "Pro" // For this example you can use a Pro or Enterprise License
// now create PDF
call pdf.CreateNewPDF f
// PDF/A requires a language set
Call pdf.SetLanguage("en-US")
// PDF/A requires a structure tree
Call pdf.CreateStructureTree
// prepare for PDF/A
Dim flags As Integer
flags = Bitwise.BitOr(flags, pdf.kifImportAll)
flags = Bitwise.BitOr(flags, pdf.kifImportAsPage)
flags = Bitwise.BitOr(flags, pdf.kifPrepareForPDFA)
call pdf.SetImportFlags(flags)
// import PDF file
Dim ImportFile As FolderItem = MyDynaPDFMBS.FindFile("invoice template.pdf")
call pdf.OpenImportFile(ImportFile)
call pdf.ImportPDFFile(1)
// now add xml
Dim file As FolderItem = MyDynaPDFMBS.FindFile("ZUGFeRD-invoice.xml")
dim n as integer = pdf.AttachFile(file, "ZUGFeRD Rechnung", false)
if not pdf.AssociateEmbFile(pdf.kadCatalog, -1, pdf.karAlternative, n) then
Break // error
end if
// make sure we conform
// for perfect usage, you need PDF/A extension for DynaPDF (extra purchase)
// here we pass Basic level. Please make sure XML and level here match!
Dim retval As Integer = pdf.CheckConformance(pdf.kctZUGFeRD2_Basic, pdf.kcoDefault)
Select case retval
case 1
'MsgBox "RGB"
call pdf.AddOutputIntent(RGBProfileFile) // RGB
case 2
'call pdf.AddOutputIntent(profilefile) // CMYK
break
case 3
call pdf.AddOutputIntent(GrayProfileFile) // Gray
'break
end Select
call pdf.CloseFile
f.Launch
quit
End EventHandler
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
EventHandler Function OnFontNotFound(PDFFontRef as integer, FontName as string, Style as integer, StdFontIndex as integer, IsSymbolFont as boolean) As integer
// Here you could use your own mapping table.
// In this example we replace the font simply with Arial
if (WeightFromStyle(Style) < 500) then
// Only the weights 500 and 700 of Arial are installed
// by default. If you have also light variants then it is
// not required to change the style.
Style = BitwiseAnd(Style, &h0F)
Style = BitwiseOr(Style, kfsRegular)
end if
return ReplaceFont(PDFFontRef, "Arial", Style, true)
End EventHandler
EventHandler Function OnReplaceICCProfile(Type as integer, ColorSpace as integer) As integer
// provide missing ICC Profiles to DynaPDF
// The ICC profiles which should normally be configured by the user.
Dim filename As String
Select Case type
Case Me.kictGray
filename = "Generic Gray Profile.icc"
Case Me.kictRGB
filename = "sRGB.icc"
Case Me.kictCMYK
filename = "Generic CMYK Profile.icc"
Case Me.kictLab
// not yet needed, but maybe in future
filename = "Generic Lab Profile.icc"
Else
Break
Return -1 // new type we don't know?
End Select
Dim f As FolderItem = FindFile(filename)
If f = Nil Or Not f.Exists Then
// file missing?
Return -1
End If
Dim e As Integer = ReplaceICCProfile(ColorSpace, f)
If e < 0 Then
// failed
Break
End If
// pass along success or failure
Return e
End EventHandler
Shared 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
Property IgnoreWarnings As Boolean
End Class
End Project
See also:
The items on this page are in the following plugins: MBS DynaPDF Plugin.
