Package com.lowagie.text.pdf

Examples of com.lowagie.text.pdf.PdfWriter


      System.out.println("There are " + n + " pages in the original file.");

      // step 1: creation of a document-object
      LwgDocument document = new LwgDocument(LwgPageSize.A4);
      // step 2: we create a writer that listens to the document
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
      // step 3: we open the document
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfImportedPage page;
      int rotation;
      int i = 0;
      int p = 0;
      // step 4: we add content
      while (i < n) {
        i++;
        LwgRectangle rect = reader.getPageSizeWithRotation(i);
        float factorx = (x2 - x1) / rect.getWidth();
        float factory = (y1[p] - y2[p]) / rect.getHeight();
        float factor = (factorx < factory ? factorx : factory);
        float dx = (factorx == factor ? 0f : ((x2 - x1) - rect.getWidth() * factor) / 2f);
        float dy = (factory == factor ? 0f : ((y1[p] - y2[p]) - rect.getHeight() * factor) / 2f);
        page = writer.getImportedPage(reader, i);
        rotation = reader.getPageRotation(i);
        if (rotation == 90 || rotation == 270) {
          cb.addTemplate(page, 0, -factor, factor, 0, x1 + dx, y2[p] + dy + rect.getHeight() * factor);
        }
        else {
View Full Code Here


                throw new InstantiationException(
                        "You need to choose a destination file");
            }
            File pdf_file = (File) getValue("destfile");
            LwgDocument document = new LwgDocument();
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream(pdf_file));
            writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
            PdfPageLabels pageLabels = new PdfPageLabels();
            int dpiX, dpiY;
            float imgWidthPica, imgHeightPica;
            TreeSet<File> images = new TreeSet<File>();
            File[] files = directory.listFiles();
            if (files == null) {
                throw new NullPointerException("listFiles() returns null");
            }
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    images.add(files[i]);
                }
            }
            String label;
            for (File image: images) {
                System.out.println("Testing image: " + image.getName());
                try {
                    LwgImage img = LwgImage.getInstance(image.getAbsolutePath());
                    String caption = "";
                    dpiX = img.getDpiX();
                    if (dpiX == 0) {
                        dpiX = 72;
                    }
                    dpiY = img.getDpiY();
                    if (dpiY == 0) {
                        dpiY = 72;
                    }
                    imgWidthPica = (72 * img.getPlainWidth()) / dpiX;
                    imgHeightPica = (72 * img.getPlainHeight()) / dpiY;
                    img.scaleAbsolute(imgWidthPica, imgHeightPica);
                    document.setPageSize(new LwgRectangle(imgWidthPica,
                            imgHeightPica));
                    if (document.isOpen()) {
                        document.newPage();
                    } else {
                        document.open();
                    }
                    img.setAbsolutePosition(0, 0);
                    document.add(img);

                    BaseFont bf = BaseFont.createFont("Helvetica",
                            BaseFont.WINANSI,
                            false);
                    PdfGState gs1 = new PdfGState();
                    gs1.setBlendMode(PdfGState.BM_OVERLAY);
                    PdfContentByte cb = writer.getDirectContent();
                    cb.saveState();
                    cb.setGState(gs1);
                    cb.beginText();
                    cb.setFontAndSize(bf, 40);
                    cb.setTextMatrix(50, 50);
                    cb.showText(caption);
                    cb.endText();
                    cb.restoreState();

                    label = image.getName();
                    if (label.lastIndexOf('.') > 0) {
                        label = label.substring(0, label.lastIndexOf('.'));
                    }
                    pageLabels.addPageLabel(writer.getPageNumber(),
                                            PdfPageLabels.EMPTY, label);
                    System.out.println("Added image: " + image.getName());
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                }
            }
            if (document.isOpen()) {
                writer.setPageLabels(pageLabels);
                document.close();
            } else {
                System.err.println("No images were found in directory " +
                                   directory.getAbsolutePath());
            }
View Full Code Here

      else if ("LETTER".equals(getValue("pagesize"))) {
        document.setPageSize(LwgPageSize.LETTER);
                width = LwgPageSize.LETTER.getWidth() - 40;
                height = LwgPageSize.LETTER.getHeight() - 120;
      }
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
            for (int c = 0; c < comps; ++c) {
                LwgImage img = TiffImage.getTiffImage(ra, c + 1);
                if (img != null) {
                    if (img.getDpiX() > 0 && img.getDpiY() > 0) {
                        img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY());
View Full Code Here

      RandomAccessFileOrArray odd = new RandomAccessFileOrArray(odd_file.getAbsolutePath());
      RandomAccessFileOrArray even = new RandomAccessFileOrArray(even_file.getAbsolutePath());
      LwgImage img = TiffImage.getTiffImage(odd, 1);
      LwgDocument document = new LwgDocument(new LwgRectangle(img.getScaledWidth(),
          img.getScaledHeight()));
      PdfWriter writer = PdfWriter.getInstance(document,
          new FileOutputStream(pdf_file));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      int count = Math.max(TiffImage.getNumberOfPages(odd), TiffImage
          .getNumberOfPages(even));
      for (int c = 0; c < count; ++c) {
        try {
          LwgImage imgOdd = TiffImage.getTiffImage(odd, c + 1);
View Full Code Here

      // step 1: creation of a document-object
      LwgDocument document1 = new LwgDocument(reader.getPageSizeWithRotation(1));
      LwgDocument document2 = new LwgDocument(reader.getPageSizeWithRotation(pagenumber));
      // step 2: we create a writer that listens to the document
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream(file1));
      PdfWriter writer2 = PdfWriter.getInstance(document2, new FileOutputStream(file2));
      // step 3: we open the document
      document1.open();
      PdfContentByte cb1 = writer1.getDirectContent();
      document2.open();
      PdfContentByte cb2 = writer2.getDirectContent();
      PdfImportedPage page;
      int rotation;
      int i = 0;
      // step 4: we add content
      while (i < pagenumber - 1) {
        i++;
        document1.setPageSize(reader.getPageSizeWithRotation(i));
        document1.newPage();
        page = writer1.getImportedPage(reader, i);
        rotation = reader.getPageRotation(i);
        if (rotation == 90 || rotation == 270) {
          cb1.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight());
        }
        else {
          cb1.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
        }
      }
      while (i < n) {
        i++;
        document2.setPageSize(reader.getPageSizeWithRotation(i));
        document2.newPage();
        page = writer2.getImportedPage(reader, i);
        rotation = reader.getPageRotation(i);
        if (rotation == 90 || rotation == 270) {
          cb2.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight());
        }
        else {
View Full Code Here

      // step 1
      LwgDocument document = new LwgDocument(new LwgRectangle(width, height));
      try
      {
         // step 2
         PdfWriter writer;
         writer = PdfWriter.getInstance(document, new FileOutputStream(strPathAbsoluteFile));
         // step 3
         document.open();
         // step 4
         PdfContentByte cb = writer.getDirectContent();

         PdfTemplate tp = cb.createTemplate(width, height);

         Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
View Full Code Here

   {
      try
      {
         LwgDocument document = new LwgDocument();

         PdfWriter writer = PdfWriter.getInstance(document,
                     new FileOutputStream(this._strPathAbsOut));

         writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
         PdfPageLabels pageLabels = new PdfPageLabels();
        
         for (int i=0; i<flesIn.length; i++)
         {
            System.out.println("Testing image: " + flesIn[i].getName());
           
            LwgImage img = LwgImage.getInstance(flesIn[i].getAbsolutePath());
            String caption = "";

            int intDpiX = img.getDpiX();

            if (intDpiX == 0)
               intDpiX = GfrPdfPhotoAlbumFromAbs._INT_DEFAULT_DPI_;


            int intDpiY = img.getDpiY();

            if (intDpiY == 0)
               intDpiY = GfrPdfPhotoAlbumFromAbs._INT_DEFAULT_DPI_;

            float fltImgWidthPica = (GfrPdfPhotoAlbumFromAbs._INT_DEFAULT_DPI_ * img.getPlainWidth()) / intDpiX;
            float fltImgHeightPica = (GfrPdfPhotoAlbumFromAbs._INT_DEFAULT_DPI_ * img.getPlainHeight()) / intDpiY;
            img.scaleAbsolute(fltImgWidthPica, fltImgHeightPica);

            document.setPageSize(new LwgRectangle(fltImgWidthPica, fltImgHeightPica));

            if (document.isOpen())
            {
               document.newPage();
            }

            else
            {
               document.open();
            }

            img.setAbsolutePosition(0, 0);
            document.add(img);

            BaseFont bf = BaseFont.createFont("Helvetica",
                     BaseFont.WINANSI,
                     false);

            PdfGState gs1 = new PdfGState();
            gs1.setBlendMode(PdfGState.BM_OVERLAY);
            PdfContentByte cb = writer.getDirectContent();
            cb.saveState();
            cb.setGState(gs1);
            cb.beginText();
            cb.setFontAndSize(bf, 40);
            cb.setTextMatrix(50, 50);
            cb.showText(caption);
            cb.endText();
            cb.restoreState();

            pageLabels.addPageLabel(writer.getPageNumber(),
                                    PdfPageLabels.EMPTY, strsLabel[i]);

            System.out.println("Added image: " + strsLabel[i]);
         }
        
         if (document.isOpen())
         {
            writer.setPageLabels(pageLabels);
            document.close();
         } // ??? else ???
      }
     
      catch(Exception exc)
View Full Code Here

      LwgRectangle newSize = new LwgRectangle(pageSize.getWidth() / 2, pageSize
          .getHeight());
      // step 1: creation of a document-object
      LwgDocument document = new LwgDocument(newSize, 0, 0, 0, 0);
      // step 2: we create a writer that listens to the document
      PdfWriter writer = PdfWriter.getInstance(document,
          new FileOutputStream(dest));
      // step 3: we open the document
      document.open();
      // step 4: adding the content
      PdfContentByte cb = writer.getDirectContent();
      PdfImportedPage page;
      float offsetX, offsetY;
      int p;
      for (int i = 0; i < total; i++) {
        p = i + 1;
        pageSize = reader.getPageSize(p);
        newSize = new LwgRectangle(pageSize.getWidth() / 2, pageSize.getHeight());

        document.newPage();
        offsetX = 0;
        offsetY = 0;
        page = writer.getImportedPage(reader, p);
        cb.addTemplate(page, 1, 0, 0, 1, offsetX, offsetY);
        document.newPage();
        offsetX = -newSize.getWidth();
        offsetY = 0;
        page = writer.getImportedPage(reader, p);
        cb.addTemplate(page, 1, 0, 0, 1, offsetX, offsetY);

      }
      // step 5: we close the document
      document.close();
View Full Code Here

               
        // step 1: creation of a document-object
        LwgDocument document1 = new LwgDocument(reader.getPageSizeWithRotation(1));
        LwgDocument document2 = new LwgDocument(reader.getPageSizeWithRotation(pagenumber));
        // step 2: we create a writer that listens to the document
        PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream(args[1]));
        PdfWriter writer2 = PdfWriter.getInstance(document2, new FileOutputStream(args[2]));
        // step 3: we open the document
        document1.open();
        PdfContentByte cb1 = writer1.getDirectContent();
        document2.open();
        PdfContentByte cb2 = writer2.getDirectContent();
        PdfImportedPage page;
        int rotation;
        int i = 0;
        // step 4: we add content
        while (i < pagenumber - 1) {
          i++;
          document1.setPageSize(reader.getPageSizeWithRotation(i));
          document1.newPage();
          page = writer1.getImportedPage(reader, i);
          rotation = reader.getPageRotation(i);
          if (rotation == 90 || rotation == 270) {
            cb1.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight());
          }
          else {
            cb1.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
          }
        }
        while (i < n) {
          i++;
          document2.setPageSize(reader.getPageSizeWithRotation(i));
          document2.newPage();
          page = writer2.getImportedPage(reader, i);
          rotation = reader.getPageRotation(i);
          if (rotation == 90 || rotation == 270) {
            cb2.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight());
          }
          else {
View Full Code Here

              while (filename.length() < digits) filename = "0" + filename;
              filename = "_" + filename + ".pdf";
              // step 1: creation of a document-object
              document = new LwgDocument(reader.getPageSizeWithRotation(pagenumber));
        // step 2: we create a writer that listens to the document
              PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(directory, name + filename)));
              // step 3: we open the document
              document.open();
              PdfContentByte cb = writer.getDirectContent();
        PdfImportedPage page = writer.getImportedPage(reader, pagenumber);
        int rotation = reader.getPageRotation(pagenumber);
        if (rotation == 90 || rotation == 270) {
          cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(pagenumber).getHeight());
        }
        else {
View Full Code Here

TOP

Related Classes of com.lowagie.text.pdf.PdfWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.