Response myself

/**
Create a new pdf file whith escale pages.
@param pdf bytes from pdf
@return bytes from new pdf file
*/
public byte[] solution1B(byte[] pdf) throws  IOException, DocumentException
{

      byte[] arrayBytesfinal = new byte[0];;
      PdfReader reader = new PdfReader(pdf);
      int n = reader.getNumberOfPages();

      if(n>0){


        for (int i = 1; i <= n; i++) {

        Rectangle rectReder = reader.getPageSize(i);
        Document document = new Document(rectReder);
        ByteArrayOutputStream bouf = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, bouf);

        document.open();
        PdfContentByte directcontent = writer.getDirectContent();

        PdfImportedPage page;
        Image image;
          page = writer.getImportedPage(reader, i);

          image = Image.getInstance(page);


          int altoMenos = firmantes.length*25;
          image.scaleAbsolute(rectReder.getWidth(), rectReder.getHeight() -
altoMenos);
          image.setAbsolutePosition(0,altoMenos+20);

          directcontent.addImage(image);

          document.close();

          arrayBytesfinal =
this.concatenar(arrayBytesfinal,bouf.toByteArray());
        }

        return arrayBytesfinal;
      }
      return new byte[0];
    }

     /**
   * Concatena los bytes que forman dos ficheros.
   * @return
   * @param ficheroNuevo contenido del nuevo fichero a concatenar
   * @param ficheroTotal contenido del fichero total al que se concatenará
el documento nuevo.
   */
  private byte[] concatenar(byte[] ficheroTotal,byte[] ficheroNuevo)
  {
    try {
      int pageOffset = 0;

      ArrayList master = new ArrayList();

      ByteArrayOutputStream outByte = new ByteArrayOutputStream();


      Document document = null;
      PdfCopy  writer = null;

      for(int j=0;j<2;){
          // we create a reader for a certain document
          PdfReader reader = null;
          if(j==0 && ficheroTotal.length!=0){
           reader= new PdfReader(ficheroTotal);
          }else{
            reader= new PdfReader(ficheroNuevo);
          }
          reader.consolidateNamedDestinations();
          // we retrieve the total number of pages
          int n = reader.getNumberOfPages();
          List bookmarks = SimpleBookmark.getBookmark(reader);
          if (bookmarks != null) {
              if (pageOffset != 0)
                  SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset,
null);
              master.addAll(bookmarks);
          }
          pageOffset += n;

          if (j == 0) {
              // step 1: creation of a document-object
              document = new
Document();//reader.getPageSizeWithRotation(1));
              // step 2: we create a writer that listens to the document
              writer = new PdfCopy(document, outByte);
              // step 3: we open the document
              document.open();

          }
          // step 4: we add content
          PdfImportedPage page;
          for (int i = 0; i < n; ) {
              ++i;
              page = writer.getImportedPage(reader, i);
              writer.addPage(page);
          }
          PRAcroForm form = reader.getAcroForm();
          if (form != null)
              writer.copyAcroForm(reader);
          //Esto vale para cuando se concatena por primera vez los ficheros
y el destino esta vacio.
          //Obligamos a salir del bucle con una sola iteración.
          if(j==0 && ficheroTotal.length!=0){j++;}else{j=2;}
      }


      if (!master.isEmpty())
          writer.setOutlines(master);


      // step 5: we close the document
      document.close();

      outByte.close();
      return outByte.toByteArray();
    }
    catch(Exception e) {
      e.printStackTrace();

      return null;
    }
  }

Thanks and regards, Ángel.


2010/2/15 amcereijo cereijo <[email protected]>

> Hi to all,
>
> Can itext create a document with horizontal and vertical pages in the same
> document?
>
> Thanks and regards, Ángel.
>
------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to