Example usage for com.itextpdf.text.pdf PdfAction javaScript

List of usage examples for com.itextpdf.text.pdf PdfAction javaScript

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfAction javaScript.

Prototype

public static PdfAction javaScript(String code, PdfWriter writer, boolean unicode) 

Source Link

Document

Creates a JavaScript action.

Usage

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

/**
 * ?pdf???/*from  w  w  w. ja  v  a 2s .  c  o  m*/
 * <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???/* ww  w  .j a  v a  2  s.c o  m*/
 * <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
}