Package com.itextpdf.text

Examples of com.itextpdf.text.Document


  }

  private static void createCombinedPDF(String filename, String title,
      List<JFreeChart> charts1, List<JFreeChart> charts2) throws IOException {
    Rectangle rect = PageSize.A4.rotate();
    Document document = new Document(rect);
    OutputStream out = new FileOutputStream(filename);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, out);
      document.addAuthor("Daniel Velten");
      document.addTitle(title);
      document.open();
     
      for (int i=0; i<2;i++) {
        addChartToPDF2(charts1.get(i), writer, 1-i, 0);       
        addChartToPDF2(charts2.get(i), writer, 1-i, 1);       
      }
      document.newPage();
      for (int i=0; i<2;i++) {
        addChartToPDF2(charts1.get(i+2), writer, 1-i, 0);       
        addChartToPDF2(charts2.get(i+2), writer, 1-i, 1);       
      }
      document.newPage();
      for (int i=0; i<2;i++) {
        addChartToPDF2(charts1.get(i+4), writer, 1-i, 0);       
        addChartToPDF2(charts2.get(i+4), writer, 1-i, 1);       
      }
      document.newPage();
      for (int i=0; i<2;i++) {
        addChartToPDF2(charts1.get(i+6), writer, 1-i, 0);       
        addChartToPDF2(charts2.get(i+6), writer, 1-i, 1);       
      }
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    }
    document.close();
    out.close();
  }
View Full Code Here


    try {
      File dd = new File(path);
      dd.createNewFile();
     
      FileOutputStream f=new FileOutputStream(dd);
      Document document = new Document();
      PdfWriter.getInstance(document, f);
      document.open();
      addTitlePage(document);
      addContent(document);
      document.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here

  public InputStream cut(final InputStream source, final int start, final int end) throws IOException {
    try {
      Assert.notNull(source, "source");

      PdfReader reader = new PdfReader(source);
      Document document = new Document(reader.getPageSizeWithRotation(1));
      InputOutputStream outputStream = new InputOutputStream();
      PdfCopy writer = new PdfCopy(document, outputStream);
      int pageCount = reader.getNumberOfPages();

      Assert.lessOrEqual(start, pageCount, "start");

      int endPage = end;
      if (endPage > pageCount) {
        endPage = pageCount;
      }

      document.open();

      for (int i = start; i <= endPage; i++) {
        PdfImportedPage page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }

      document.close();
      writer.close();
      reader.close();

      return outputStream.getInputStream();
    } catch (DocumentException e) {
View Full Code Here

  public InputStream merge(final InputStream[] sources) throws IOException {
    try {
      Assert.notNull(sources, "sources");
      Assert.notEmpty(sources, "sources");

      Document document = new Document();
      InputOutputStream outputStream = new InputOutputStream();
      PdfCopy writer = new PdfCopy(document, outputStream);

      document.open();

      for (InputStream source : sources) {
        PdfReader reader = new PdfReader(source);
        int pageCount = reader.getNumberOfPages();
        for (int i = 1; i <= pageCount; i++) {
          PdfImportedPage page = writer.getImportedPage(reader, i);
          writer.addPage(page);
        }
        reader.close();
      }

      document.close();
      writer.close();

      return outputStream.getInputStream();
    } catch (DocumentException e) {
      throw new br.net.woodstock.rockframework.office.DocumentException(e);
View Full Code Here

      PdfReader reader = new PdfReader(source);
      int pageCount = reader.getNumberOfPages();
      List<InputStream> list = new LinkedList<InputStream>();

      Document document = null;
      InputOutputStream outputStream = null;
      PdfCopy writer = null;
      for (int i = 1; i <= pageCount; i++) {
        if ((document == null) || (i % size == 0)) {
          if (document != null) {
            document.close();
            writer.close();
            list.add(outputStream.getInputStream());
          }
          document = new Document(reader.getPageSizeWithRotation(1));
          outputStream = new InputOutputStream();
          writer = new PdfCopy(document, outputStream);
        }
        PdfImportedPage page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }

      if (document != null) {
        document.close();
        writer.close();
        list.add(outputStream.getInputStream());
      }

      reader.close();
View Full Code Here

    }
    if (resultWidth > 64) {
      size = PageSize.A0.rotate();
    }

    Document document = new Document(size, 15, 15, 10, 10);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, baos);
    document.open();
    populatePdf(document, writer, qr);

    // do we want to add a svg image?
    if (StringUtils.isNotBlank(svg)) {
      document.newPage();
      StringBuffer s1 = new StringBuffer(svg);
      if (!svg.startsWith("<svg xmlns=\"http://www.w3.org/2000/svg\" ")) {
        s1.insert(s1.indexOf("<svg") + 4, " xmlns='http://www.w3.org/2000/svg'");
      }

      String t = "<?xml version='1.0' encoding='ISO-8859-1'"
                 + " standalone='no'?>" + s1.toString();
      PdfContentByte cb = writer.getDirectContent();
      cb.saveState();
      cb.concatCTM(1.0f, 0, 0, 1.0f, 36, 0);
      float width = document.getPageSize().getWidth() - 20;
      float height = document.getPageSize().getHeight() - 20;
      Graphics2D g2 = cb.createGraphics(width, height);
      //g2.rotate(Math.toRadians(-90), 100, 100);
      PrintTranscoder prm = new PrintTranscoder();
      TranscoderInput ti = new TranscoderInput(new StringReader(t));
      prm.transcode(ti, null);
      PageFormat pg = new PageFormat();
      Paper pp = new Paper();
      pp.setSize(width, height);
      pp.setImageableArea(5, 5, width, height);
      pg.setPaper(pp);
      prm.print(g2, pg, 0);
      g2.dispose();
      cb.restoreState();
    }

    document.close();
    return baos.toByteArray();
  }
View Full Code Here

    this.titlePosition = titlePosition;
    initDocument();
  }

  private void initDocument() throws Exception {
    doc = new Document();
    pwriter = PdfWriter.getInstance(doc, new FileOutputStream(filename));
    pwriter.setPageEvent(this);
    width = doc.getPageSize().getWidth() - doc.leftMargin() - doc.rightMargin();
    height = doc.getPageSize().getHeight() - doc.topMargin() - doc.bottomMargin();
    doc.open();
View Full Code Here

        Integer ii = null;
        ArrayList<Character> charList = null;
        ArrayList<Integer> oneDataList = ps.matchIntAllCapturingGroups(seq);
        ArrayList<QuestionData> qdList = generateQuestions(oneDataList, howmany);
        // step 1
        Document document = new Document(PageSize.LETTER);
        // step 2
        ServletContext sc = ServletActionContext.getServletContext();
        String FilePath = sc.getRealPath("/");
        String outPath = (FilePath==null)?"temp/" + FNAME:FilePath + "temp/" + FNAME;
        String pathForJavaScript = "./" + "temp/" + FNAME;
        PdfWriter.getInstance(document, new FileOutputStream(outPath));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("StrutsSchoolWeb - Random Multiplication Questions"));
        document.add(new Paragraph(new Date().toString()));
        document.add(Chunk.NEWLINE);
        //document.add(new Paragraph("Seq:"+seq));
        //  for(QuestionData qd: qdList) {
        //      document.add(new Paragraph(qd.toStringQuestionAndAnswer()));
        //  }

        PdfPTable table = null;
        PdfPCell cell = null;
        int columns = 0;

        PdfPTable tableInner = null;
        PdfPCell cellInner = null;

        table = new PdfPTable(tableColumns);
        table.setWidthPercentage(100);
        table.setSpacingBefore(0);


        for (QuestionData qd : qdList) {
            columns++;

            tableInner = new PdfPTable(3); //Grade 3 questions should be 3 cols only (2 digits and 1 op-code)

            cellInner = new PdfPCell(new Paragraph(" "));
            cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
            cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            tableInner.addCell(cellInner);

            charList = QuestionData.getIntChars(qd.getFactor1());
            if (charList.size() == 1) {
                cellInner = new PdfPCell(new Paragraph(" "));
                cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                tableInner.addCell(cellInner);

                cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
                cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                tableInner.addCell(cellInner);
            } else if (charList.size() >= 2) {
                cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
                cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                tableInner.addCell(cellInner);

                cellInner = new PdfPCell(new Paragraph(charList.get(1).toString()));
                cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                tableInner.addCell(cellInner);
            }
            cellInner = new PdfPCell(new Paragraph(qd.getOperation()));
            cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
            cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            tableInner.addCell(cellInner);

            charList = QuestionData.getIntChars(qd.getFactor2());
            if (charList.size() == 1) {
                cellInner = new PdfPCell(new Paragraph(" "));
                cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                tableInner.addCell(cellInner);

                cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
                cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                tableInner.addCell(cellInner);
            } else if (charList.size() >= 2) {
                cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
                cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                tableInner.addCell(cellInner);

                cellInner = new PdfPCell(new Paragraph(charList.get(1).toString()));
                cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                tableInner.addCell(cellInner);
            }
            // Add top answer line
            cellInner = new PdfPCell(new Paragraph(" "));
            cellInner.setColspan(3);
            cellInner.setBorder(PdfPCell.TOP);
            cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            tableInner.addCell(cellInner);

            if (showAnswers == true) {
                charList = QuestionData.getIntChars(qd.getAnswer());
                switch (charList.size()) {
                    case 3:
                        cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);

                        cellInner = new PdfPCell(new Paragraph(charList.get(1).toString()));
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);

                        cellInner = new PdfPCell(new Paragraph(charList.get(2).toString()));
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);

                        break;
                    case 2:
                        cellInner = new PdfPCell(new Paragraph(" "));
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);

                        cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);

                        cellInner = new PdfPCell(new Paragraph(charList.get(1).toString()));
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);

                        break;
                    case 1:
                        cellInner = new PdfPCell(new Paragraph(" "));
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);

                        cellInner = new PdfPCell(new Paragraph(" "));
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);

                        cellInner = new PdfPCell(new Paragraph(charList.get(0).toString()));
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);

                        break;
                    default:
                        cellInner = new PdfPCell(new Paragraph(" "));
                        cellInner.setColspan(3);
                        cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                        cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                        tableInner.addCell(cellInner);
                }
            }
            //Add 1 blank lines for calculations
            cellInner = new PdfPCell(new Paragraph(" "));
            cellInner.setColspan(3);
            cellInner.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
            cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            tableInner.addCell(cellInner);
            // Add lower Answer line
            cellInner = new PdfPCell(new Paragraph(" "));
            cellInner.setColspan(3);
            cellInner.setBorder(PdfPCell.TOP);
            cellInner.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            tableInner.addCell(cellInner);

            cell = new PdfPCell(tableInner);
            // cell.addElement(tableInner);
            cell.setBorder(PdfPCell.BOX);
            cell.setBorderWidthRight(borderWidth);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            table.addCell(cell);
            if (columns > tableColumns) {
                columns = 0;
            }
        }
        int remaining = tableColumns - columns;
        while (remaining > 0) {
            cell = new PdfPCell();
            cell.setBorder(PdfPCell.BOX);
            cell.setBorderWidthRight(borderWidth);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            table.addCell(cell);
            remaining--;
        }
        document.add(table);
        // but the result looks like one big table

        // step 4
        document.close();
        return (pathForJavaScript);
    }
View Full Code Here

    private String generatePDF(boolean borders, boolean ans, ArrayList<Integer> numbs)
            throws DocumentException, IOException {
       // int[] numbs = {n1, n2, n3, n4, n5};
        // step 1
        Document document = new Document(PageSize.LETTER);
        // step 2
        ServletContext sc = ServletActionContext.getServletContext();
        String FilePath = sc.getRealPath("/");
        String outPath = (FilePath==null)?"temp/" + FNAME:FilePath + "temp/" + FNAME;
        String pathForJavaScript = "./" + "temp/" + FNAME;
        PdfWriter.getInstance(document, new FileOutputStream(outPath));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("StrutsSchoolWeb - Times Table"));
        document.add(new Paragraph(new Date().toString()));
        document.add(Chunk.NEWLINE);

        PdfPTable table = null, tableOuter = null;
        PdfPCell cell = null, cellOuter = null;
        int columns = 0;
        tableOuter = new PdfPTable(tableColumns);
        tableOuter.setWidthPercentage(100);
        tableOuter.setSpacingBefore(0);
        for (Iterator<Integer> it = numbs.iterator(); it.hasNext();) {
            int i = it.next();
            columns++;
            table = new PdfPTable(5);
            table.setWidthPercentage(24);
            table.setSpacingBefore(0);
            cell = new PdfPCell(new Phrase(i + " Times Table"));
            cell.setColspan(5);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
            table.addCell(cell);
            ArrayList<QuestionData> list = generateTable(i);
            for (QuestionData qd : list) {
                cell = new PdfPCell(new Phrase(Integer.toString(qd.getFactor1())));
                cell.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(qd.getOperation()));
                cell.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(Integer.toString(qd.getFactor2())));
                cell.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase("="));
                cell.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.addCell(cell);
                if (ans) {
                    cell = new PdfPCell(new Phrase(Integer.toString(qd.getAnswer())));
                    cell.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                    cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                    table.addCell(cell);
                } else {
                    cell = new PdfPCell();
                    cell.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
                    cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                    table.addCell(cell);
                }
            }
            cellOuter = new PdfPCell(table);
            cellOuter.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
            cellOuter.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            tableOuter.addCell(cellOuter);
            if(columns>tableColumns)
                columns=0;
        }
// if the last row column is less than total columns then add empty cells to complete the last row.
        int remaining = tableColumns-columns;
        while(remaining>0) {
            cellOuter = new PdfPCell();
            // cell.addElement(tableInner);
            cellOuter.setBorder(borders ? PdfPCell.NO_BORDER : PdfPCell.BOX);
            cellOuter.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            tableOuter.addCell(cellOuter);
            remaining--;
        }
        document.add(tableOuter);
        // step 4
        document.close();
        return (pathForJavaScript);
    }
View Full Code Here

        BeefReport.repportf = repportfile ;
        BeefReport.url = url ;
        BeefReport.key = key ;
        BeefReport.logo = logo ;
        String FILE = BeefReport.repportf  ;
      Document document = new Document();
      try {
      PdfWriter.getInstance(document, new FileOutputStream(FILE));
      document.open();
      BeefReport.addMetaData(document);
      BeefReport.addTitlePage(document);
      BeefReport.addContent(document);
      document.close();
    } catch (Exception e) {
    }
    }
View Full Code Here

TOP

Related Classes of com.itextpdf.text.Document

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.