Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

c# - iText7 create a document from IFormFile

I want to insert image to the user upload file, when I tried to create pdfDocument from the stream I got the "You can't read from OutputStream" error

public ActionResult GetTemplate(IFormFile template)
{
    byte[] pdfBytes;
    using (var memoryStream = new MemoryStream())
    {
        template.CopyTo(memoryStream);
        var writer = new PdfWriter(memoryStream);
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(memoryStream));
        var document = new Document(pdfDocument);
        var pageCount = pdfDocument.GetNumberOfPages();
        ImageData imageData = ImageDataFactory.CreatePng(System.Convert.FromBase64String(//));
        var img = new Image(imageData).Scale(200, 100).SetFixedPosition(pageCount, 50, 120);
        document.Add(img);
        document.Close();
        pdfBytes = memoryStream.ToArray();
    }
    return new FileContentResult(pdfBytes, "application/pdf");
}

If I changed it to new PdfDocument(new PdfReader(writer)); I got "PDF header not found" error instead.

This is my first time using iText, most of the answers I found on their website only worked with java. Any suggestion or help would be great, thank you for your time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...