Convert PDF to PDFA using Aspose.PDF for .NET - cause System.ArgumentException exception

Hello,
Using Aspose.Pdf.dll version 17.1.0.0

Converting pdf file in attachment with code:

PdfFormatConversionOptions opts = new PdfFormatConversionOptions(report, PdfFormat.PDF_A_2A, ConvertErrorAction.Delete);
bool converted = inputDocument.Convert(opts);
inputDocument.Save(outputFilePath);

causes exception System.ArgumentException, hexadecimal value 0x1F, is an invalid character. when calling inputDocument.Save(outputFilePath).

Changing conversion options not giving any result.

Thank you,
Konstatnin.

Hello Konstatnin,


Thanks for using our API’s.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}

I have tested the scenario and have managed to reproduce the problem that conversion of your PDF document to PDFA causes exception. For the sake of correction, I have logged it as PDFNET-42181 in our issue tracking system.


Please be patient and spare us little time for investigation. We are sorry for this inconvenience.


Best Regards,

Hi,

Would you be so kind to provide us with status on the PDFNET-42181 issue?

Best regards,

Oleh

@uaprogrammer

We are afraid that earlier logged issue could not get resolved due to other high priority issues in the queue. We will surely inform you as soon as there are some certain updates regarding its resolution. Please spare us little time.

We are sorry for the inconvenience.

Hi,

We are wondering if there are any updates regarding the issue?

BR
Oleh

@uaprogrammer

We apologize for the inconvenience and delay in resolution.

The issue is still not resolved owing to other tasks and implementations to the API. We will surely inform you as soon as it is resolved. We greatly appreciate your patience and comprehension in this matter. Please spare us some time.

We are sorry for the inconvenience.

Hello,

Do you have any updates regarding this issue?
Thank you in advance.

Best regards,
Oleh

@uaprogrammer

We are afraid that earlier logged issue is not yet resolved. We will surely inform you within this forum thread as soon as we have some updates regarding its fix. Please give us some time.

We are sorry for the inconvenience.

Hello,

Do you have any updates regarding this issue?
Thank you in advance.

Best regards,
Oleh

@uaprogrammer

We really regret to share that the issue is not yet fixed. We will certainly let you know as soon as we have some definite updates regarding its resolution.

We apologize for the inconvenience caused.

I’ve hit the same (or similar) problem. has there been any progress over the 4 years since this was first reported?

Rik

@Weechap

Can you please share the source PDF file and code sample so that we may try to reproduce the same on our end.

Hi @mudassir.fayyaz
Here’s some replicating code:

// .NET Framework 4.7
// reference: Aspose.Pdf v20.10.0 via NuGet 


    [TestMethod]
    public void ArgumentExceptionOnConversion_WithPasswordProtection()
    {
        // arrange
        var sourcePath = MakePdf();
        Protect(sourcePath, "P@55w0rd");
        var targetPath = "C:\\Temp\\aspose\\PdfDocument_152939_001.archive.pdf";
        using var document = new Aspose.Pdf.Document(sourcePath, "P@55w0rd");
        document.Decrypt();

        if (File.Exists(targetPath))
        {
            File.Delete(targetPath);
        }

        using var ms = new MemoryStream();
        document.Save(ms);

        using var outputStream = File.OpenWrite(targetPath);
        using var logStream = new MemoryStream();
        using var pdfDoc = new global::Aspose.Pdf.Document(ms);
        
        var conversionOptions = new global::Aspose.Pdf.PdfFormatConversionOptions(logStream, PdfFormat.PDF_A_2A, ConvertErrorAction.Delete);
        conversionOptions.TransparencyAction = ConvertTransparencyAction.Default;

        pdfDoc.Convert(conversionOptions);


        // act
        pdfDoc.Save(outputStream);


        // assert
    }

    [TestMethod]
    public void ArgumentExceptionOnConversion_WithoutPasswordProtection()
    {
        // arrange
        var sourcePath = MakePdf();
        //Protect(sourcePath, "P@55w0rd");
        var targetPath = "C:\\Temp\\aspose\\PdfDocument_152939_001.archive.pdf";
        using var document = new Aspose.Pdf.Document(sourcePath/*, "P@55w0rd"*/);
        document.Decrypt();

        if (File.Exists(targetPath))
        {
            File.Delete(targetPath);
        }

        using var ms = new MemoryStream();
        document.Save(ms);

        using var outputStream = File.OpenWrite(targetPath);
        using var logStream = new MemoryStream();
        using var pdfDoc = new global::Aspose.Pdf.Document(ms);
        
        var conversionOptions = new global::Aspose.Pdf.PdfFormatConversionOptions(logStream, PdfFormat.PDF_A_2A, ConvertErrorAction.Delete);
        conversionOptions.TransparencyAction = ConvertTransparencyAction.Default;

        pdfDoc.Convert(conversionOptions);


        // act
        pdfDoc.Save(outputStream);


        // assert
    }

    private static void Protect(string filePath, string password)
    {
        using var doc = new Aspose.Pdf.Document(filePath);

        Aspose.Pdf.SaveOptions options = new Aspose.Pdf.PdfSaveOptions();

        doc.Encrypt(password, null, Aspose.Pdf.Facades.DocumentPrivilege.AllowAll, Aspose.Pdf.CryptoAlgorithm.AESx128, true);
        doc.Save(filePath, options);
    }

    private static string MakePdf()
    {
        var fileName = $"{Path.GetTempFileName()}.pdf";
        using var doc = new Aspose.Pdf.Document();
        var page = doc.Pages.Add();
        var html = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\" /></head><body><p>HELLO</p><p>WORLD!</p></body></html>";
        var textFragment = new Aspose.Pdf.Text.TextFragment(html);

        // Set text properties
        textFragment.TextState.FontSize = 12;
        textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
        textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.White);
        textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);

        var paragraph = new Aspose.Pdf.Text.TextParagraph();
        paragraph.FirstLineIndent = 30;
        paragraph.SubsequentLinesIndent = 15;
        paragraph.Rectangle = new Rectangle(20, 20, doc.PageInfo.Width - 40, doc.PageInfo.Height - 40);
        paragraph.FormattingOptions.WrapMode = Aspose.Pdf.Text.TextFormattingOptions.WordWrapMode.ByWords;
        paragraph.VerticalAlignment = VerticalAlignment.Top;
        paragraph.AppendLine(textFragment);

        // Create TextBuilder object
        Aspose.Pdf.Text.TextBuilder textBuilder = new Aspose.Pdf.Text.TextBuilder(page);

        // Append the text fragment to the PDF page
        textBuilder.AppendParagraph(paragraph);

        doc.Save(fileName);

        return fileName;
    }

@mudassir.fayyaz It looks like my issue is related to the fact that the PDF has been password protected at some point. The non-password protected code works fine, the password protected code fails

@Weechap

I can not reproduce the issue with Aspose.PDF for .NET 21.5 so please upgrade to latest version and share your feedback.

Hi,

Could you please let me know if the original case PDFNET-42181 has been taken a look at?

Sincerely,
Anna

@uaprogrammer

We really regret to inform you that the earlier logged ticket could not get resolved due to some technical difficulties. However, we have escalated the issue priority already and will soon inform you as soon as it is resolved.

We humbly apologize for the inconvenience and delay.

The issues you have found earlier (filed as PDFNET-42181) have been fixed in Aspose.PDF for .NET 23.3.