Example usage for com.itextpdf.text Document setPageCount

List of usage examples for com.itextpdf.text Document setPageCount

Introduction

In this page you can find the example usage for com.itextpdf.text Document setPageCount.

Prototype


public void setPageCount(int pageN) 

Source Link

Document

Sets the page number.

Usage

From source file:be.zenodotus.creatie.GeneratePDF.java

License:Open Source License

public String vakantieAfdruk(Context context, String name, int jaar) {
    this.context = context;
    PdfWriter w = null;/*from  w ww . ja  va 2  s  .c o  m*/
    Document d = new Document(PageSize.A4.rotate(), 5, 5, 10, 10);
    d.setPageCount(3);
    String fileName = name;
    String file = name;
    GregorianCalendar datum = new GregorianCalendar();
    datum.set(GregorianCalendar.YEAR, jaar);

    String[] maanden = { "Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus",
            "September", "Oktober", "November", "December" };
    int[] dagen = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    VerlofDao dao = new VerlofDao(context);
    FeestdagDao feestdagDao = new FeestdagDao(context);
    WerkdagDao werkdagDao = new WerkdagDao(context);
    File folder = new File(context.getFilesDir(), "pdfs");
    folder.mkdirs();
    if (datum.isLeapYear(jaar)) {
        dagen[1] = 29;
    }
    File temp = null;
    temp = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
            "Jacqueline" + jaar + ".pdf");
    fileName = temp.toString();
    try {
        dao.open();
        ArrayList<Verlof> verloflijst = dao.getAlleVerlovenPerJaar(jaar);
        w = PdfWriter.getInstance(d, new FileOutputStream(temp));
        d.open();
        d.addAuthor("Jacqueline Vandenbroecke");
        d.addCreationDate();
        d.addCreator("Verlofplanner");
        d.addTitle("Vakantie " + jaar + " van Jacqueline Vandenbroecke");
        Font standaard = FontFactory.getFont(FontFactory.HELVETICA, 8);
        Font standaardBold = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD);
        Paragraph gegeven = new Paragraph("Jacqueline Vandenbroecke Verlof " + jaar + "\n", standaardBold);
        gegeven.setAlignment(Paragraph.ALIGN_CENTER);
        d.add(gegeven);

        for (int paginas = 0; paginas < 2; paginas++) {
            int aantal = 0;
            if (paginas == 1) {
                d.newPage();
                aantal = 6;
            }
            PdfPTable table = new PdfPTable(6);
            for (int i = aantal; i < (aantal + 6); i++) {
                PdfPCell cell = new PdfPCell(new Paragraph(maanden[i], standaardBold));
                cell.setBorder(1);
                table.addCell(cell);
            }
            int dag = 1;
            int k = aantal;
            for (int i = aantal; i < (aantal + 6); i++) {
                for (int j = 0; j < 32; j++) {
                    if (k > ((aantal + 6) - 1)) {
                        k = aantal;
                        dag++;
                    }
                    if (dag > dagen[k]) {
                        PdfPCell cell = new PdfPCell(new Paragraph("", standaard));
                        table.addCell(cell);
                        k++;
                    } else {
                        SimpleDateFormat formatterDag = new SimpleDateFormat("dd");
                        SimpleDateFormat formatterWeek = new SimpleDateFormat("EEE");
                        datum.set(jaar, k, dag);
                        PdfPTable dagTabel = new PdfPTable(4);
                        PdfPCell cellDag = new PdfPCell(
                                new Paragraph(formatterDag.format(datum.getTime()), standaard));
                        PdfPCell cellWeek = new PdfPCell(
                                new Paragraph(formatterWeek.format(datum.getTime()), standaard));
                        ArrayList<Verlof> verlof = new ArrayList<Verlof>();
                        for (int z = 0; z < verloflijst.size(); z++) {
                            if (((verloflijst.get(z).getDag() + 1) == dag)
                                    && (verloflijst.get(z).getMaand() == k)) {
                                verlof.add(verloflijst.get(z));

                            }
                        }
                        feestdagDao.open();
                        Feestdag feestdag = feestdagDao.getFeestdag(jaar, datum.get(GregorianCalendar.MONTH),
                                datum.get(GregorianCalendar.DATE));
                        feestdagDao.close();
                        werkdagDao.open();
                        java.util.List<Werkdag> weekend = werkdagDao.getWeekend();
                        werkdagDao.close();
                        String Verlof = "";
                        String uur = "";
                        if (verlof.size() > 0) {
                            if (verlof.size() > 1) {
                                Verlof = verlof.get(0).getVerlofsoort() + "\n" + verlof.get(1).getVerlofsoort();
                                uur = verlof.get(0).getUrental() + "\n" + verlof.get(1).getUrental();
                            } else {
                                Verlof = verlof.get(0).getVerlofsoort();
                                uur = verlof.get(0).getUrental();
                            }

                        }
                        PdfPCell cellVerlof = new PdfPCell(new Paragraph(Verlof, standaard));
                        PdfPCell uren = new PdfPCell(new Paragraph(uur, standaard));
                        if (verlof.size() > 0) {
                            BaseColor kleur = new BaseColor(Color.GRAY);
                            cellVerlof.setBackgroundColor(kleur);
                            uren.setBackgroundColor(kleur);
                            cellDag.setBackgroundColor(kleur);
                            cellWeek.setBackgroundColor(kleur);
                        }
                        for (int z = 0; z < weekend.size(); z++) {
                            if ((formatterWeek.format(datum.getTime())).equals(weekend.get(z).getDag())) {
                                BaseColor kleur = new BaseColor(Color.LTGRAY);
                                cellVerlof.setBackgroundColor(kleur);
                                uren.setBackgroundColor(kleur);
                                cellDag.setBackgroundColor(kleur);
                                cellWeek.setBackgroundColor(kleur);
                            }
                        }
                        if (feestdag != null) {
                            BaseColor kleur = new BaseColor(Color.GREEN);
                            uren.setBackgroundColor(kleur);
                            cellVerlof.setBackgroundColor(kleur);
                            uren.setBackgroundColor(kleur);
                            cellDag.setBackgroundColor(kleur);
                            cellWeek.setBackgroundColor(kleur);
                        }
                        dagTabel.addCell(cellDag);
                        dagTabel.addCell(cellWeek);
                        dagTabel.addCell(cellVerlof);
                        dagTabel.addCell(uren);
                        table.addCell(dagTabel);
                        k++;
                    }
                }

            }
            d.add(table);
            dao.close();
        }
    } catch (Exception ex) {

        ex.printStackTrace();

    } finally {
        d.close();
        w.close();
    }
    return fileName;
}

From source file:com.bdaum.zoom.email.internal.job.PdfJob.java

License:Open Source License

private void printPage(Document document, int pageNo, MultiStatus status, IProgressMonitor monitor)
        throws DocumentException, IOException {
    if (pageNo > 1)
        document.newPage();//w  ww  .j  av  a2s  .  c  om
    document.setPageCount(pageNo);
    final Display display = Display.getDefault();
    int pageItem = 0;
    if (!layout.getTitle().isEmpty()) {
        String title = PageProcessor.computeTitle(layout.getTitle(), fileName, now, assets.size(), pageNo,
                pages, collection, meta);
        Paragraph p = new Paragraph(title,
                FontFactory.getFont(FontFactory.HELVETICA, titleSize, Font.BOLD, BaseColor.DARK_GRAY));
        p.setAlignment(Element.ALIGN_CENTER);
        p.setSpacingAfter(layout.getSubtitle().isEmpty() ? titleLead + upperWaste : titleLead);
        document.add(p);
    }
    if (!layout.getSubtitle().isEmpty()) {
        String subtitle = PageProcessor.computeTitle(layout.getSubtitle(), fileName, now, assets.size(), pageNo,
                pages, collection, meta);
        Paragraph p = new Paragraph(subtitle,
                FontFactory.getFont(FontFactory.HELVETICA, subtitleSize, Font.NORMAL, BaseColor.DARK_GRAY));
        p.setAlignment(Element.ALIGN_CENTER);
        p.setSpacingAfter(subtitleLead + upperWaste);
        document.add(p);
    }
    IVolumeManager vm = Core.getCore().getVolumeManager();
    PdfPTable table = new PdfPTable(layout.getColumns());
    try (Assetbox box = new Assetbox(null, status, false)) {
        for (int i = 0; i < rows; i++) {
            int ni = i * layout.getColumns();
            for (int j = 0; j < layout.getColumns(); j++) {
                int a = (pageNo - 1) * imagesPerPage + ni + j;
                PdfPCell cell;
                if (a >= assets.size() || monitor.isCanceled())
                    cell = new PdfPCell();
                else {
                    final int dpi = quality == Constants.SCREEN_QUALITY ? SCREENDPI : PRINTERDPI;
                    final int pixelWidth = (int) (imageWidth * dpi / 72);
                    final int pixelHeight = (int) (imageHeight * dpi / 72);
                    Asset asset = assets.get(a);
                    zimage = new ZImage(ImageUtilities.loadThumbnail(display, asset.getJpegThumbnail(), cms,
                            SWT.IMAGE_JPEG, true), null);
                    org.eclipse.swt.graphics.Rectangle bounds = zimage.getBounds();
                    URI uri = vm.findExistingFile(asset, false);
                    if (uri != null) {
                        boolean r = asset.getRotation() % 180 != 0;
                        double w = r ? asset.getHeight() : asset.getWidth();
                        double h = r ? asset.getWidth() : asset.getHeight();
                        double scale = w == 0 || h == 0 ? 1d : Math.min(pixelWidth / w, pixelHeight / h);
                        scale = (scale <= 0.5d) ? 0.5d : 1d;
                        File file = box.obtainFile(uri);
                        if (file != null)
                            try {
                                ZImage hzimage = CoreActivator.getDefault().getHighresImageLoader().loadImage(
                                        null, status, file, asset.getRotation(),
                                        asset.getFocalLengthIn35MmFilm(), null, scale, Double.MAX_VALUE, true,
                                        ImageConstants.SRGB, null, unsharpMask, null, fileWatcher, opId, null);
                                if (hzimage != null) {
                                    zimage.dispose();
                                    zimage = hzimage;
                                }
                            } catch (UnsupportedOperationException e) {
                                // do nothing
                            }
                    }
                    display.syncExec(() -> {
                        int kl = (keyLine > 0) ? (int) Math.max(1, (keyLine * dpi / 144)) : 0;
                        org.eclipse.swt.graphics.Rectangle ibounds = zimage.getBounds();
                        double factor = Math.min((double) pixelWidth / ibounds.width,
                                (double) pixelHeight / ibounds.height);
                        int lw = pixelWidth + 2 * kl;
                        int lh = pixelHeight + 2 * kl;
                        Image newImage = new Image(display, lw, lh);
                        GC gc = new GC(newImage);
                        try {
                            gc.setAntialias(SWT.ON);
                            gc.setInterpolation(SWT.HIGH);
                            gc.setAdvanced(true);
                            gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
                            gc.fillRectangle(0, 0, lw, lh);
                            int width = (int) (ibounds.width * factor + 2 * kl);
                            int height = (int) (ibounds.height * factor + 2 * kl);
                            int xoff = (lw - width) / 2;
                            int yoff = (lh - height) / 2;
                            if (kl > 0) {
                                gc.setBackground(display.getSystemColor(SWT.COLOR_DARK_GRAY));
                                gc.fillRectangle(xoff, yoff, width, height);
                            }
                            zimage.draw(gc, 0, 0, ibounds.width, ibounds.height, xoff + kl, yoff + kl,
                                    width - 2 * kl, height - 2 * kl, ZImage.CROPPED, pixelWidth, pixelHeight,
                                    false);
                        } finally {
                            gc.dispose();
                            zimage.dispose();
                            zimage = new ZImage(newImage, null);
                        }
                    });
                    bounds = zimage.getBounds();
                    File jpegFile = ImageActivator.getDefault().createTempFile("PdfImg", ".jpg"); //$NON-NLS-1$//$NON-NLS-2$
                    tempFiles.add(jpegFile);
                    try (FileOutputStream out = new FileOutputStream(jpegFile)) {
                        zimage.saveToStream(monitor, true, ZImage.UNCROPPED, SWT.DEFAULT, SWT.DEFAULT, out,
                                SWT.IMAGE_JPEG, jpegQuality);
                    }
                    zimage.dispose();
                    com.itextpdf.text.Image pdfImage = com.itextpdf.text.Image.getInstance(jpegFile.getPath());
                    double factor = Math.min(imageWidth / bounds.width, imageHeight / bounds.height);
                    pdfImage.setInterpolation(true);
                    pdfImage.scaleToFit((float) (bounds.width * factor), (float) (bounds.height * factor));
                    cell = new PdfPCell(pdfImage, false);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    monitor.worked(1);
                }
                cell.setFixedHeight((float) imageHeight);
                cell.setBorderWidth(0);
                table.addCell(cell);
            }
            renderCaptions(pageNo, seqNo, pageItem, table, ni, layout.getCaption1());
            renderCaptions(pageNo, seqNo, pageItem, table, ni, layout.getCaption2());
            pageItem += layout.getColumns();
            seqNo += layout.getColumns();
            if (verticalGap > 0 && i < rows - 1)
                for (int j = 0; j < layout.getColumns(); j++) {
                    PdfPCell cell = new PdfPCell();
                    cell.setFixedHeight(verticalGap);
                    cell.setBorderWidth(0);
                    table.addCell(cell);
                }
        }
        table.setWidthPercentage(100f);
        document.add(table);
        if (!layout.getFooter().isEmpty()) {
            String footer = PageProcessor.computeTitle(layout.getFooter(), fileName, now, assets.size(), pageNo,
                    pages, collection, meta);
            Paragraph p = new Paragraph(footer,
                    FontFactory.getFont(FontFactory.HELVETICA, subtitleSize, Font.NORMAL, BaseColor.DARK_GRAY));
            p.setAlignment(Element.ALIGN_CENTER);
            p.setSpacingBefore(upperWaste / 2 + footerLead);
            document.add(p);
        }
    }
}