DEV Community

Cover image for Create PDF/A-4 Documents for Long-Term Archiving in C#
Rajeshwari Pandinagarajan for Syncfusion, Inc.

Posted on • Originally published at syncfusion.com on

Create PDF/A-4 Documents for Long-Term Archiving in C#

The Syncfusion .NET PDF Library allows you to create, edit, and modify PDF documents in your .NET applications. We have provided support for PDF/A-4 conformance level in the 2022 Volume 2 (20.2.0.36) release.

In this article, we will see how to create PDF/A-4 documents from the scratch and convert existing normal PDF documents to PDF/A-4 format using the Syncfusion .NET PDF Library.

PDF/A-4 conformance

PDF/A-4 (ISO 19005-4: 2020) is based on the latest PDF version 2.0 (ISO 32000-2) standard. It is an evolution of PDF/A-3, which is based on ISO 32000-2 (PDF 1.7).

The main difference between PDF/A-4 and the previous conformance level is that it simplifies the PDF standard by removing conformance levels like -a (accessibility), -u (unicode), and -b (basic). So, the PDF/A-4 conformance documents may or may not contain PDF tags.

PDF/A-4 supports the following two conformance levels:

  • PDF/A-4e : PDF/A-4e (engineering) is intended for technical documents and replaces PDF/E format. It supports 3D models, rich media, 3D annotations, and embedded files.
  • PDF/A-4f : PDF/A-4f allows us to embed any files in PDF documents as attachments similar to the PDF/A-3 conformance level.

Create a PDF/A-4 document

With the Syncfusion .NET PDF Library, you can easily create PDF documents with PDF/A-4 conformance support.

To do so, first, install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your .NET app from the NuGet Gallery.

Refer to the following image.

Install the Syncfusion.Pdf.Net.Core NuGet package

Refer to the following code example to create a PDF/A-4 document using the Syncfusion .NET PDF Library.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

//Create a new document with the PDF/A-4 standard.
PdfDocument document = new PdfDocument(PdfConformanceLevel.Pdf_A4);

//Add a page to the document.
PdfPage page = document.Pages.Add();

//Create the PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

//Load the TrueType font from the local file.
FileStream fontStream = new FileStream("../../../arial.ttf", FileMode.Open, FileAccess.Read);
PdfFont font = new PdfTrueTypeFont(fontStream, 14);

//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));

//Create the stream object.
FileStream stream = new FileStream("output.pdf",FileMode.Create);

//Save the document into the memory stream.
document.Save(stream);

//Close the document.
document.Close(true);
Enter fullscreen mode Exit fullscreen mode

By executing the previous code example, we will get a PDF document similar to the following screenshot.

Creating PDF/A-4 conformance document using Syncfusion .NET PDF Library
We have learned how to create a PDF/A- 4 conformance document. Let’s see how to create PDF/A-4e and 4f documents.

Create a PDF/A-4e PDF document

PDF/A-4e allows us to create PDF documents with technical information such as construction plans, 3D technical drawings, and modern geospatial data. You can also embed these PDF documents.

It supports RichMedia annotation that helps us embed video, audio, and 3D models within the page content.

Refer to the following code example to create a PDF/A-4e document with 3D annotation.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Graphics;

//Create a new PDF document. 
PdfDocument document = new PdfDocument(PdfConformanceLevel.Pdf_A4E);

//Create a new page. 
PdfPage page = document.Pages.Add(); 
FileStream inputStream = new FileStream("../../../box.u3d", FileMode.Open, FileAccess.Read);

//Create a new PDF 3D annotation. 
Pdf3DAnnotation pdf3dAnnotation = new Pdf3DAnnotation(new RectangleF(10, 50, 300, 150), inputStream);

//Handle the activation of the 3D annotation. 
Pdf3DActivation activation = new Pdf3DActivation(); 
activation.ActivationMode = Pdf3DActivationMode.PageOpen; 
activation.ShowToolbar = true; 
pdf3dAnnotation.Activation = activation; 
pdf3dAnnotation.Appearance.Normal.Graphics.DrawRectangle(PdfBrushes.Red, new RectangleF(10, 50, 300, 150));

//Add annotation to page. 
page.Annotations.Add(pdf3dAnnotation);

//Save the document into the stream. 
FileStream stream = new FileStream("sample.pdf",FileMode.Create); 
document.Save(stream);

//Close the document. 
document.Close(true);
Enter fullscreen mode Exit fullscreen mode

By executing the previous code example, we will get a PDF document similar to the following screenshot.

Creating a PDF/A-4e conformance document using Syncfusion .NET PDF Library

Create a PDF/A-4f PDF document

With PDF/A-4f conformance, you can even embed non-PDF files in your PDF document as attachments.

Refer to the following code example to create a PDF/A-4f document with an attachment.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Graphics;

//Create a new PDF document. 
PdfDocument document = new PdfDocument(PdfConformanceLevel.Pdf_A4F);

//Create a new page. 
PdfPage page = document.Pages.Add(); 
page.Graphics.DrawRectangle(PdfBrushes.Red, new RectangleF(10, 50, 300, 150)); 
FileStream data = new FileStream("../../../input.txt", FileMode.Open);

//Create an attachment.
PdfAttachment attachment = new PdfAttachment(@"Input.txt", data); 
attachment.Relationship = PdfAttachmentRelationship.Alternative; 
attachment.ModificationDate = DateTime.Now; 
attachment.Description = "Input.txt"; 
attachment.MimeType = "application/txt";

//Add the attachment to the document. 
document.Attachments.Add(attachment);

//Save the document into the stream. 
FileStream stream = new FileStream("sample.pdf",FileMode.Create); 
document.Save(stream);

//Close the document. 
document.Close(true);
Enter fullscreen mode Exit fullscreen mode

By executing the previous code example, we will get a PDF document similar to the following screenshot.

Creating a PDF/A-4f conformance document using Syncfusion .NET PDF Library

Convert a PDF to a PDF/A-4 document

With Syncfusion’s .NET PDF Library, you can easily convert an existing PDF to a PDF/A-4 document for archiving and long-term preservation.

As per the PDF/A-4 conformance rules, all the fonts must be embedded in the PDF document. So, we use the SubstituteFont event to get the font data from the system and embed it into the PDF document. In the following example, we use the Skiasharp NuGet package to get the font details.

Note: To convert the existing PDF to a PDF/A conformance document, you have to add the Syncfusion.Pdf.Imaging.Net.Core NuGet package to your .NET Core project.

Refer to the following code example to convert the existing PDF to a PDF/A-4 document.

using SkiaSharp;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;

FileStream docStream = new FileStream(@"../../../invoice.pdf", FileMode.Open, FileAccess.Read);

PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

//Sample level font event handling. 
loadedDocument.SubstituteFont += LoadedDocument_SubstituteFont;

//Convert the loaded document to PDF/A document. 
loadedDocument.ConvertToPDFA(PdfConformanceLevel.Pdf_A4E); 
FileStream stream = new FileStream("output.pdf",FileMode.Create);

//Save the document. 
loadedDocument.Save(stream);


//Close the document. 
loadedDocument.Close(true);


static void LoadedDocument_SubstituteFont(object sender, PdfFontEventArgs args)
{
  //Get the font name. 
  string fontName = args.FontName.Split(',')[0];

  //Get the font style. 
  PdfFontStyle fontStyle = args.FontStyle; 
  SKFontStyle sKFontStyle = SKFontStyle.Normal;

  if (fontStyle != PdfFontStyle.Regular)
  {
    if (fontStyle == PdfFontStyle.Bold)
    {
       sKFontStyle = SKFontStyle.Bold;
    }
    else if (fontStyle == PdfFontStyle.Italic)
    {
        sKFontStyle = SKFontStyle.Italic;
    }
    else if (fontStyle == (PdfFontStyle.Italic | PdfFontStyle.Bold))
    {
        sKFontStyle = SKFontStyle.BoldItalic;
    }
  }

  SKTypeface typeface = SKTypeface.FromFamilyName(fontName, sKFontStyle); 
  SKStreamAsset typeFaceStream = typeface.OpenStream(); 
  MemoryStream memoryStream = null;

  if (typeFaceStream != null && typeFaceStream.Length > 0)
  {
    //Create the fontData from the type face stream. 
    byte[] fontData = new byte[typeFaceStream.Length - 1]; 
    typeFaceStream.Read(fontData, typeFaceStream.Length); 
    typeFaceStream.Dispose();

    //Create the new memory stream from the font data. 
    memoryStream = new MemoryStream(fontData);
  }

  //Set the font stream to the event args. 
  args.FontStream = memoryStream;
}
Enter fullscreen mode Exit fullscreen mode

By executing the previous code example, we will get a PDF document similar to the following screenshot.

Converting an existing PDF to PDF/A-4 conformance document

GitHub reference

For more details, refer to the creating a PDF/4-A conformance document using the Syncfusion .NET PDF Library demo on GitHub.

Conclusion

Thanks for reading! In this blog post, we have seen how to create PDF/A-4 documents and convert existing PDFs to PDF/4-A conformance documents using the Syncfusion .NET PDF Library. Try out the steps in this blog post and leave your feedback.

Take a moment to peruse our PDF/A- 4 conformance documentation, where you’ll find other options and features, all with accompanying code examples.

For questions, you can contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Related blogs

Latest comments (0)