PDF/A is an ISO-standardized version of PDF that ensures the long-term preservation of digital documents. It restricts certain features of the regular PDF format to ensure that documents can be accessed and read in the future, even if the software or hardware used to create them is no longer available. In certain cases, it’s necessary to convert PDF files to PDF/A format for long-term archiving purposes. However, there may also be a need to convert PDF/A files back to regular PDF format if the restrictions and standards of PDF/A are no longer required, or if there is a need to edit or modify the files. This article will explain how to convert PDF files to PDF/A or convert PDF/A files to PDF in C# and VB.NET.
Installation
To achieve conversions between PDF and PDF/A, this article uses Spire.PDF for .NET library. You can install Spire.PDF for .NET via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console, and then executing the following command:
PM> Install-Package Spire.PDF
Alternatively, you can also download Spire.PDF for .NET from its official website, extract the package and then add the DLL files under the Bin folder to your project as references.
Convert PDF to PDF/A in C# and VB.NET
The Spire.PDF library provides the PdfStandardsConverter class, which is specifically designed for converting PDF files to PDF/A and PDF/X-1a:2001 formats. This class offers a range of methods that enable you to convert regular PDF files to different versions of PDF/A, including:
- PDF/A-1a
- PDF/A-1b
- PDF/A-2a
- PDF/A-2b
- PDF/A-3a
- PDF/A-3b
The following steps describe how to convert a regular PDF file to different versions of PDF/A:
- Initialize an instance of the PdfStandardsConverter class, and pass the PDF file path to the constructor of the class as a parameter.
- Convert the PDF file to Pdf/A-1a and save the result file to a specific location using the PdfStandardsConverter.ToPdfA1A(string filePath) method.
- Convert the PDF file to Pdf/A-1b and save the result file to a specific location using the PdfStandardsConverter.ToPdfA1B(string filePath) method.
- Convert the PDF file to Pdf/A-2a and save the result file to a specific location using the PdfStandardsConverter.ToPdfA2A(string filePath) method.
- Convert the PDF file to Pdf/A-2b and save the result file to a specific location using the PdfStandardsConverter.ToPdfA2B(string filePath) method.
- Convert the PDF file to Pdf/A-3a and save the result file to a specific location using the PdfStandardsConverter.ToPdfA3A(string filePath) method.
- Convert the PDF file to Pdf/A-3b and save the result file to a specific location using the PdfStandardsConverter.ToPdfA3B(string filePath) method.
C#
using Spire.Pdf.Conversion;
namespace ConvertPdfToPdfA
{
internal class Program
{
static void Main(string[] args)
{
//Specify the input PDF’s file path
string inputFile = @"C:\Users\Administrator\Desktop\sample.pdf";
//Specify the output folder
string outputFolder = @"C:\Users\Administrator\Desktop\Output\";
//Initialize an instance of the PdfStandardsConverter class, and pass the input PDF’s file path to the constructor of the class as a parameter
PdfStandardsConverter converter = new PdfStandardsConverter(inputFile);
//Convert the PDF file to PdfA1A
converter.ToPdfA1A(outputFolder + "ToPdfA1A.pdf");
//Convert the PDF file to PdfA1B
converter.ToPdfA1B(outputFolder + "ToPdfA1B.pdf");
//Convert the PDF file to PdfA2A
converter.ToPdfA2A(outputFolder + "ToPdfA2A.pdf");
//Convert the PDF file to PdfA2B
converter.ToPdfA2B(outputFolder + "ToPdfA2B.pdf");
//Convert the PDF file to PdfA3A
converter.ToPdfA3A(outputFolder + "ToPdfA3A.pdf");
//Convert the PDF file to PdfA3B
converter.ToPdfA3B(outputFolder + "ToPdfA3B.pdf");
}
}
}
VB.NET
Imports Spire.Pdf.Conversion
Namespace ConvertPdfToPdfA
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Specify the input PDF’s file path
Dim inputFile = "C:\Users\Administrator\Desktop\sample.pdf"
'Specify the output folder
Dim outputFolder = "C:\Users\Administrator\Desktop\Output\"
'Initialize an instance of the PdfStandardsConverter class, and pass the input PDF’s file path to the constructor of the class as a parameter
Dim converter As PdfStandardsConverter = New PdfStandardsConverter(inputFile)
'Convert the PDF file to PdfA1A
converter.ToPdfA1A(outputFolder & "ToPdfA1A.pdf")
'Convert the PDF file to PdfA1B
converter.ToPdfA1B(outputFolder & "ToPdfA1B.pdf")
'Convert the PDF file to PdfA2A
converter.ToPdfA2A(outputFolder & "ToPdfA2A.pdf")
'Convert the PDF file to PdfA2B
converter.ToPdfA2B(outputFolder & "ToPdfA2B.pdf")
'Convert the PDF file to PdfA3A
converter.ToPdfA3A(outputFolder & "ToPdfA3A.pdf")
'Convert the PDF file to PdfA3B
converter.ToPdfA3B(outputFolder & "ToPdfA3B.pdf")
End Sub
End Class
End Namespace
Convert PDF/A to PDF in C# and VB.NET
The way to convert a PDF/A file to regular PDF format is to create a new regular PDF, then draw the content of the PDF/A file into the new PDF.
The following steps describe how to convert a PDF/A file to regular PDF format:
- Initialize an instance of the PdfDocument class.
- Load the PDF/A file using the PdfDocument.LoadFromFile(string filePath) method.
- Initialize an instance of the PdfNewDocument class to create a new PDF file.
- Iterate through all pages in the PDF/A file.
- Add pages to the new PDF file using the PdfDocumentBase.Pages.Add(SizeF size, PdfMargins margins) method.
- Create templates from the pages of the PDF/A file, and draw the templates on the pages of the new PDF file using the PdfPageBase.CreateTemplate().Draw(PdfPageBase page, float x, float y) method.
- Save the result file using the PdfDocumentBase.Save(string filePath) method.
C#
using Spire.Pdf;
using System.Drawing;
namespace ConvertPdfAToPdf
{
internal class Program
{
static void Main(string[] args)
{
//Specify the input file path
string inputFile = @"C:\Users\Administrator\Desktop\Output\ToPdfA1A.pdf";
//Specify the output folder
string outputFolder = @"C:\Users\Administrator\Desktop\Output\";
//Initialize an instance of the PdfDocument class
PdfDocument doc = new PdfDocument();
//Load the PDF/A file specified by the input file path
doc.LoadFromFile(inputFile);
//Initialize an instance of the PdfNewDocument class to create a new PDF file
PdfNewDocument newDoc = new PdfNewDocument();
//Set the conpression level of the new PDF file as none
newDoc.CompressionLevel = PdfCompressionLevel.None;
//Iterate through all the pages in the PDF/A file
foreach (PdfPageBase page in doc.Pages)
{
SizeF size = page.Size;
//Add pages to the new PDF file
PdfPageBase p = newDoc.Pages.Add(size, new Spire.Pdf.Graphics.PdfMargins(0));
//Draw the page content of the PDF/A file into the new PDF file
page.CreateTemplate().Draw(p, 0, 0);
}
//Save the result file in the specified output folder
newDoc.Save(outputFolder + "ToPdf.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Imports System.Drawing
Namespace ConvertPdfAToPdf
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Specify the input file path
Dim inputFile = "C:\Users\Administrator\Desktop\Output\ToPdfA1A.pdf"
'Specify the output folder
Dim outputFolder = "C:\Users\Administrator\Desktop\Output\"
'Initialize an instance of the PdfDocument class
Dim doc As PdfDocument = New PdfDocument()
'Load the PDF/A file specified by the input file path
doc.LoadFromFile(inputFile)
'Initialize an instance of the PdfNewDocument class to create a new PDF file
Dim newDoc As PdfNewDocument = New PdfNewDocument()
'Set the conpression level of the new PDF file as none
newDoc.CompressionLevel = PdfCompressionLevel.None
'Iterate through all the pages in the PDF/A file
For Each page As PdfPageBase In doc.Pages
Dim size As SizeF = page.Size
'Add pages to the new PDF file
Dim p As PdfPageBase = newDoc.Pages.Add(size, New Spire.Pdf.Graphics.PdfMargins(0))
'Draw page content of the PDF/A file into the new PDF file
page.CreateTemplate().Draw(p, 0, 0)
Next
'Save the result file in the specified output folder
newDoc.Save(outputFolder & "ToPdf.pdf")
End Sub
End Class
End Namespace