Converting the normal PDF file to Password Protected File using SAP CPI

In this scenario we are going to change normal pdf file into password protected file using  SAP CPI.

Step 1: Create the Iflow with the desired name. In this blog I am giving the Iflow name as Dummy(As shown in the below picture).

Step 2: Connect the sender and integration process with the HTTP Sender Adapter and give the preferred HTTP address.

Step 3: Now add the itextpdf JAR file which will be used in the groovy script in the Iflow by going to references tab – archive.

Here is the link to download the JAR (


Step 4: Create one groovy script and write the script below.


import java.util.HashMap;
import com.itextpdf.text.Document
import com.itextpdf.text.Paragraph
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.text.pdf.PdfReader
import com.itextpdf.text.pdf.PdfStamper
def Message processData(Message message) {
    def inputPayload = message.getBody();
    def name1 = "username";
    def pass1 = name1.substring(0,4);
    def name2 = "password"
    def pass2 = name2.substring(0,4);
    def userPassword = pass1+pass2;
    def reader = new PdfReader(inputPayload.getBytes());
    def outputStreamProtected = new ByteArrayOutputStream();
    def stamper = new PdfStamper(reader, outputStreamProtected);
    stamper.setEncryption(userPassword.getBytes(), null, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
    return message;


Step 5: After writing the groovy script, deploy the Iflow and copy the endpoint URL from the SAP CPI Message Monitoring Section.

Step6: To test ,we can use the Postman Tool.

In the Postman tool create new request with the Endpoint URL and in the Authorization section, use Basic Auth with username and password as Client ID and Client Secret from the BTP Cockpit.

Then in the body, change the type to binary and upload your pdf, after that Click on the send button.


After the completion of this request, we get the below screen, now click on the right side 3 dots , and save response to file.


Now you can open the pdf file and check whether the password is set or not to the given pdf.


Source link

Be the first to comment

Leave a Reply

Your email address will not be published.