Strip Adobe Reader and Version requirements from PDF before outputting it to browser

吃可爱长大的小学妹 提交于 2019-12-11 18:28:11

问题


I am planning on using pdf.js to have PDF context via the browser with Javascript. The problem is that some PDFs, the ones I am using, require Adobe's Reader with a specific Version. pdf.js does does not yet(ever?) support spoofing of these. What I need to know is if there's a way in C# to open the PDF and remove these Reader and Version requirements and how to do it. I was planning on using itextsharp to do other PDF manipulation server-side so an example using this would be most helpful. I plan on serving these as an ActionResult from an ajax request via MVC 4, so a MemoryStream would be most helpful at the end of this manipulation.


回答1:


Your PDF file n-400.pdf uses the Adobe XML Forms Architecture (XFA). This means you require a viewer that also supports XFA which pdf.js seemingly does not.

Such a PDF normally contains some standard PDF content which indicates that the PDF requires some viewer that supports XFA. In your case the content contains

If this message is not eventually replaced by the proper contents of the document, your PDF viewer may not be able to display this type of document.

This actually indicates what a XFA enabled viewer does, it renders some pages based upon information in the XFA XML data and displays it instead of the PDF style page descriptions.

While being defined proprietarily by Adobe, the PDF specification ISO 32000-1 describes how XFA data is to be embedded in a PDF document, cf. section 12.7.8 XFA Forms.

If you only need those forms in a flattened state, you might want to have a look at iText Demo: Dynamic XFA forms in PDF.




回答2:


So in the end pdf.js was unable to do what I needed it too, however, what I was able to do was convert the Xfa/Pdf to a C# object then send the pages as needed via Json to my Javascript for rendering in the HTML5 Canvas. The code below takes an xfa-in-a-pdf file and turns it into a C# object with the help of itextsharp:

    PdfReader.unethicalreading = true;
    PdfReader reader = new PdfReader(new FileStream(Statics.PdfUploadLocation + PdfFileName, FileMode.Open, FileAccess.Read));

    XfaForm xfaForm = new XfaForm(reader);
    XDocument xDoc = XDocument.Parse(xfaForm.DomDocument.InnerXml);

    string xfaNamespace = @"{http://www.xfa.org/schema/xfa-template/2.6/}";


    List<XElement> formPages = xDoc.Descendants(xfaNamespace + "subform").Descendants(xfaNamespace + "subform").ToList();
    TotalPages = formPages.Count();


    var fieldIndex = 0;
    RawPdfFields = new List<XfaField>();

    for (int page = 0; page < formPages.Count(); page++)
    {
        RawPdfFields.AddRange(formPages[page].Descendants(xfaNamespace + "field")
                    .Select(x => new XfaField
                    {
                        Page = page,
                        Index = fieldIndex++,
                        Name = (string)x.Attribute("name"),
                        Height = GetUnitFromPossibleString((string)x.Attribute("h")),
                        Width = GetUnitFromPossibleString((string)x.Attribute("w")),
                        XPosition = GetUnitFromPossibleString((string)x.Attribute("x")),
                        YPosition = GetUnitFromPossibleString((string)x.Attribute("y")),
                        Reference = GetReference(x.Descendants(xfaNamespace + "traverse")),
                        AssistSpeak = GetAssistSpeak(x.Descendants(xfaNamespace + "speak"))
                    }).ToList());
    }


来源:https://stackoverflow.com/questions/14903904/strip-adobe-reader-and-version-requirements-from-pdf-before-outputting-it-to-bro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!