VintaSoft Imaging .NET SDK 14.0: Документация для .NET разработчика
Vintasoft.Imaging.Pdf Namespace / PdfDocument Class / ConvertDocument Methods / ConvertDocument(PdfDocumentConformance,ProcessingState) Method
Синтаксис Exceptions Ремарки Example Требования Смотрите также
В этом разделе
    ConvertDocument(PdfDocumentConformance,ProcessingState) Метод (PdfDocument)
    В этом разделе
    Преобразует PDF документ в соответствие с указанным форматом.
    Синтаксис

    Parameters

    documentConformance
    Соответствие PDF документа.
    processingState
    Состояние процесса преобразования.

    Return Value

    Результат преобразования.
    Исключения
    ИсключениеОписание
    Выбрасывается, если у PDF документа нет источника.
    Выбрасывается, если documentConformance равно Undefined.
    Выбрасывается, если преобразование в documentConformance сейчас не поддерживается.
    Ремарки

    Метод поддерживает только следующие форматы: PDF/A-1b, PDF/A-2b, PDF/A-3b, PDF/A-1a, PDF/A-2a, PDF/A-3a, PDF/A-2u, PDF/A-3u, PDF/A-4, PDF/A-4e, PDF/A-4f.

    PdfA1bConverter, PdfA2bConverter, PdfA3bConverter или Классы PdfA4Converter можно использовать, если PDF документ необходимо преобразовать с пользовательскими настройками (профили ICC, сжатие и т. д.).

    Пример

    Вот пример, показывающий, как преобразовать PDF документ в соответствии со спецификацией PDF/A-1b, и показать подробный результат проверки:

    
    ''' <summary>
    ''' Converts a PDF document to conformance with PDF/A-1b specification and
    ''' shows the detailed result of verification.
    ''' </summary>
    ''' <param name="pdfFilename">The filename of source PDF document.</param>
    ''' <param name="outputPdfFilename">The filename of output PDF document.</param>
    Public Shared Sub ConvertDocumentToPdfA1bDetailed(pdfFilename As String, outputPdfFilename As String)
        ' determine that file must converted to the PDF/A-1b and saved back to the source file
        Dim sameFile As Boolean = pdfFilename.ToUpperInvariant() = outputPdfFilename.ToUpperInvariant()
        ' if converted PDF document must NOT be saved to the source file
        If Not sameFile Then
            ' copy the source file to the output file
            System.IO.File.Copy(pdfFilename, outputPdfFilename, True)
        End If
    
        ' open PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(outputPdfFilename)
            Using processingState As New Vintasoft.Imaging.Processing.ProcessingState()
                ' subscribe to the events for monitoring the progress
                AddHandler processingState.Progress, New System.EventHandler(Of Vintasoft.Imaging.ProgressEventArgs)(AddressOf convertProcessingState_Progress)
    
                ' execute the conversion
                Dim result As Vintasoft.Imaging.Processing.ConversionProfileResult = document.ConvertDocument(Vintasoft.Imaging.Pdf.PdfDocumentConformance.PdfA_1b, processingState)
                System.Console.WriteLine("finished.")
    
                ' if PDF document is converted successfully
                If result.IsSuccessful Then
                    System.Console.WriteLine("Document converted to PDF/A-1b.")
                Else
                    ' if PDF document is NOT converted
                    If Not sameFile Then
                        System.IO.File.Delete(outputPdfFilename)
                    End If
    
                    System.Console.WriteLine("Cannot convert document to PDF/A-1b, unsolved problems:")
                    ' for each activated trigger
                    For Each command As Vintasoft.Imaging.Processing.IProcessingCommandInfo In result.ActivatedTriggers.Keys
                        ' output information about activated trigger
                        System.Console.WriteLine(String.Format("  {0} ({1} matches)", command, result.ActivatedTriggers(command).Count))
                    Next
                End If
            End Using
        End Using
    End Sub
    
    ''' <summary>
    ''' Handles the Progress event of the ProcessingState.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Shared Sub convertProcessingState_Progress(sender As Object, e As Vintasoft.Imaging.ProgressEventArgs)
        System.Console.CursorLeft = 0
        System.Console.Write(String.Format("Conversion {0}%...", e.Progress))
    End Sub
    
    
    
    /// <summary>
    /// Converts a PDF document to conformance with PDF/A-1b specification and
    /// shows the detailed result of verification.
    /// </summary>
    /// <param name="pdfFilename">The filename of source PDF document.</param>
    /// <param name="outputPdfFilename">The filename of output PDF document.</param>
    public static void ConvertDocumentToPdfA1bDetailed(
        string pdfFilename,
        string outputPdfFilename)
    {
        // determine that file must converted to the PDF/A-1b and saved back to the source file
        bool sameFile = pdfFilename.ToUpperInvariant() == outputPdfFilename.ToUpperInvariant();
        // if converted PDF document must NOT be saved to the source file
        if (!sameFile)
            // copy the source file to the output file
            System.IO.File.Copy(pdfFilename, outputPdfFilename, true);
    
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = 
            new Vintasoft.Imaging.Pdf.PdfDocument(outputPdfFilename))
        {
            using (Vintasoft.Imaging.Processing.ProcessingState processingState = 
                new Vintasoft.Imaging.Processing.ProcessingState())
            {
                // subscribe to the events for monitoring the progress
                processingState.Progress += new System.EventHandler<Vintasoft.Imaging.ProgressEventArgs>(
                    convertProcessingState_Progress);
    
                // execute the conversion
                Vintasoft.Imaging.Processing.ConversionProfileResult result =
                    document.ConvertDocument(Vintasoft.Imaging.Pdf.PdfDocumentConformance.PdfA_1b, processingState);
                System.Console.WriteLine("finished.");
    
                // if PDF document is converted successfully
                if (result.IsSuccessful)
                {
                    System.Console.WriteLine("Document converted to PDF/A-1b.");
                }
                // if PDF document is NOT converted
                else
                {
                    if (!sameFile)
                        System.IO.File.Delete(outputPdfFilename);
    
                    System.Console.WriteLine("Cannot convert document to PDF/A-1b, unsolved problems:");
                    // for each activated trigger
                    foreach (Vintasoft.Imaging.Processing.IProcessingCommandInfo command in result.ActivatedTriggers.Keys)
                    {
                        // output information about activated trigger
                        System.Console.WriteLine(string.Format("  {0} ({1} matches)",
                            command,
                            result.ActivatedTriggers[command].Count));
                    }
                }
            }
        }
    }
    
    /// <summary>
    /// Handles the Progress event of the ProcessingState.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    static void convertProcessingState_Progress(object sender, Vintasoft.Imaging.ProgressEventArgs e)
    {
        System.Console.CursorLeft = 0;
        System.Console.Write(string.Format("Conversion {0}%...", e.Progress));
    }
    
    

    Требования

    Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    Смотрите также