Example usage for com.itextpdf.text.pdf PdfStamper setPageAction

List of usage examples for com.itextpdf.text.pdf PdfStamper setPageAction

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfStamper setPageAction.

Prototype

public void setPageAction(final PdfName actionType, final PdfAction action, final int page)
        throws PdfException 

Source Link

Document

Sets the open and close page additional action.

Usage

From source file:org.h819.commons.file.MyPDFUtils.java

/**
 * ?pdf???/*from   ww w.j  a  va 2s  .  c om*/
 * <p>
 *  javaScript ???? javaScript ?
 * </p>
 *
 * @param srcPdfFileDir  ?
 * @param descPdfFileDir 
 * @param startDate      ? "2011,01,01"
 * @param alerDays       ???
 * @param expiredDays    ?????
 * @throws java.io.IOException
 */
public static void setExpireDateWithJavaScript(File srcPdfFileDir, File descPdfFileDir, String startDate,
        int alerDays, int expiredDays) throws IOException {

    // ??
    if (alerDays >= expiredDays) {
        logger.info(" ' ' " + alerDays + "  '' " + expiredDays);
        return;
    }

    // ??? ?
    if (StringUtils.countMatches(startDate, ",") != 2) {
        logger.info(startDate + " ??  '2011,01,01' ?");
        return;
    }

    if (!isEnoughSpace(srcPdfFileDir, descPdfFileDir))
        return;

    File listFiles[] = srcPdfFileDir.listFiles();

    if (listFiles.length == 0) {
        logger.info("srcPdfFileDir has not file. " + srcPdfFileDir.getAbsolutePath());
        return;
    }

    // ??
    // String[] resources = new String[]{"/pdfexpiration.js"};
    File resPath = MyFileUtils.copyResourceFileFromJarLibToTmpDir("/pdfexpiration.js"); //????
    // ?
    String jsStr = FileUtils.readFileToString(resPath);

    /** ? js ? */
    // ?
    jsStr = StringUtils.replace(jsStr, "2011,01,01", startDate);
    // ?
    jsStr = StringUtils.replace(jsStr, "alertDays = 355", "alertDays = " + Integer.toString(alerDays));
    // ?
    jsStr = StringUtils.replace(jsStr, "expiredDays = 365", "expiredDays = " + Integer.toString(expiredDays));

    System.out.println(jsStr);

    // logger.info(descPdfFileDir.getAbsolutePath());

    for (File f : listFiles) {
        String fileName = f.getName();
        String extensiion = FilenameUtils.getExtension(fileName).toLowerCase();

        if (f.isFile()) {
            if (extensiion.equals("pdf")) {

                PdfReader reader = getPdfReader(f);

                File fileDesc = new File(descPdfFileDir.getAbsolutePath() + File.separator + fileName);

                try {
                    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(fileDesc));
                    // Get the writer (to add actions and annotations)
                    PdfWriter writer = stamper.getWriter();
                    PdfAction action = PdfAction.javaScript(jsStr, writer, true);
                    //  javaScript ?
                    stamper.setPageAction(PdfWriter.PAGE_OPEN, action, 1);
                    // Close the stamper
                    stamper.close();
                    reader.close();

                    logger.info(fileDesc.getAbsolutePath() + " ? !");

                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            } else
                continue;

        } // end if f.isFile

        else if (f.isDirectory()) {
            setExpireDateWithJavaScript(f,
                    new File(descPdfFileDir.getAbsolutePath() + File.separator + fileName), startDate, alerDays,
                    expiredDays);
        } // end if f.isDirectory
        else
            continue;

    } // end for
}

From source file:org.h819.commons.file.MyPDFUtilss.java

/**
 * ?pdf???//from  w ww .jav a 2  s  .c  om
 * <p>
 *  javaScript ???? javaScript ?
 * </p>
 *
 * @param srcPdfFileDir  ?
 * @param descPdfFileDir 
 * @param startDate      ? "2011,01,01"
 * @param alerDays       ???
 * @param expiredDays    ?????
 * @throws java.io.IOException
 */
public static void setExpireDateWithJavaScript(File srcPdfFileDir, File descPdfFileDir, String startDate,
        int alerDays, int expiredDays) throws IOException {

    // ??
    if (alerDays >= expiredDays) {
        logger.info(" ' ' " + alerDays + "  '' " + expiredDays);
        return;
    }

    // ??? ?
    if (StringUtils.countMatches(startDate, ",") != 2) {
        logger.info(startDate + " ??  '2011,01,01' ?");
        return;
    }

    if (!isEnoughtSpace(srcPdfFileDir, descPdfFileDir))
        return;

    File listFiles[] = srcPdfFileDir.listFiles();

    if (listFiles.length == 0) {
        logger.info("srcPdfFileDir has not file. " + srcPdfFileDir.getAbsolutePath());
        return;
    }

    // ??
    // String[] resources = new String[]{"/pdfexpiration.js"};
    File resPath = MyFileUtils.copyResourceFileFromJarLibToTmpDir("/pdfexpiration.js"); //????
    // ?
    String jsStr = FileUtils.readFileToString(resPath);

    /** ? js ? */
    // ?
    jsStr = StringUtils.replace(jsStr, "2011,01,01", startDate);
    // ?
    jsStr = StringUtils.replace(jsStr, "alertDays = 355", "alertDays = " + Integer.toString(alerDays));
    // ?
    jsStr = StringUtils.replace(jsStr, "expiredDays = 365", "expiredDays = " + Integer.toString(expiredDays));

    System.out.println(jsStr);

    // logger.info(descPdfFileDir.getAbsolutePath());

    for (File f : listFiles) {
        String fileName = f.getName();
        String extensiion = FilenameUtils.getExtension(fileName).toLowerCase();

        if (f.isFile()) {
            if (extensiion.equals("pdf")) {

                PdfReader reader = getPdfReader(f);

                File fileDesc = new File(descPdfFileDir.getAbsolutePath() + File.separator + fileName);

                try {
                    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(fileDesc));
                    // Get the writer (to add actions and annotations)
                    PdfWriter writer = stamper.getWriter();
                    PdfAction action = PdfAction.javaScript(jsStr, writer, true);
                    //  javaScript ?
                    stamper.setPageAction(PdfWriter.PAGE_OPEN, action, 1);
                    // Close the stamper
                    stamper.close();
                    reader.close();

                    logger.info(fileDesc.getAbsolutePath() + " ? !");

                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            } else
                continue;

        } // end if f.isFile

        else if (f.isDirectory()) {
            setExpireDateWithJavaScript(f,
                    new File(descPdfFileDir.getAbsolutePath() + File.separator + fileName), startDate, alerDays,
                    expiredDays);
        } // end if f.isDirectory
        else
            continue;

    } // end for
}