Convert PDF to PDF/A formats

Aspose.PDF for C++ allows you to convert a PDF file to a PDF/A compliant PDF file. Before doing so, the file must be validated. This topic explains how.

Convert the file using the Document class Convert method. Before converting the PDF to PDF/A compliant file, validate the PDF using the Validate method. The validation result is stored in an XML file and then this result is also passed to the Convert method. You can also specify the action for the elements which cannot be converted using the ConvertErrorAction enumeration.

Convert PDF file to PDF/A-1b

The following code snippet shows how to convert PDF files to PDF/A-1b compliant PDF.

void ConverttoPDFA_1b()
{
    std::clog << __func__ << ": Start" << std::endl;
    // String for path name
    String _dataDir("C:\\Samples\\Conversion\\");

    // String for input file name
    String infilename("sample.pdf");
    // String for log file name
    String logfilename("log.xml");
    // String for input file name
    String outfilename("PDFToPDFA_out.pdf");

    // Open document
    auto document = new Document(_dataDir + infilename);

    // Convert to PDF/A compliant document
    // During conversion process, the validation is also performed
    document->Convert(_dataDir + logfilename, PdfFormat::PDF_A_1B, ConvertErrorAction::Delete);

    // Save output document
    document->Save(_dataDir + outfilename);
    std::clog << __func__ << ": Finish" << std::endl;
}

To perform validation only, use the following line of code:

void ConverttoPDFA_1b_Validation()
{
    std::clog << __func__ << ": Start" << std::endl;
    // String for path name
    String _dataDir("C:\\Samples\\Conversion\\");

    // String for input file name
    String infilename("sample.pdf");
    // String for log file name
    String logfilename("log.xml");

    // Open document
    auto document = new Document(_dataDir + infilename);

    // Convert to PDF/A compliant document
    // During conversion process, the validation is also performed
    document->Validate(_dataDir + logfilename, PdfFormat::PDF_A_1B);
    std::clog << __func__ << ": Finish" << std::endl;
}

Convert PDF file to PDF/A-3b

Aspose.PDF for C++ also supports the feature to convert a PDF file to PDF/A-3b format.

void ConverttoPDFA_3b()
{
    std::clog << __func__ << ": Start" << std::endl;
    // String for path name
    String _dataDir("C:\\Samples\\Conversion\\");

    // String for input file name
    String infilename("sample.pdf");
    // String for log file name
    String logfilename("log.xml");
    // String for input file name
    String outfilename("PDFToPDFA3b_out.pdf");

    // Open document
    auto document = new Document(_dataDir + infilename);

    // Convert to PDF/A compliant document
    // During conversion process, the validation is also performed
    document->Convert(_dataDir + logfilename, PdfFormat::PDF_A_3B, ConvertErrorAction::Delete);

    // Save output document
    document->Save(_dataDir + outfilename);
    std::clog << __func__ << ": Finish" << std::endl;
}

Convert PDF file to PDF/A-2u

Aspose.PDF for C++ also supports the feature to convert a PDF file to PDF/A-2u format.

void ConverttoPDFA_2u()
{
    std::clog << __func__ << ": Start" << std::endl;
    // String for path name
    String _dataDir("C:\\Samples\\Conversion\\");

    // String for input file name
     String infilename("sample.pdf");
    // String for log file name
    String logfilename("log.xml");
    // String for input file name
    String outfilename("PDFToPDFA3b_out.pdf");

    // Open document
    auto document = new Document(_dataDir + infilename);

    // Convert to PDF/A compliant document
    // During conversion process, the validation is also performed
    document->Convert(_dataDir + logfilename, PdfFormat::PDF_A_2U, ConvertErrorAction::Delete);

    // Save output document
    document->Save(_dataDir + outfilename);
    std::clog << __func__ << ": Finish" << std::endl;
}

Convert PDF file to PDF/A-3u

Aspose.PDF for C++ also supports the feature to convert a PDF file to PDF/A-3u format.

void ConverttoPDFA_3u()
{
    std::clog << __func__ << ": Start" << std::endl;
    // String for path name
    String _dataDir("C:\\Samples\\Conversion\\");

    // String for input file name
    String infilename("sample.pdf");
    // String for log file name
    String logfilename("log.xml");
    // String for input file name
    String outfilename("PDFToPDFA3b_out.pdf");

    // Open document
    auto document = new Document(_dataDir + infilename);

    // Convert to PDF/A compliant document
    // During conversion process, the validation is also performed
    document->Convert(_dataDir + logfilename, PdfFormat::PDF_A_2U, ConvertErrorAction::Delete);

    // Save output document
    document->Save(_dataDir + outfilename);
    std::clog << __func__ << ": Finish" << std::endl;
}

Add Attachment to PDF/A file

In case you have a requirement to attach files to PDF/A compliance format, then we recommend using PDF_A_3A value from Aspose.PDF.PdfFormat enumeration. PDF/A_3a is the format that provides the feature to attach any file format as an attachment to PDF/A compliant file.

void ConverttoPDFA_AddAttachment()
{
    std::clog << __func__ << ": Start" << std::endl;
    // String for path name
    String _dataDir("C:\\Samples\\Conversion\\");

    // String for input file name
    String infilename("sample.pdf");
    // String for log file name
    String logfilename("log.xml");
    // String for input file name
    String outfilename("PDFToPDFA3b_out.pdf");

    // Open document
    auto document = new Document(_dataDir + infilename);

    // Setup new file to be added as attachment
    auto fileSpecification = MakeObject<FileSpecification>(_dataDir + String("aspose-logo.jpg"), String("Large Image file"));
    // Add attachment to document's attachment collection
    document->get_EmbeddedFiles()->Add(fileSpecification);

    // Convert to PDF/A compliant document
    // During conversion process, the validation is also performed
    document->Convert(_dataDir + logfilename, PdfFormat::PDF_A_3A, ConvertErrorAction::Delete);

    // Save output document
    document->Save(_dataDir + outfilename);
    std::clog << __func__ << ": Finish" << std::endl;
}

Replace missing fonts with alternative fonts

As per PDFA standards, fonts should be embedded in PDFA document. However, if the fonts are not embedded in the source document or exist on the machine then PDFA fails the validation. In this case, we have a requirement to substituent missing fonts with some alternative fonts from the machine. We can substitute missing fonts using the SimpleFontSubsituation method as following during PDF to PDFA conversion.

void ConverttoPDFA_ReplaceFont()
{
    std::clog << __func__ << ": Start" << std::endl;
    // String for path name
    String _dataDir("C:\\Samples\\Conversion\\");

    // String for input file name
    String infilename("sample.pdf");
    // String for log file name
    String logfilename("log.xml");
    // String for input file name
    String outfilename("PDFToPDFA3b_out.pdf");

    // Open document
    auto document = new Document(_dataDir + infilename);

    System::SharedPtr<Aspose::Pdf::Text::Font> originalFont;
    try
    {
        originalFont = FontRepository::FindFont(String("AgencyFB"));
    }
    catch (Exception)
    {
        // Font is missing on destination machine
        auto substitutions = FontRepository::get_Substitutions();
        auto substitution = MakeObject<SimpleFontSubstitution>(String("AgencyFB"), String("Helvetica"));
        substitutions->Add(substitution);
    }

    // Convert to PDF/A compliant document
    try {
        // During conversion process, the validation is also performed
        document->Convert(_dataDir + logfilename, PdfFormat::PDF_A_1B, ConvertErrorAction::Delete);

        // Save output document
        document->Save(_dataDir + outfilename);
    }
    catch (Exception ex) {
        std::cerr << ex->get_Message();
    }
    std::clog << __func__ << ": Finish" << std::endl;
}