Why do I get an "System.IO.FileLoadException: Could not load file or assembly" error? with iText7

0

I'm developing AWS Lambda project with .Net SDK (Core 2.1). I need to use iText7 in my project. When I want to debug the project throws below exception.

System.IO.FileLoadException: Could not load file or assembly 'itext.kernel, Version=7.1.10.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca'. An operation is not legal in the current state. (Exception from HRESULT: 0x80131509)
at LucidMailing.Function.FunctionHandler(JObject input, ILambdaContext context)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at LucidMailing.Function.FunctionHandler(JObject input, ILambdaContext context)

I applied the solution in their thecnical support page (Support Page) but still throws the exception.

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.S3;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Extgstate;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.License;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public async Task<string> FunctionHandler(JObject input, ILambdaContext context)
    {        

        var s3Response = await this.S3Client.GetObjectAsync(BucketName, GetKey(input["id"].ToString()));

        using (Stream responseStream = s3Response.ResponseStream)
        {

            using (MemoryStream memStream = new MemoryStream())
            {
                responseStream.CopyTo(memStream);
                memStream.Seek(0, SeekOrigin.Begin);

                var stream = new MemoryStream();
                var writer = new PdfWriter(stream);

                PdfDocument pdfDoc = new PdfDocument(new PdfReader(memStream), writer);
                Document dc = new Document(pdfDoc);
                PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
                PdfName name = new PdfName(dealDoc.FileNameWithoutExtension);

                pdfDoc.GetCatalog().GetNameTree(name);

                Paragraph paragraph = new Paragraph(dealDoc.CompanyId).SetFont(font).SetFontSize(30);
                pdfDoc.GetDocumentInfo().SetTitle(dealDoc.FileName);

                PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.5f);

                // Implement transformation matrix usage in order to scale image
                for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
                {
                    PdfPage pdfPage = pdfDoc.GetPage(i);
                    Rectangle pageSize = pdfPage.GetPageSize();
                    float x = (pageSize.GetLeft() + pageSize.GetRight()) / 2;
                    float y = (pageSize.GetTop() + pageSize.GetBottom()) / 2;
                    PdfCanvas over = new PdfCanvas(pdfPage);
                    over.SetFillColor(ColorConstants.RED);
                    over.SaveState();
                    over.SetExtGState(gs1);

                    dc.ShowTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45f);

                    over.RestoreState();
                }

                dc.Close();

                return Convert.ToBase64String(stream.ToArray());
            }
        }
    }

Any suggestions on how I figure out the exception of this DLL file?

c#
asp.net-core
aws-lambda
itext7
aws-sdk-net
asked on Stack Overflow Mar 24, 2020 by Görkem Yazıcı • edited Mar 27, 2020 by Görkem Yazıcı

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0