Esempio n. 1
0
  private void doActionMerge()
      throws ApplicationException, PageException, IOException, DocumentException {

    if (source == null && params == null && directory == null)
      throw new ApplicationException(
          "at least one of the following constellation must be defined"
              + " attribute source, attribute directory or cfpdfparam child tags");
    if (destination == null && StringUtil.isEmpty(name, true))
      throw new ApplicationException(
          "at least one of the following attributes must be defined " + "[destination,name]");
    if (destination != null && !overwrite)
      throw new ApplicationException("destination file [" + destination + "] already exists");

    ArrayList docs = new ArrayList();
    PDFDocument doc;
    boolean isListing = false;

    // source
    if (source != null) {
      if (Decision.isArray(source)) {
        Array arr = Caster.toArray(source);
        int len = arr.size();
        for (int i = 1; i <= len; i++) {
          docs.add(doc = toPDFDocument(arr.getE(i), password, null));
          doc.setPages(pages);
        }
      } else if (source instanceof String) {
        String[] sources =
            List.toStringArrayTrim(List.listToArrayRemoveEmpty((String) source, ','));
        for (int i = 0; i < sources.length; i++) {
          docs.add(doc = toPDFDocument(sources[i], password, null));
          doc.setPages(pages);
        }
      } else docs.add(toPDFDocument(source, password, null));
    }
    boolean destIsSource = false;

    // params
    if (directory != null && !directory.isDirectory()) {
      if (!directory.exists())
        throw new ApplicationException("defined attribute directory does not exist");
      throw new ApplicationException("defined attribute directory is not a directory");
    }
    if (params != null) {
      Iterator it = params.iterator();
      PDFParamBean param;
      while (it.hasNext()) {
        param = (PDFParamBean) it.next();
        docs.add(doc = toPDFDocument(param.getSource(), param.getPassword(), directory));
        doc.setPages(param.getPages());
      }
    } else if (directory != null) {
      isListing = true;
      Resource[] children = ResourceUtil.listResources(directory, filter);

      if (ascending) {
        for (int i = children.length - 1; i >= 0; i--) {
          if (destination != null && children[i].equals(destination)) destIsSource = true;
          docs.add(doc = toPDFDocument(children[i], password, null));
          doc.setPages(pages);
        }
      } else {
        for (int i = 0; i < children.length; i++) {
          if (destination != null && children[i].equals(destination)) destIsSource = true;
          docs.add(doc = toPDFDocument(children[i], password, null));
          doc.setPages(pages);
        }
      }
    }

    int doclen = docs.size();
    if (doclen == 0) throw new ApplicationException("you have to define at leat 1 pdf file");

    // output
    OutputStream os = null;
    if (!StringUtil.isEmpty(name) || destIsSource) {
      os = new ByteArrayOutputStream();
    } else if (destination != null) {
      os = destination.getOutputStream();
    }

    /*com.lowagie.text.Document document=null;
    PdfCopy copy=null;
    PdfReader pr;
    Set pages;
    int size;*/

    try {
      if (!isListing) stopOnError = true;
      PDFUtil.concat(
          (PDFDocument[]) docs.toArray(new PDFDocument[docs.size()]),
          os,
          keepBookmark,
          false,
          stopOnError,
          version);
      /*
      boolean init=false;
      for(int d=0;d<doclen;d++) {
      	doc=(PDFDocument) docs.get(d);
      	pages=doc.getPages();
      	try {
      		pr=doc.getPdfReader();
      		print.out(pr.getCatalog().getKeys());

      	}
      	catch(Throwable t) {
      		if(isListing && !stopOnError)continue;
      		throw Caster.toPageException(t);
      	}
      	print.out("d+"+d);
      	if(!init) {
      		init=true;
      		print.out("set");
      		document = new com.lowagie.text.Document(pr.getPageSizeWithRotation(1));
      		copy = new PdfCopy(document,os);
      		document.open();
      	}
      	size=pr.getNumberOfPages();
      	print.out("pages:"+size);
      	for(int page=1;page<=size;page++) {
      		if(pages==null || pages.contains(Constants.Integer(page))) {
      			copy.addPage(copy.getImportedPage(pr, page));
      		}
      	}
      }*/
    } finally {
      // if(document!=null)document.close();
      IOUtil.closeEL(os);
      if (os instanceof ByteArrayOutputStream) {
        if (destination != null)
          IOUtil.copy(
              new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray()),
              destination,
              true); // MUST overwrite
        if (!StringUtil.isEmpty(name))
          pageContext.setVariable(name, ((ByteArrayOutputStream) os).toByteArray());
      }
    }
  }