How to flatten already filled out PDF form using iTextSharp

后端 未结 4 1659
清酒与你
清酒与你 2021-01-01 22:57

I\'m using iTextSharp to merge a number of pdf files together into a single file.

I\'m using method described in iTextSharp official tutorials, specifically here, wh

4条回答
  •  孤街浪徒
    2021-01-01 23:39

    This is the same answer as the accepted one but without any unused variables:

    private byte[] GetUnEditablePdf(byte[] fileContents)
    {
        byte[] newFileContents = null;
    
        var reader = new PdfReader(fileContents);
    
        if (reader.AcroForm != null)
            newFileContents = FlattenPdfFormToBytes(reader);
    
        else newFileContents = fileContents;
    
        return newFileContents;
    }
    
    private byte[] FlattenPdfFormToBytes(PdfReader reader)
    {
        var memStream = new MemoryStream();
        var stamper = new PdfStamper(reader, memStream) { FormFlattening = true };
        stamper.Close();
        return memStream.ToArray();
    }
    

提交回复
热议问题