Exporting to PDF/A (C# / netframework)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\csharp\VS2022\netframework\25.Printing and Exporting\32.PDFA and also at https://github.com/tmssoftware/TMS-FlexCel.NET-demos/tree/master/csharp/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.cs
using System.Reflection;
using 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 - 2025 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("7.24.0.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.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using FlexCel.Core;
using FlexCel.XlsAdapter;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Drawing.Drawing2D;
using FlexCel.Pdf;
using FlexCel.Render;
namespace PDFA
{
/// <summary>
/// Exporting xls files to PDF/A.
/// </summary>
public partial class mainForm: System.Windows.Forms.Form
{
public mainForm()
{
InitializeComponent();
cbPdfType.SelectedIndex = 1;
}
private void button2_Click(object sender, System.EventArgs e)
{
Close();
}
private void export_Click(object sender, System.EventArgs e)
{
bool EmbedSource = cbEmbed.Checked;
TPdfType PdfType = GetPdfType();
TTagMode TagMode = GetTagMode();
if (EmbedSource)
{
if (PdfType != TPdfType.PDFA3 && PdfType != TPdfType.Standard)
{
MessageBox.Show("To embed a file, you need to use standard PDF or PDF/A3");
return;
}
}
if (exportDialog.ShowDialog() != DialogResult.OK)
{
return;
}
CreateFile(exportDialog.FileName, EmbedSource, PdfType, TagMode);
if (MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Process.Start(exportDialog.FileName);
}
}
private TPdfType GetPdfType()
{
switch (cbPdfType.SelectedIndex)
{
case 0: return TPdfType.Standard;
case 1:
case 2: return TPdfType.PDFA1;
case 3:
case 4: return TPdfType.PDFA2;
case 5:
case 6: return TPdfType.PDFA3;
}
throw new Exception("Unexpected PDF type");
}
private TTagMode GetTagMode()
{
switch (cbPdfType.SelectedIndex)
{
case 0:
case 1:
case 3:
case 5: return TTagMode.Full;
}
return TTagMode.None;
}
private void CreateFile(string FileName, bool EmbedSource, TPdfType PdfType, TTagMode TagMode)
{
ExcelFile xls = CreateSourceFile();
using (FlexCelPdfExport pdf = new FlexCelPdfExport(xls, true))
{
pdf.PdfType = PdfType;
pdf.TagMode = TagMode;
if (EmbedSource)
{
pdf.AttachFile("Report.xlsx", StandardMimeType.Xlsx, "This is the source file used to create the PDF", DateTime.Now, TPdfAttachmentKind.Source,
(attachWriter) =>
{
using (MemoryStream ms = new MemoryStream())
{
xls.Save(ms, TFileFormats.Xlsx);
ms.Position = 0;
attachWriter.Write(ms);
}
});
}
pdf.Export(FileName);
}
}
private ExcelFile CreateSourceFile()
{
ExcelFile xls = 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;
}
}
}
Form1.Designer.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using FlexCel.Core;
using FlexCel.XlsAdapter;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Drawing.Drawing2D;
using FlexCel.Pdf;
using System.Runtime.InteropServices;
namespace PDFA
{
public partial class mainForm: System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.SaveFileDialog exportDialog;
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#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 void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(mainForm));
this.panel1 = new System.Windows.Forms.Panel();
this.cbEmbed = new System.Windows.Forms.CheckBox();
this.cbPdfType = new System.Windows.Forms.ComboBox();
this.exportDialog = new System.Windows.Forms.SaveFileDialog();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.export = new System.Windows.Forms.ToolStripButton();
this.btnClose = new System.Windows.Forms.ToolStripButton();
this.panel1.SuspendLayout();
this.toolStrip1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.Controls.Add(this.cbEmbed);
this.panel1.Controls.Add(this.cbPdfType);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 38);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(372, 104);
this.panel1.TabIndex = 3;
//
// cbEmbed
//
this.cbEmbed.AutoSize = true;
this.cbEmbed.Location = new System.Drawing.Point(12, 54);
this.cbEmbed.Name = "cbEmbed";
this.cbEmbed.Size = new System.Drawing.Size(351, 17);
this.cbEmbed.TabIndex = 5;
this.cbEmbed.Text = "Embed xslx source file inside the PDF. (requires PDF/A3 or Standard)";
this.cbEmbed.UseVisualStyleBackColor = true;
//
// cbPdfType
//
this.cbPdfType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbPdfType.Items.AddRange(new object[] {
"Standard",
"PDF/A1a",
"PDF/A1b",
"PDF/A2a",
"PDF/A2b",
"PDF/A3a",
"PDF/A3b"});
this.cbPdfType.Location = new System.Drawing.Point(12, 14);
this.cbPdfType.Name = "cbPdfType";
this.cbPdfType.Size = new System.Drawing.Size(144, 21);
this.cbPdfType.TabIndex = 35;
//
// exportDialog
//
this.exportDialog.DefaultExt = "pdf";
this.exportDialog.Filter = "Pdf files|*.pdf";
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.export,
this.btnClose});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(372, 38);
this.toolStrip1.TabIndex = 4;
this.toolStrip1.Text = "mainToolbar";
//
// export
//
this.export.Image = ((System.Drawing.Image)(resources.GetObject("export.Image")));
this.export.ImageTransparentColor = System.Drawing.Color.Magenta;
this.export.Name = "export";
this.export.Size = new System.Drawing.Size(69, 35);
this.export.Text = "Create PDF";
this.export.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
this.export.Click += new System.EventHandler(this.export_Click);
//
// btnClose
//
this.btnClose.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
this.btnClose.Image = ((System.Drawing.Image)(resources.GetObject("btnClose.Image")));
this.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta;
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(59, 35);
this.btnClose.Text = " E&xit ";
this.btnClose.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
this.btnClose.Click += new System.EventHandler(this.button2_Click);
//
// mainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(372, 142);
this.Controls.Add(this.panel1);
this.Controls.Add(this.toolStrip1);
this.Name = "mainForm";
this.Text = "PDF/A";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private ToolStrip toolStrip1;
private ToolStripButton export;
private ToolStripButton btnClose;
private CheckBox cbEmbed;
private ComboBox cbPdfType;
}
}
Program.cs
using System;
using System.Windows.Forms;
namespace PDFA
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mainForm());
}
}
}