0

I'm using PDF.JS to display document that I upload to the server in canvas element using PDF.JS that's working perfectely. That time i'm using iTextSharp to digitally sign the document. When i try to sign the document an Exception is throwed (Exception.IO.Exception) The file is already used by another process. here is my Code for uploding the file :)

[HttpPost]
    public async Task<JsonResult> Upload()
    {
        string fileName = null;
        try
        {
            foreach (string item in Request.Files)
            {
                var fileContent = Request.Files[item];
                if(fileContent != null && fileContent.ContentLength > 0)
                {
                    var inputStream = fileContent.InputStream;
                    fileName = fileContent.FileName;
                    string path = Path.Combine(Server.MapPath("~/UploadFolder"), fileName);
                    using (fileContent.InputStream)
                    {
                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            await inputStream.CopyToAsync(stream);
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            return Json("Upload failed");
        }
        return Json(fileName);
    }

There's how i display PDF in canvas

$(document).ready(function () {
$("#btn2").click(function () {
    var url = document.getElementById("document-to-sign").getAttribute("required-document");
    if (url != "" && url != null) {
        var pdfDoc = null,
            pageNum = 1,
            pageRendering = false,
            pageNumPending = null,
            scale = 1.5,
            canvas = document.getElementById('document-to-sign'),
            ctx = canvas.getContext('2d');

        function renderPage(num) {
            pageRendering = true;

            pdfDoc.getPage(num).then(function (page) {
                var viewport = page.getViewport(scale);
                canvas.height = viewport.height;
                canvas.width = viewport.width;

                var renderContext = {
                    canvasContext: ctx,
                    viewport: viewport
                };
                var renderTask = page.render(renderContext);

                renderTask.promise.then(function () {
                    pageRendering = false;
                    if (pageNumPending !== null) {

                        renderPage(pageNumPending);
                        pageNumPending = null;
                    }
                });
            });

            document.getElementById('page_num').textContent = pageNum;
        }

        function queueRenderPage(num) {
            if (pageRendering) {
                pageNumPending = num;
            } else {
                renderPage(num);
            }
        }

        function onPrevPage() {
            if (pageNum <= 1) {
                return;
            }
            pageNum--;
            queueRenderPage(pageNum);
        }
        document.getElementById('prev').addEventListener('click', onPrevPage);

        function onNextPage() {
            if (pageNum >= pdfDoc.numPages) {
                return;
            }
            pageNum++;
            queueRenderPage(pageNum);
        }
        document.getElementById('next').addEventListener('click', onNextPage);

        PDFJS.getDocument(url).then(function (pdfDoc_) {
            pdfDoc = pdfDoc_;
            document.getElementById('page_count').textContent = pdfDoc.numPages;
            renderPage(pageNum);
        });
        PDFJS.disableStream = true;
        $("#document-to-sign").removeAttr("required-document");
    }
});

I finally that's how i'm signing the document (Adding the empty field to sign)

public static void AddField(string src, 
                                Double x1X, Double x1Y, Double x2X, Double x2Y, int page, 
                                string User)
    {
        try
        {
            PdfReader reader = new PdfReader(src);
            using (PdfStamper s = new PdfStamper(reader, new FileStream(src, FileMode.Open)))
            {
                PdfFormField field = PdfFormField.CreateSignature(s.Writer);
                field.FieldName = "Signature de " + User;
                field.SetWidget(new Rectangle(Convert.ToSingle(x1X), Convert.ToSingle(x1Y), Convert.ToSingle(x2X), Convert.ToSingle(x2Y)), PdfAnnotation.HIGHLIGHT_PUSH);
                field.Flags = PdfAnnotation.FLAGS_PRINT;
                s.AddAnnotation(field, page);
            }
        }
        catch (Exception e)
        {
            logger.Fatal(e.ToString());
            throw e;
        }
    }

I'm stacked in this line

using (PdfStamper s = new PdfStamper(reader, new FileStream(src, FileMode.Open)))

EDIT:

I'm just adding the siging field in this step. Signing the document will be the next task, in console application i'm singing the document with a self-certificate. Upload the document, and adding the signing field and signing it will be further :)
Sorry for the confussion.

Thanks a lot. :)

  • You are adding a signature annotation. You are **not** signing the document. If you show the result to your customer and if your customer knows what he wants, you will get fired. I suggest that you read a manual on digital signatures before you continue. – Bruno Lowagie Aug 05 '16 at 11:28
  • As for your reported problem: you are accessing *the same file* from two different processes. That's not possible. You need to do this sequentially, and you need to release the file as soon as one of the processes is done processing. – Bruno Lowagie Aug 05 '16 at 11:30
  • I know that i'm not signing the file i'm just adding the signing field ,and signing it will be a little bit further. and to do it sequentially it's not possible. upload it, and signing it, will be in two different views. – Mohamed Chaabouni Aug 05 '16 at 12:54
  • OK, that wasn't what you were saying in your question, hence the confusion. – Bruno Lowagie Aug 05 '16 at 12:57

1 Answers1

0

I just found what i'm missing in reading the file

refer to this Cannot access the file because it is being used by another process i was passing the url of the file instead of reading the all bytes from stream

PdfReader reader = new PdfReader(src);


PdfReader reader = new PdfReader(System.IO.File.ReadAllBytes(filePath))
Community
  • 1
  • 1