Exporting to PDF/A (VB.Net / netframework)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\vb\VS2022\netframework\25.Printing and Exporting\32.PDFA and also at https://github.com/tmssoftware/TMS-FlexCel.NET-demos/tree/master/vb/VS2022/netframework/Modules/25.Printing and Exporting/32.PDFA
Overview
FlexCel can export to PDF/A1, PDF/A2 and PDF/A3 in both their "a" and "b" variants.
Concepts
How to export a file to PDF/A
How to select between PDF/Ana and PDF/Anb
How to embed files in PDF/A3
Files
AssemblyInfo.vb
Imports System.Reflection
Imports System.Runtime.CompilerServices
'
' General Information about an assembly is controlled through the following
' set of attributes. Change these attribute values to modify the information
' associated with an assembly.
'
<Assembly: AssemblyTitle("")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyConfiguration("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("")>
<Assembly: AssemblyCopyright("(c) 2002 - 2014 TMS Software")>
<Assembly: AssemblyTrademark("")>
<Assembly: AssemblyCulture("")>
'
' Version information for an assembly consists of the following four values:
'
' Major Version
' Minor Version
' Build Number
' Revision
'
' You can specify all the values or you can default the Revision and Build Numbers
' by using the '*' as shown below:
<Assembly: AssemblyVersion("6.2.1.0")>
'
' In order to sign your assembly you must specify a key to use. Refer to the
' Microsoft .NET Framework documentation for more information on assembly signing.
'
' Use the attributes below to control which key is used for signing.
'
' Notes:
' (*) If no key is specified, the assembly is not signed.
' (*) KeyName refers to a key that has been installed in the Crypto Service
' Provider (CSP) on your machine. KeyFile refers to a file which contains
' a key.
' (*) If the KeyFile and the KeyName values are both specified, the
' following processing occurs:
' (1) If the KeyName can be found in the CSP, that key is used.
' (2) If the KeyName does not exist and the KeyFile does exist, the key
' in the KeyFile is installed into the CSP and used.
' (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
' When specifying the KeyFile, the location of the KeyFile should be
' relative to the project output directory which is
' %Project Directory%\obj\<configuration>. For example, if your KeyFile is
' located in the project directory, you would specify the AssemblyKeyFile
' attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
' (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
' documentation for more information on this.
'
<Assembly: AssemblyDelaySign(False)>
<Assembly: AssemblyKeyFile("")>
<Assembly: AssemblyKeyName("")>
Form1.Designer.vb
Imports System.Collections
Imports System.ComponentModel
Imports FlexCel.Core
Imports FlexCel.XlsAdapter
Imports System.IO
Imports System.Reflection
Imports System.Drawing.Drawing2D
Imports FlexCel.Pdf
Imports System.Runtime.InteropServices
Namespace PDFA
Partial Public Class mainForm
Inherits System.Windows.Forms.Form
Private panel1 As System.Windows.Forms.Panel
Private exportDialog As System.Windows.Forms.SaveFileDialog
Private components As System.ComponentModel.IContainer = Nothing
''' <summary>
''' Clean up any resources being used.
''' </summary>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
''' <summary>
''' Required method for Designer support - do not modify
''' the contents of this method with the code editor.
''' </summary>
Private Sub InitializeComponent()
Dim resources As New System.ComponentModel.ComponentResourceManager(GetType(mainForm))
Me.panel1 = New System.Windows.Forms.Panel()
Me.cbEmbed = New System.Windows.Forms.CheckBox()
Me.cbPdfType = New System.Windows.Forms.ComboBox()
Me.exportDialog = New System.Windows.Forms.SaveFileDialog()
Me.toolStrip1 = New System.Windows.Forms.ToolStrip()
Me.export = New System.Windows.Forms.ToolStripButton()
Me.btnClose = New System.Windows.Forms.ToolStripButton()
Me.panel1.SuspendLayout()
Me.toolStrip1.SuspendLayout()
Me.SuspendLayout()
'
' panel1
'
Me.panel1.BackColor = System.Drawing.Color.White
Me.panel1.Controls.Add(Me.cbEmbed)
Me.panel1.Controls.Add(Me.cbPdfType)
Me.panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.panel1.Location = New System.Drawing.Point(0, 38)
Me.panel1.Name = "panel1"
Me.panel1.Size = New System.Drawing.Size(372, 104)
Me.panel1.TabIndex = 3
'
' cbEmbed
'
Me.cbEmbed.AutoSize = True
Me.cbEmbed.Location = New System.Drawing.Point(12, 54)
Me.cbEmbed.Name = "cbEmbed"
Me.cbEmbed.Size = New System.Drawing.Size(351, 17)
Me.cbEmbed.TabIndex = 5
Me.cbEmbed.Text = "Embed xslx source file inside the PDF. (requires PDF/A3 or Standard)"
Me.cbEmbed.UseVisualStyleBackColor = True
'
' cbPdfType
'
Me.cbPdfType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cbPdfType.Items.AddRange(New Object() { "Standard", "PDF/A1a", "PDF/A1b", "PDF/A2a", "PDF/A2b", "PDF/A3a", "PDF/A3b"})
Me.cbPdfType.Location = New System.Drawing.Point(12, 14)
Me.cbPdfType.Name = "cbPdfType"
Me.cbPdfType.Size = New System.Drawing.Size(144, 21)
Me.cbPdfType.TabIndex = 35
'
' exportDialog
'
Me.exportDialog.DefaultExt = "pdf"
Me.exportDialog.Filter = "Pdf files|*.pdf"
'
' toolStrip1
'
Me.toolStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() { Me.export, Me.btnClose})
Me.toolStrip1.Location = New System.Drawing.Point(0, 0)
Me.toolStrip1.Name = "toolStrip1"
Me.toolStrip1.Size = New System.Drawing.Size(372, 38)
Me.toolStrip1.TabIndex = 4
Me.toolStrip1.Text = "mainToolbar"
'
' export
'
Me.export.Image = (CType(resources.GetObject("export.Image"), System.Drawing.Image))
Me.export.ImageTransparentColor = System.Drawing.Color.Magenta
Me.export.Name = "export"
Me.export.Size = New System.Drawing.Size(69, 35)
Me.export.Text = "Create PDF"
Me.export.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
' Me.export.Click += New System.EventHandler(Me.export_Click)
'
' btnClose
'
Me.btnClose.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.btnClose.Image = (CType(resources.GetObject("btnClose.Image"), System.Drawing.Image))
Me.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnClose.Name = "btnClose"
Me.btnClose.Size = New System.Drawing.Size(59, 35)
Me.btnClose.Text = " E&xit "
Me.btnClose.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
' Me.btnClose.Click += New System.EventHandler(Me.button2_Click)
'
' mainForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(372, 142)
Me.Controls.Add(Me.panel1)
Me.Controls.Add(Me.toolStrip1)
Me.Name = "mainForm"
Me.Text = "PDF/A"
Me.panel1.ResumeLayout(False)
Me.panel1.PerformLayout()
Me.toolStrip1.ResumeLayout(False)
Me.toolStrip1.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
Private toolStrip1 As ToolStrip
Private WithEvents export As ToolStripButton
Private WithEvents btnClose As ToolStripButton
Private cbEmbed As CheckBox
Private cbPdfType As ComboBox
End Class
End Namespace
Form1.vb
Imports System.Collections
Imports System.ComponentModel
Imports FlexCel.Core
Imports FlexCel.XlsAdapter
Imports System.IO
Imports System.Reflection
Imports System.Drawing.Drawing2D
Imports FlexCel.Pdf
Imports FlexCel.Render
Namespace PDFA
''' <summary>
''' Exporting xls files to PDF/A.
''' </summary>
Partial Public Class mainForm
Inherits System.Windows.Forms.Form
Public Sub New()
InitializeComponent()
cbPdfType.SelectedIndex = 1
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
Close()
End Sub
Private Sub export_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles export.Click
Dim EmbedSource As Boolean = cbEmbed.Checked
Dim PdfType As TPdfType = GetPdfType()
Dim TagMode As TTagMode = GetTagMode()
If EmbedSource Then
If PdfType <> TPdfType.PDFA3 AndAlso PdfType <> TPdfType.Standard Then
MessageBox.Show("To embed a file, you need to use standard PDF or PDF/A3")
Return
End If
End If
If exportDialog.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then
Return
End If
CreateFile(exportDialog.FileName, EmbedSource, PdfType, TagMode)
If MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.Yes Then
Process.Start(exportDialog.FileName)
End If
End Sub
Private Function GetPdfType() As TPdfType
Select Case cbPdfType.SelectedIndex
Case 0
Return TPdfType.Standard
Case 1, 2
Return TPdfType.PDFA1
Case 3, 4
Return TPdfType.PDFA2
Case 5, 6
Return TPdfType.PDFA3
End Select
Throw New Exception("Unexpected PDF type")
End Function
Private Function GetTagMode() As TTagMode
Select Case cbPdfType.SelectedIndex
Case 0, 1, 3, 5
Return TTagMode.Full
End Select
Return TTagMode.None
End Function
Private Sub CreateFile(ByVal FileName As String, ByVal EmbedSource As Boolean, ByVal PdfType As TPdfType, ByVal TagMode As TTagMode)
Dim xls As ExcelFile = CreateSourceFile()
Using pdf As New FlexCelPdfExport(xls, True)
pdf.PdfType = PdfType
pdf.TagMode = TagMode
If EmbedSource Then
pdf.AttachFile("Report.xlsx", StandardMimeType.Xlsx, "This is the source file used to create the PDF", Date.Now, TPdfAttachmentKind.Source, _
Sub(attachWriter)
Using ms As New MemoryStream()
xls.Save(ms, TFileFormats.Xlsx)
ms.Position = 0
attachWriter.Write(ms)
End Using
End Sub)
End If
pdf.Export(FileName)
End Using
End Sub
Private Function CreateSourceFile() As ExcelFile
Dim xls As ExcelFile = New XlsFile()
xls.NewFile(1, TExcelFileFormat.v2019)
xls.SetCellValue(1, 1, "This is a test from FlexCel!")
xls.SetCellValue(2, 1, "Here is some emoji to show unicode surrogate support: 🐜🐏")
xls.SetCellValue(3, 1, "You might need a font able to show emoji for those characters to show")
xls.SetCellValue(4, 1, "Windows 7 and 8 have SegoeUISymbol, which can show them and is used automatically by FlexCel.")
Return xls
End Function
End Class
End Namespace
Program.vb
Namespace PDFA
Friend NotInheritable Class Program
Private Sub New()
End Sub
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New mainForm())
End Sub
End Class
End Namespace