PDFTron webviewer - 如何使用 ASP.net MVC Core 将整个编辑的 pdf 保存到服务器

问题描述

我目前正在 MVC Core 中开发一个使用 PDFTron webviewer 的应用程序。无论如何,是否可以将使用 pdftron webviewer 编辑的已编辑 pdf 保存到服务器?

pdftron 有一个功能可以将注释保存到服务器,但我需要将整个 pdf 连同编辑内容保存到服务器。

 WebViewer({
          path: '/lib/WebViewer',initialDoc: '/StaticResource/Music.pdf',fullAPI: !0,enableRedaction: !0
      },document.getElementById('viewer')).then(
      function(t) {
        samplesSetup(t);
        var n = t.docViewer;
         
          n.on('documentLoaded',function() {
            document.getElementById('apply-redactions').onclick = function() {
              t.showWarningMessage({
                title: 'Apply redaction?',message: 'This action will permanently remove all items selected for redaction. It cannot be undone.',onConfirm: function () {
                      alert( );
                      t.docViewer.getAnnotationManager().applyRedactions()
                   debugger

                      var options = {
                          xfdfString: n.getAnnotationManager().exportAnnotations()
                      };
                      var doc = n.getDocument();
                      const data =   doc.getFileData(options);
                      const arr = new Uint8Array(data);
                      const blob = new Blob([arr],{ type: 'application/pdf' });

                      const data = new FormData();
                      data.append('mydoc.pdf',blob,'mydoc.pdf');
                      // depending on the server,'FormData' might not be required and can just send the Blob directly

                      const req = new XMLHttpRequest();
                      req.open("POST",'/DocumentRedaction/SaveFileOnServer',true);
                      req.onload = function (oEvent) {
                          // Uploaded.
                      }; 
                      req.send(data); 
                     
                  return   Promise.resolve();
                },});
            };
          }),t.setToolbarGroup('toolbarGroup-Edit'),t.setToolMode('AnnotationCreateRedaction');
      }
    );

当我向控制器发送请求时,我没有得到文件,它是空的

 [HttpPost]
        public IActionResult SaveFileOnServer(IFormFile file)
        {
            
            return    Json(new { Result="ok"});
        }

谁能告诉我哪里出错了 提前致谢

解决方法

对于 JavaScript 异步函数,您需要等待它完成才能做其他事情。例如,svg { width: 25px; z-index: 200; } 返回一个 Promise,对于 AnnotationManager#applyRedactions()AnnotationManager#exportAnnotations() 也是如此。

对于JS异步函数,可以看看:

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

所以这里你可能想使用 Document#getFileData() 来等待 Promise 完成。