Pulling PDF form fields/values in C# using iText7 and memory streams

108 Views Asked by At

I am getting a "PDF header not found" error when trying to use iText7 but working purely with loading the pdf via memorystreams rather than a file path for it saved on a server. Is this not possible? I am just looking to get the form fields and their associated values since these pdf forms are filled in already. I download them as a memory stream from OneDrive and I figured I need to convert to stream first to use as a parameter in PDFReader but that is where I draw my error. My code is as follows:

using (var memoryStream = await oneDrive.DriveItemDownloadAsync(SharedDriveID, Path.Combine(fileDirectory, "testPDFForm.pdf"), "path"))
{
    byte[] buffer = new byte[memoryStream.Length];
    await memoryStream.ReadAsync(buffer, 0, (int)memoryStream.Length);
    document.Base64 = Convert.ToBase64String(buffer);
    document.DownloadLink = driveItem.AdditionalData["@microsoft.graph.downloadUrl"].ToString();

    try
    {
        Stream testStream = memoryStream;
        PdfReader reader = new PdfReader(testStream);
        PdfDocument pdfDocument = new PdfDocument(reader);
        PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(pdfDocument, false);
        IDictionary<string, PdfFormField> Map = new Dictionary<string, PdfFormField>();

        var test = acroForm.GetAllFormFieldsAndAnnotations();
    }
    catch (Exception ex)
    {
        Ok(ex);
    }

}
0

There are 0 best solutions below