Protect

Protect sets password or removes password from a document. It also allows customization of permissions to be set while locking the document with password.

FeatureParameterResponseActionDescriptionLinks
protectProtectProtectResProtectActionProtect will password protect a page or an entire document.Swagger
Sample
protectDocument
file,permission,password
file streamProtects a pdf document by setting a password.Swagger
Sample
unlockDocument file,
password
file stream Unlocks a password protected pdf document
Swagger
Sample

Samples

Protect

  • curl
  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
curl No Sample
// 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)
  })
// 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);
# 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)

ProtectDocument

  • curl
  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
curl https://api.pdf4me.com/PdfA/ProtectDocument ^
-H "Authorization: Basic DEV-KEY" ^
-F password=123456 ^
-F permissions=all ^  
-F "file=@./myPdf.pdf" ^
-o ./protectedPdf.pdf
// securing pdf with password protection (password: All)
var res = Pdf4me.Instance.PdfAClient.ProtectDocumentAsync(File.ReadAllBytes("myPdf.pdf"), "123456", "All").GetAwaiter().GetResult();

// and writing the secured PDF to disk
File.WriteAllBytes("protectedPdf.pdf", res);
// create pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')

// securing pdf with password protection (password: All)
pdf4meClient.protectDocument('123456', 'all', fs.createReadStream(path.join(__dirname, 'myPdf.pdf')))
  .then(pdf => {
    // and writing the secured PDF to disk
    fs.writeFileSync(path.join(__dirname, 'protectedPdf.pdf'), pdf)
  })
  .catch(error => {
    console.error(error)
  })
// securing pdf with password protection (password: All)
$res = $client->pdf4me()->protectDocument(
    [
        "password" => "123456",
        "permissions" => "all",
        "file" => 'myPdf.pdf'
    ]);
    
// and writing the secured PDF to disk
file_put_contents('protectedPdf.pdf', $res);
# setup the pdfA_client
pdfA_client = PdfAClient(pdf4me_client)

# protecting the PDF
protected_pdf = pdfA_client.protect_document(
        password='123456',
        permissions='all',
        file=FileReader().get_file_handler(path='myPdf.pdf')
    )
# and writing the secured PDF to disk
with open('protectedPdf.pdf', 'wb') as f:
    f.write(protected_pdf)

UnlockDocument

  • curl
  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
curl https://api.pdf4me.com/PdfA/UnlockDocument ^
-H "Authorization: Basic DEV-KEY" ^
-F password=123456 ^
-F "file=@./myPdf.pdf" ^
-o ./unlockedPdf.pdf
// removing password security from pdf
var res = Pdf4me.Instance.PdfAClient.UnlockDocumentAsync(File.ReadAllBytes("myPdf.pdf"), "123456").GetAwaiter().GetResult();

// and writing the unsecured PDF to disk
File.WriteAllBytes("unlockedPdf.pdf", res);
// create pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')

// removing password security from pdf
pdf4meClient.unlockDocument('123456', fs.createReadStream(path.join(__dirname, 'myPdf.pdf')))
  .then(pdf => {
    // and writing the unsecured PDF to disk
    fs.writeFileSync(path.join(__dirname, 'unlockedPdf.pdf'), pdf)
  })
  .catch(error => {
    console.error(error)
    process.exit(1)
  })
// removing password security from pdf
$res = $client->pdf4me()->unlockDocument(
    [
        "password" => "123456",
        "file" => 'myPdf.pdf'
    ]);
    
// and writing the secured PDF to disk
file_put_contents('unlockedPdf.pdf', $res);
# setup the pdfA_client
pdfA_client = PdfAClient(pdf4me_client)

# protecting the PDF
unlocked_pdf = pdfA_client.unlock_document(
        password='123456',
        file=FileReader().get_file_handler(path='myPdf.pdf')
    )
# writing the generated PDF to disk
with open('unlockedPdf.pdf', 'wb') as f:
    f.write(unlocked_pdf)

Model

Protect

Name Type Description Notes
document Document
protectAction ProtectAction
jobId String [optional]
jobIdExtern String [optional]
integrations [String] [optional]

ProtetctAction

Name Type Description Notes
userPassword String Password to be set for user for protecting the document. [Optional]
ownerPassword String Password for document owner [Optional]
unlock boolean -true: unlock the document
-false:
[Optional]
permissions String [Optional]

ProtectRes

Name Type Description Notes
document Document Protected document.

Document

Name Type Description Notes
jobId String JobId of Documents WorkingSet.
documentId String Document Id
name String Filename inlcuding filetype.
docStatus String Status of the Document, e.g. Stamped.
pages Page Description of pages.
docData [byte] Document bytes.
docMetadata DocMetadata Document metadata such as title, pageCount et al.
docLogs DocLog Logging information about the request, e.g. timestamp.

Page

Name Type Description Notes
documentId String Globally unique Id.
pageId String Globally unique Id.
pageNumber Integer PageNumber, starting with 1.
rotate double By how much the page was rotated from its original orientation.
thumbnail byte Thumbnail representing this particular page.
sourceDocumentId String Id of the document it was created from, e.g. in case of an extraction, the result's sourceDocumentId is the Id of the PDF the pages have been extracted from.
sourcePageNumber Integer Page number of the original page in the original document, e.g. let's assume document B consists of page number 4 of document A (extraction).
Thus, document B's only page's sourcePageNumber is number 4.

DocMetadata

Name Type Description Notes
title String Title of document.
subject String Subject of document.
pageCount long Number of pages.
size long Number bytes of the document.
isEncrypted boolean If the document is Encrypted
pdfCompliance String Pdf Compliance, e.g. PDF/A.
isSigned boolean If the document is Encrypted
uploadedMimeType String Uploaded MimeType, e.g. application/bson.
uploadedFileSize long Uploaded file size.

DocLog

Name Type Description Notes
messageType String MessageType, e.g. PdfALog.
message String Message itself, e.g. a warning.
timestamp dateTime Timestamp.
docLogLevel String Type of message. Supported Values :
"verbose", "info", "warning", "error", "timing"
durationMilliseconds long Timing for requested log information [ms].

How can we help?