Search code examples
javapdfreporting-servicesbirt

How to generate password protected pdf using birt report engine api


I have successfully used the BIRT report engine api to generate the pdf using external oracle data source. To generate design I have used eclipse BIRT report designer. My next task is to make this pdf as password protected . Please guide in doing same. Below is code to generate pdf

package com.birt.Main;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.ReportEngine;

public class MainPdf {

    public static void main(String[] args){
        try {
            EngineConfig config = new EngineConfig();

            Platform.startup(config);

            ReportEngine engine = new ReportEngine(config);
            String reportDesign = "new_report.rptdesign";
            IReportRunnable reportRunnable = engine.openReportDesign(reportDesign);
            IRunAndRenderTask runAndRender = engine.createRunAndRenderTask(reportRunnable);

            PDFRenderOption option = new PDFRenderOption();
            option.setOutputFileName("output/resample/mypdf.pdf");
            option.setOutputFormat("pdf");
            runAndRender.setRenderOption(option);
            runAndRender.run();
            runAndRender.close();
            engine.destroy();
            Platform.shutdown();
        } catch (EngineException e) {
            e.printStackTrace();
        } catch (BirtException e) {
            e.printStackTrace();
        }
    }

} 

I am using below version 4.2.0 of org.eclipse.birt.runtime Screen shot of generated pdf

enter image description here


Solution

  • I got it done through itext library . Here is the added code
    
    PdfReader reader = new PdfReader("output/resample/temp.pdf");
                PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output/resample/mypdf.pdf"));
                stamper.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
                        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
                stamper.close();
                reader.close();
                System.out.println("Done");
    

    temp.pdf is the one generated using BIRT . The resource used is here