Protect

Protect will password protect a page or an entire document.

Code Samples

Try the API in the language you prefer

  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
// create protect object
var protectReq = new Protect()
    {
        // document
        Document = new Document()
        {
            DocData = File.ReadAllBytes("myPdf.pdf")
        },
        // action
        ProtectAction = new ProtectAction()
        {
            OwnerPassword = "Pdf4me",
            UserPassword = "Pdf4me",
            Permissions = new HashSet()
            {
                PdfPermission.All
            }
        },
    };
    
// applying protection to the PDF
var res = Pdf4me.Instance.PdfAClient.ProtectAsync(protectReq).GetAwaiter().GetResult();
            
// extract the protected PDF and writing it to disk
byte[] protectedPdf = res.Document.DocData;
File.WriteAllBytes("protectedPdf.pdf", protectedPdf);
// create pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')

// create protect object
const protectReq = {
  // document
  document: {
    docData: fs.readFileSync(path.join(__dirname, 'myPdf.pdf')).toString('base64'),
  },
  // action
  protectAction: {
    userPassword: '123456',
  },
}

// applying protection to the PDF
pdf4meClient.protect(protectReq)
  .then(function(protectRes) {
    // extracting the generated PDF and writing it to disk
    const pdfDocument = Buffer.from(protectRes.document.docData, 'base64')
    fs.writeFileSync(path.join(__dirname, 'protectedPdf.pdf'), pdfDocument)
  })
  .catch(error => {
    console.log(error)
  })
# setup the pdfA_client
pdfA_client = PdfAClient(pdf4me_client)

# create the protect object
protect = Protect(
    # document
    document=Document(
        doc_data=FileReader().get_file_data('myPdf.pdf')
    ),
    # action
    protect_action=ProtectAction(
        user_password='pdf4me',
        owner_password='pdf4me',
        permissions=['all']
    )
)

# applying protection to the PDF
res = pdfA_client.protect(protect=protect)

# extracting the generated PDF
protected_pdf = base64.b64decode(res['document']['doc_data'])
# writing it to disk
with open('protectedPdf.pdf', 'wb') as f:
    f.write(protected_pdf)
// create protect object
$protectReq = [
    // document
    'document'=> [
        'name' => 'PDF_10pages.pdf',
        'docData' => $client->getFileData('PDF_10pages.pdf')
    ],
    // action
    'protectAction' => [
        "ownerPassword" => "pdf4me",
        "userPassword" => "pdf4me",
        "permissions" => ["all"],
    ],
];

// applying protection to the PDF
$res = $client->pdf4me()->protect($protectReq);

// extracting the protected PDF
$protectedPdf = base64_decode($res->document->docData);
// writing it to disk
file_put_contents('protectedPdf.pdf', $protectedPdf);

Important Links

Swagger - Protect