Example usage for com.lowagie.text Chunk setLocalDestination

List of usage examples for com.lowagie.text Chunk setLocalDestination

Introduction

In this page you can find the example usage for com.lowagie.text Chunk setLocalDestination.

Prototype

public Chunk setLocalDestination(String name) 

Source Link

Document

Sets a local destination for this Chunk.

Usage

From source file:ambit.data.qmrf.Qmrf_Xml_Pdf.java

License:Open Source License

public void xml2pdf(InputSource xml, OutputStream pdf) {

    try {/*from   www  . j av a 2  s .  co m*/
        Document document = new Document(PageSize.A4, 80, 50, 30, 65);

        PdfWriter writer = PdfWriter.getInstance(document, pdf);
        //writer.setViewerPreferences(PdfWriter.HideMenubar| PdfWriter.HideToolbar);
        writer.setViewerPreferences(PdfWriter.PageModeUseThumbs | PdfWriter.PageModeUseOutlines);
        //PdfOutline root = writer.getDirectContent().getRootOutline();
        //new PdfOutline(root, new PDFAction("http://nina.acad.bg/qmrf"), "a bookmark");

        //writer.addFileAttachment(arg0, arg1, arg2, arg3)

        if (docBuilder == null)
            docBuilder = docBuilderFactory.newDocumentBuilder();
        docBuilder.setErrorHandler(new SimpleErrorHandler(getClass().getName()));
        QMRFSchemaResolver resolver = new QMRFSchemaResolver("http://ambit.acad.bg/qmrf/qmrf.dtd", null);
        resolver.setIgnoreSystemID(true);
        docBuilder.setEntityResolver(resolver);

        org.w3c.dom.Document doc = null;
        try {
            doc = docBuilder.parse(xml);
        } catch (Exception x) {
            document.addCreationDate();
            document.addCreator(getClass().getName());
            document.open();
            document.add(new Paragraph(new Chunk(x.getMessage())));
            document.close();
            return;
        }

        document.addCreationDate();
        document.addCreator(getClass().getName());
        document.addKeywords(replaceTags(findNodeValue("keywords", doc)));
        document.addTitle(replaceTags(findNodeValue("QSAR_title", doc)));

        try {
            NodeList info = doc.getElementsByTagName("QMRF");
            for (int i = 0; i < info.getLength(); i++)
                document.addSubject(findAttributeValue("name", info.item(i)) + '.'
                        + findAttributeValue("version", info.item(i)));
        } catch (Exception x) {
            document.addSubject("QMRF");
        }
        try {
            document.addAuthor(listNodeAttributes(doc, "qmrf_authors", "author_ref", "author", att_author,
                    new Boolean(true)));
        } catch (Exception x) {
            document.addAuthor(getClass().getName());
        }

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

        try {
            headerTable(document, doc);
        } catch (Exception x) {
            document.add(new Paragraph(new Chunk(x.getMessage())));
            document.close();
            return;
        }

        PdfOutline root = writer.getDirectContent().getRootOutline();

        for (int i = 0; i < subchapters.length; i++)

            try {
                int align = Paragraph.ALIGN_LEFT;
                if (Mode.chapter == (Mode) subchapters[i][2]) {
                    document.add(new Paragraph(new Chunk('\n')));
                    PdfPTable table = new PdfPTable(1);

                    table.setWidthPercentage(100);

                    StringBuffer b = new StringBuffer();
                    b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc));
                    b.append('.');
                    b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc));

                    String bookmark = b.toString();

                    Chunk title = new Chunk(bookmark);
                    title.setLocalDestination(bookmark);
                    title.setFont(bfont);

                    PdfDestination destination = new PdfDestination(PdfDestination.FITH);
                    PdfOutline outline = new PdfOutline(root, destination, bookmark);

                    Paragraph p = new Paragraph(title);

                    PdfPCell cell = new PdfPCell(p);
                    cell.setBackgroundColor(chapterColor);
                    table.addCell(cell);
                    document.add(table);
                    float pos = writer.getVerticalPosition(false);
                    if (pos < 90)
                        document.newPage();
                } else {
                    Phrase phrase = new Phrase();
                    switch ((Mode) subchapters[i][2]) {
                    case title: {
                        StringBuffer b = new StringBuffer();
                        String cn = findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter,
                                doc);
                        if (cn == null)
                            break;

                        b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc));
                        b.append('.');
                        b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc));

                        String subchapterBookmark = b.toString();
                        b.append(':');

                        Chunk title = new Chunk(b.toString());
                        title.setLocalDestination(subchapterBookmark);
                        title.setFont(bfont);
                        phrase.add(title);

                        PdfDestination destination = new PdfDestination(PdfDestination.FITBH);
                        PdfOutline outline = new PdfOutline(root, destination, subchapterBookmark);

                        break;

                    }
                    case text: {
                        createNodePhrase(subchapters[i][0].toString(), doc, phrase, font);
                        align = Paragraph.ALIGN_JUSTIFIED;
                        break;
                    }
                    case answer: {
                        String a = findAnswer(subchapters[i][0].toString(), doc);
                        if (a != null) {
                            Chunk answer = new Chunk(a);
                            answer.setFont(font);
                            phrase.add(answer);
                        }
                        break;
                    }
                    case dataset: {
                        StringBuffer b = new StringBuffer();
                        b.append(findDataAvailable(subchapters[i][0].toString(), doc));
                        Chunk dataset = new Chunk(b.toString());
                        dataset.setFont(font);
                        phrase.add(dataset);
                        break;
                    }
                    case attachments: {
                        PdfPTable table = getAttachmentsAsTable(doc, "attachment_training_data");
                        if (table != null) {
                            phrase.add(new Paragraph("Training set(s)"));
                            phrase.add(table);
                        }
                        table = getAttachmentsAsTable(doc, "attachment_validation_data");
                        if (table != null) {
                            phrase.add(new Paragraph("Test set(s)"));
                            phrase.add(table);
                        }
                        table = getAttachmentsAsTable(doc, "attachment_documents");
                        if (table != null) {
                            phrase.add(new Paragraph("Supporting information"));
                            phrase.add(table);
                        }
                        break;
                        /*
                        StringBuffer b = new StringBuffer();
                        b.append("Training set(s)\n");
                         b.append(listAttachments(doc,"attachment_training_data"));
                         b.append("Test set(s)\n");
                         b.append(listAttachments(doc,"attachment_validation_data"));
                         b.append("Supporting information\n");
                         b.append(listAttachments(doc,"attachment_documents"));
                        Chunk attachments = new Chunk(b.toString());
                        attachments.setFont(font);
                        phrase.add(attachments);
                                
                        break;
                        */
                    }
                    case reference: {
                        try {
                            String value = listNodeAttributes(doc, subchapters[i][0].toString(),
                                    subchapters[i][3].toString(), subchapters[i][4].toString(),
                                    (String[]) subchapters[i][5], (Boolean) subchapters[i][6]);

                            Chunk reference = new Chunk(value);
                            reference.setFont(font);
                            align = Paragraph.ALIGN_JUSTIFIED;
                            phrase.add(reference);
                        } catch (Exception x) {

                        }
                        break;

                    }
                    }

                    Paragraph p = new Paragraph(phrase);
                    p.setAlignment(align);
                    document.add(p);
                    float pos = writer.getVerticalPosition(false);
                    /*
                    cb.moveTo(0, pos);
                    cb.lineTo(PageSize.A4.width(), pos);
                    cb.stroke();
                    */
                    if (pos < 90)
                        document.newPage();
                }
            } catch (Exception x) {
                x.printStackTrace();
            }

        document.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
    }
}

From source file:ambit2.data.qmrf.Qmrf_Xml_Pdf.java

License:Open Source License

public void xml2pdf(InputSource xml, OutputStream pdf) {

    try {//from   w  w  w.  j  a va2  s  . c  om
        Document document = new Document(PageSize.A4, 80, 50, 30, 65);

        PdfWriter writer = PdfWriter.getInstance(document, pdf);
        //writer.setViewerPreferences(PdfWriter.HideMenubar| PdfWriter.HideToolbar);
        writer.setViewerPreferences(PdfWriter.PageModeUseThumbs | PdfWriter.PageModeUseOutlines);
        //PdfOutline root = writer.getDirectContent().getRootOutline();
        //new PdfOutline(root, new PDFAction("http://nina.acad.bg/qmrf"), "a bookmark");

        //writer.addFileAttachment(arg0, arg1, arg2, arg3)

        if (docBuilder == null)
            docBuilder = docBuilderFactory.newDocumentBuilder();
        docBuilder.setErrorHandler(new SimpleErrorHandler(getClass().getName()));
        QMRFSchemaResolver resolver = new QMRFSchemaResolver("http://ambit2.acad.bg/qmrf/qmrf.dtd", null);
        resolver.setIgnoreSystemID(true);
        docBuilder.setEntityResolver(resolver);

        org.w3c.dom.Document doc = null;
        try {
            doc = docBuilder.parse(xml);
        } catch (Exception x) {
            document.addCreationDate();
            document.addCreator(getClass().getName());
            document.open();
            document.add(new Paragraph(new Chunk(x.getMessage())));
            document.close();
            return;
        }

        document.addCreationDate();
        document.addCreator(getClass().getName());
        document.addKeywords(replaceTags(findNodeValue("keywords", doc)));
        document.addTitle(replaceTags(findNodeValue("QSAR_title", doc)));

        try {
            NodeList info = doc.getElementsByTagName("QMRF");
            for (int i = 0; i < info.getLength(); i++)
                document.addSubject(findAttributeValue("name", info.item(i)) + '.'
                        + findAttributeValue("version", info.item(i)));
        } catch (Exception x) {
            document.addSubject("QMRF");
        }
        try {
            document.addAuthor(listNodeAttributes(doc, "qmrf_authors", "author_ref", "author", att_author,
                    new Boolean(true)));
        } catch (Exception x) {
            document.addAuthor(getClass().getName());
        }

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

        try {
            headerTable(document, doc);
        } catch (Exception x) {
            document.add(new Paragraph(new Chunk(x.getMessage())));
            document.close();
            return;
        }

        PdfOutline root = writer.getDirectContent().getRootOutline();

        for (int i = 0; i < subchapters.length; i++)

            try {
                int align = Paragraph.ALIGN_LEFT;
                if (Mode.chapter == (Mode) subchapters[i][2]) {
                    document.add(new Paragraph(new Chunk('\n')));
                    PdfPTable table = new PdfPTable(1);

                    table.setWidthPercentage(100);

                    StringBuffer b = new StringBuffer();
                    b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc));
                    b.append('.');
                    b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc));

                    String bookmark = b.toString();

                    Chunk title = new Chunk(bookmark);
                    title.setLocalDestination(bookmark);
                    title.setFont(bfont);

                    PdfDestination destination = new PdfDestination(PdfDestination.FITH);
                    PdfOutline outline = new PdfOutline(root, destination, bookmark);

                    Paragraph p = new Paragraph(title);

                    PdfPCell cell = new PdfPCell(p);
                    cell.setBackgroundColor(chapterColor);
                    table.addCell(cell);
                    document.add(table);
                    float pos = writer.getVerticalPosition(false);
                    if (pos < 90)
                        document.newPage();
                } else {
                    Phrase phrase = new Phrase();
                    switch ((Mode) subchapters[i][2]) {
                    case title: {
                        StringBuffer b = new StringBuffer();
                        String cn = findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter,
                                doc);
                        if (cn == null)
                            break;

                        b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc));
                        b.append('.');
                        b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc));

                        String subchapterBookmark = b.toString();
                        b.append(':');

                        Chunk title = new Chunk(b.toString());
                        title.setLocalDestination(subchapterBookmark);
                        title.setFont(bfont);
                        phrase.add(title);

                        PdfDestination destination = new PdfDestination(PdfDestination.FITBH);
                        PdfOutline outline = new PdfOutline(root, destination, subchapterBookmark);

                        break;

                    }
                    case text: {
                        createNodePhrase(subchapters[i][0].toString(), doc, phrase, font);
                        align = Paragraph.ALIGN_JUSTIFIED;
                        break;
                    }
                    case answer: {
                        String a = findAnswer(subchapters[i][0].toString(), doc);
                        if (a != null) {
                            Chunk answer = new Chunk(a);
                            answer.setFont(font);
                            phrase.add(answer);
                        }
                        break;
                    }
                    case dataset: {
                        StringBuffer b = new StringBuffer();
                        b.append(findDataAvailable(subchapters[i][0].toString(), doc));
                        Chunk dataset = new Chunk(b.toString());
                        dataset.setFont(font);
                        phrase.add(dataset);
                        break;
                    }
                    case attachments: {
                        PdfPTable table = getAttachmentsAsTable(doc, "attachment_training_data");
                        if (table != null) {
                            phrase.add(new Paragraph("Training set(s)"));
                            phrase.add(table);
                        }
                        table = getAttachmentsAsTable(doc, "attachment_validation_data");
                        if (table != null) {
                            phrase.add(new Paragraph("Test set(s)"));
                            phrase.add(table);
                        }
                        table = getAttachmentsAsTable(doc, "attachment_documents");
                        if (table != null) {
                            phrase.add(new Paragraph("Supporting information"));
                            phrase.add(table);
                        }
                        break;
                        /*
                        StringBuffer b = new StringBuffer();
                        b.append("Training set(s)\n");
                         b.append(listAttachments(doc,"attachment_training_data"));
                         b.append("Test set(s)\n");
                         b.append(listAttachments(doc,"attachment_validation_data"));
                         b.append("Supporting information\n");
                         b.append(listAttachments(doc,"attachment_documents"));
                        Chunk attachments = new Chunk(b.toString());
                        attachments.setFont(font);
                        phrase.add(attachments);
                                
                        break;
                        */
                    }
                    case reference: {
                        try {
                            String value = listNodeAttributes(doc, subchapters[i][0].toString(),
                                    subchapters[i][3].toString(), subchapters[i][4].toString(),
                                    (String[]) subchapters[i][5], (Boolean) subchapters[i][6]);

                            Chunk reference = new Chunk(value);
                            reference.setFont(font);
                            align = Paragraph.ALIGN_JUSTIFIED;
                            phrase.add(reference);
                        } catch (Exception x) {

                        }
                        break;

                    }
                    }

                    Paragraph p = new Paragraph(phrase);
                    p.setAlignment(align);
                    document.add(p);
                    float pos = writer.getVerticalPosition(false);
                    /*
                    cb.moveTo(0, pos);
                    cb.lineTo(PageSize.A4.width(), pos);
                    cb.stroke();
                    */
                    if (pos < 90)
                        document.newPage();
                }
            } catch (Exception x) {
                x.printStackTrace();
            }

        document.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
    }
}

From source file:com.develog.utils.report.engine.export.JRPdfExporter.java

License:Open Source License

/**
 *
 *///w  w  w.j  a va  2  s.c om
protected void exportReportToStream(OutputStream os) throws JRException {
    //ByteArrayOutputStream baos = new ByteArrayOutputStream();

    document = new Document(new Rectangle(jasperPrint.getPageWidth(), jasperPrint.getPageHeight()));

    imageTesterDocument = new Document(new Rectangle(10, //jasperPrint.getPageWidth(),
            10 //jasperPrint.getPageHeight()
    ));

    try {
        PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
        pdfWriter.setCloseStream(false);

        if (isEncrypted) {
            pdfWriter.setEncryption(is128BitKey, userPassword, ownerPassword, permissions);
        }

        document.open();

        PdfWriter imageTesterPdfWriter = PdfWriter.getInstance(imageTesterDocument,
                new ByteArrayOutputStream());
        imageTesterDocument.open();
        imageTesterDocument.newPage();
        imageTesterPdfContentByte = imageTesterPdfWriter.getDirectContent();
        imageTesterPdfContentByte.setLiteral("\n");

        List pages = jasperPrint.getPages();
        if (pages != null && pages.size() > 0) {
            Chunk chunk = null;
            ColumnText colText = null;
            JRPrintPage page = null;
            for (int i = startPageIndex; i <= endPageIndex; i++) {
                if (Thread.currentThread().isInterrupted()) {
                    throw new JRException("Current thread interrupted.");
                }

                page = (JRPrintPage) pages.get(i);

                document.newPage();

                pdfContentByte = pdfWriter.getDirectContent();

                pdfContentByte.setLineCap(2);

                chunk = new Chunk(" ");
                chunk.setLocalDestination("JR_PAGE_ANCHOR_" + (i + 1));

                colText = new ColumnText(pdfContentByte);
                colText.setSimpleColumn(new Phrase(chunk), 0, jasperPrint.getPageHeight(), 1, 1, 0,
                        Element.ALIGN_LEFT);

                colText.go();

                /*   */
                exportPage(page);
            }
        } else {
            document.newPage();
            pdfContentByte = pdfWriter.getDirectContent();
            pdfContentByte.setLiteral("\n");
        }
    } catch (DocumentException e) {
        throw new JRException("PDF Document error : " + jasperPrint.getName(), e);
    } catch (IOException e) {
        throw new JRException("Error generating PDF report : " + jasperPrint.getName(), e);
    } finally {
        document.close();
        imageTesterDocument.close();
    }

    //return os.toByteArray();
}

From source file:com.develog.utils.report.engine.export.JRPdfExporter.java

License:Open Source License

/**
 *
 *///from   w  ww  .  j  a v a2  s  .  c om
protected void exportImage(JRPrintImage printImage) throws DocumentException, IOException {
    pdfContentByte.setRGBColorFill(printImage.getBackcolor().getRed(), printImage.getBackcolor().getGreen(),
            printImage.getBackcolor().getBlue());

    int borderOffset = 0;
    float borderCorrection = 0f;
    float lineWidth = 1f;
    boolean isLineDotted = false;

    switch (printImage.getPen()) {
    case JRGraphicElement.PEN_DOTTED: {
        borderOffset = 0;
        borderCorrection = 0f;
        lineWidth = 1f;
        isLineDotted = true;
        break;
    }
    case JRGraphicElement.PEN_4_POINT: {
        borderOffset = 2;
        borderCorrection = 0f;
        lineWidth = 4f;
        isLineDotted = false;
        break;
    }
    case JRGraphicElement.PEN_2_POINT: {
        borderOffset = 1;
        borderCorrection = 0f;
        lineWidth = 2f;
        isLineDotted = false;
        break;
    }
    case JRGraphicElement.PEN_THIN: {
        borderOffset = 0;
        borderCorrection = 0.25f;
        lineWidth = 0.5f;
        isLineDotted = false;
        break;
    }
    case JRGraphicElement.PEN_NONE: {
        borderOffset = 0;
        borderCorrection = 0.5f;
        lineWidth = 1f;
        isLineDotted = false;

        break;
    }
    case JRGraphicElement.PEN_1_POINT:
    default: {
        borderOffset = 0;
        borderCorrection = 0f;
        lineWidth = 1f;
        isLineDotted = false;
        break;
    }
    }

    if (printImage.getMode() == JRElement.MODE_OPAQUE) {
        pdfContentByte.setRGBColorStroke(printImage.getBackcolor().getRed(),
                printImage.getBackcolor().getGreen(), printImage.getBackcolor().getBlue());
        pdfContentByte.setLineWidth(0.1f);
        pdfContentByte.setLineDash(0f);
        pdfContentByte.rectangle(printImage.getX() - borderCorrection,
                jasperPrint.getPageHeight() - printImage.getY() + borderCorrection,
                printImage.getWidth() + 2 * borderCorrection - 1,
                -printImage.getHeight() - 2 * borderCorrection + 1);
        pdfContentByte.fillStroke();
    }

    int availableImageWidth = printImage.getWidth() - 2 * borderOffset;
    availableImageWidth = (availableImageWidth < 0) ? 0 : availableImageWidth;

    int availableImageHeight = printImage.getHeight() - 2 * borderOffset;
    availableImageHeight = (availableImageHeight < 0) ? 0 : availableImageHeight;

    int xoffset = 0;
    int yoffset = 0;

    if (printImage.getImageData() != null && availableImageWidth > 0 && availableImageHeight > 0) {
        //java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData());

        //com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(awtImage, printImage.getBackcolor());
        //com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(awtImage, null);
        com.lowagie.text.Image image = null;

        float xalignFactor = 0f;
        switch (printImage.getHorizontalAlignment()) {
        case JRAlignment.HORIZONTAL_ALIGN_RIGHT: {
            xalignFactor = 1f;
            break;
        }
        case JRAlignment.HORIZONTAL_ALIGN_CENTER: {
            xalignFactor = 0.5f;
            break;
        }
        case JRAlignment.HORIZONTAL_ALIGN_LEFT:
        default: {
            xalignFactor = 0f;
            break;
        }
        }

        float yalignFactor = 0f;
        switch (printImage.getVerticalAlignment()) {
        case JRAlignment.VERTICAL_ALIGN_BOTTOM: {
            yalignFactor = 1f;
            break;
        }
        case JRAlignment.VERTICAL_ALIGN_MIDDLE: {
            yalignFactor = 0.5f;
            break;
        }
        case JRAlignment.VERTICAL_ALIGN_TOP:
        default: {
            yalignFactor = 0f;
            break;
        }
        }

        switch (printImage.getScaleImage()) {
        case JRImage.SCALE_IMAGE_CLIP: {
            java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData());
            //image = com.lowagie.text.Image.getInstance(awtImage, null);

            int awtWidth = awtImage.getWidth(null);
            int awtHeight = awtImage.getHeight(null);

            xoffset = (int) (xalignFactor * (availableImageWidth - awtWidth));
            yoffset = (int) (yalignFactor * (availableImageHeight - awtHeight));

            int minWidth = Math.min(awtWidth, availableImageWidth);
            int minHeight = Math.min(awtHeight, availableImageHeight);

            BufferedImage bi = new BufferedImage(minWidth, minHeight, BufferedImage.TYPE_INT_RGB);

            Graphics g = bi.getGraphics();
            g.setColor(printImage.getBackcolor());
            g.fillRect(0, 0, minWidth, minHeight);
            g.drawImage(awtImage, (xoffset > 0 ? 0 : xoffset), (yoffset > 0 ? 0 : yoffset), null);

            xoffset = (xoffset < 0 ? 0 : xoffset);
            yoffset = (yoffset < 0 ? 0 : yoffset);

            //awtImage = bi.getSubimage(0, 0, minWidth, minHeight);
            awtImage = bi;

            //image = com.lowagie.text.Image.getInstance(awtImage, printImage.getBackcolor());
            image = com.lowagie.text.Image.getInstance(awtImage, null);

            break;
        }
        case JRImage.SCALE_IMAGE_FILL_FRAME: {
            try {
                image = com.lowagie.text.Image.getInstance(printImage.getImageData());
                imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
            } catch (Exception e) {
                java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData());
                image = com.lowagie.text.Image.getInstance(awtImage, null);
            }
            image.scaleAbsolute(availableImageWidth, availableImageHeight);
            break;
        }
        case JRImage.SCALE_IMAGE_RETAIN_SHAPE:
        default: {
            try {
                image = com.lowagie.text.Image.getInstance(printImage.getImageData());
                imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
            } catch (Exception e) {
                java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData());
                image = com.lowagie.text.Image.getInstance(awtImage, null);
            }
            image.scaleToFit(availableImageWidth, availableImageHeight);

            xoffset = (int) (xalignFactor * (availableImageWidth - image.plainWidth()));
            yoffset = (int) (yalignFactor * (availableImageHeight - image.plainHeight()));

            xoffset = (xoffset < 0 ? 0 : xoffset);
            yoffset = (yoffset < 0 ? 0 : yoffset);

            break;
        }
        }

        /*
        image.setAbsolutePosition(
           printImage.getX() + borderOffset,
           jasperPrint.getPageHeight() - printImage.getY() - image.scaledHeight() - borderOffset
           );
                
        pdfContentByte.addImage(image);
        */

        Chunk chunk = new Chunk(image, -0.5f, 0.5f);

        if (printImage.getAnchorName() != null) {
            chunk.setLocalDestination(printImage.getAnchorName());
        }

        switch (printImage.getHyperlinkType()) {
        case JRHyperlink.HYPERLINK_TYPE_REFERENCE: {
            if (printImage.getHyperlinkReference() != null) {
                chunk.setAnchor(printImage.getHyperlinkReference());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR: {
            if (printImage.getHyperlinkAnchor() != null) {
                chunk.setLocalGoto(printImage.getHyperlinkAnchor());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE: {
            if (printImage.getHyperlinkPage() != null) {
                chunk.setLocalGoto("JR_PAGE_ANCHOR_" + printImage.getHyperlinkPage().toString());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR: {
            if (printImage.getHyperlinkReference() != null && printImage.getHyperlinkAnchor() != null) {
                chunk.setRemoteGoto(printImage.getHyperlinkReference(), printImage.getHyperlinkAnchor());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE: {
            if (printImage.getHyperlinkReference() != null && printImage.getHyperlinkPage() != null) {
                chunk.setRemoteGoto(printImage.getHyperlinkReference(),
                        printImage.getHyperlinkPage().intValue());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_NONE:
        default: {
            break;
        }
        }

        ColumnText colText = new ColumnText(pdfContentByte);
        colText.setSimpleColumn(new Phrase(chunk), printImage.getX() + xoffset + borderOffset,
                jasperPrint.getPageHeight() - printImage.getY() - image.scaledHeight() - yoffset - borderOffset,
                printImage.getX() + xoffset + borderOffset + image.scaledWidth(),
                jasperPrint.getPageHeight() - printImage.getY() - yoffset - borderOffset, image.scaledHeight(),
                Element.ALIGN_LEFT);

        colText.go();
    }

    if (printImage.getPen() != JRGraphicElement.PEN_NONE) {
        pdfContentByte.setRGBColorStroke(printImage.getForecolor().getRed(),
                printImage.getForecolor().getGreen(), printImage.getForecolor().getBlue());

        pdfContentByte.setLineWidth(lineWidth);

        if (isLineDotted) {
            pdfContentByte.setLineDash(5f, 3f, 0f);
        } else {
            pdfContentByte.setLineDash(0f);
        }

        pdfContentByte.rectangle(printImage.getX() - borderCorrection,
                jasperPrint.getPageHeight() - printImage.getY() + borderCorrection,
                printImage.getWidth() + 2 * borderCorrection - 1,
                -printImage.getHeight() - 2 * borderCorrection + 1);

        pdfContentByte.stroke();
    }
}

From source file:com.develog.utils.report.engine.export.JRPdfExporter.java

License:Open Source License

/**
 * /*from w w w.ja va2 s.com*/
 */
protected Chunk getHyperlinkInfoChunk(JRPrintText text) {
    Chunk chunk = new Chunk(EMPTY_STRING);

    if (text.getAnchorName() != null) {
        chunk.setLocalDestination(text.getAnchorName());
    }

    switch (text.getHyperlinkType()) {
    case JRHyperlink.HYPERLINK_TYPE_REFERENCE: {
        if (text.getHyperlinkReference() != null) {
            chunk.setAnchor(text.getHyperlinkReference());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR: {
        if (text.getHyperlinkAnchor() != null) {
            chunk.setLocalGoto(text.getHyperlinkAnchor());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE: {
        if (text.getHyperlinkPage() != null) {
            chunk.setLocalGoto("JR_PAGE_ANCHOR_" + text.getHyperlinkPage().toString());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR: {
        if (text.getHyperlinkReference() != null && text.getHyperlinkAnchor() != null) {
            chunk.setRemoteGoto(text.getHyperlinkReference(), text.getHyperlinkAnchor());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE: {
        if (text.getHyperlinkReference() != null && text.getHyperlinkPage() != null) {
            chunk.setRemoteGoto(text.getHyperlinkReference(), text.getHyperlinkPage().intValue());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_NONE:
    default: {
        break;
    }
    }

    return chunk;
}

From source file:com.dlya.facturews.DlyaPdfExporter2.java

License:Open Source License

protected void writePageAnchor(int pageIndex) throws DocumentException {
    Map<Attribute, Object> attributes = new HashMap<Attribute, Object>();
    FontUtil.getInstance(jasperReportsContext).getAttributesWithoutAwtFont(attributes,
            new JRBasePrintText(jasperPrint.getDefaultStyleProvider()));
    Font pdfFont = getFont(attributes, getLocale(), false);
    Chunk chunk = new Chunk(" ", pdfFont);

    chunk.setLocalDestination(JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + (pageIndex + 1));

    //tagHelper.startPageAnchor();

    ColumnText colText = new ColumnText(pdfContentByte);
    colText.setSimpleColumn(new Phrase(chunk), 0, jasperPrint.getPageHeight(), 1, 1, 0, Element.ALIGN_LEFT);

    colText.go();/* www  . j a va 2  s  .  c o  m*/

    //tagHelper.endPageAnchor();
}

From source file:com.dlya.facturews.DlyaPdfExporter2.java

License:Open Source License

protected void setAnchor(Chunk chunk, JRPrintAnchor anchor, JRPrintElement element) {
    String anchorName = anchor.getAnchorName();
    if (anchorName != null) {
        chunk.setLocalDestination(anchorName);

        if (anchor.getBookmarkLevel() != JRAnchor.NO_BOOKMARK) {
            addBookmark(anchor.getBookmarkLevel(), anchor.getAnchorName(), element.getX(), element.getY());
        }/*  ww w  . j a  va2s . c  om*/
    }
}

From source file:com.songbook.pc.exporter.PdfExporter.java

License:Open Source License

private PageStats generatePDF(List<SongNode> songList, File outputFile) throws IOException, DocumentException {
    logger.info("Starting export to PDF file {}.", outputFile.getAbsolutePath());

    // Initialize Writer
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
    PageStats pageStats = new PageStats();
    writer.setPageEvent(pageStats);/*from  ww  w .j  a v a2s  .com*/

    // Initialize document
    document.setPageSize(PageSize.A4);
    document.setMargins(35 * POINTS_PER_MM, 10 * POINTS_PER_MM, 7 * POINTS_PER_MM, 7 * POINTS_PER_MM);
    document.setMarginMirroring(true);

    document.open();

    // Add QR codes
    Element qrCodeSection = buildQrCodeSection();
    document.add(qrCodeSection);

    // Line separator
    document.add(verseSpacing);
    document.add(new LineSeparator());
    document.add(verseSpacing);

    // Build TOC
    Chunk tocTitle = new Chunk("SONG BOOK - TABLE OF CONTENTS", songTitleFont);
    tocTitle.setLocalDestination("TOC");
    document.add(new Paragraph(tocTitle));
    for (int i = 0; i < songList.size(); i++) {
        SongNode songNode = songList.get(i);
        int chapterNumber = i + 1;
        Chunk tocEntry = new Chunk(chapterNumber + ". " + songNode.getTitle(), textFont);
        tocEntry.setLocalGoto("SONG::" + chapterNumber);
        document.add(new Paragraph(tocEntry));
    }
    document.newPage();
    pageStats.setSectionLength("TOC", pageStats.getCurrentPage() - 1);

    // Build document
    for (int i = 0; i < songList.size(); i++) {
        // Get song node
        SongNode songNode = songList.get(i);

        // Mark song start
        int songStartPage = pageStats.getCurrentPage();

        // Write song
        document.add(buildChapter(songNode, i + 1));
        document.newPage();

        // Record song length
        pageStats.setSectionLength(songNode.getTitle(), pageStats.getCurrentPage() - songStartPage);
    }

    // Close document
    document.close();

    logger.info("COMPLETED export to PDF file {}.", outputFile.getAbsolutePath());

    return pageStats;
}

From source file:com.songbook.pc.exporter.PdfExporter.java

License:Open Source License

private Chapter buildChapter(SongNode songNode, int chapterNumber) {
    // Title//from   w ww  .jav  a  2s.c  om
    Chunk chapterTitle = new Chunk(songNode.getTitle(), songTitleFont);
    chapterTitle.setLocalDestination("SONG::" + chapterNumber);
    chapterTitle.setLocalGoto("TOC");

    Chapter chapter = new Chapter(new Paragraph(chapterTitle), chapterNumber);
    for (VerseNode verseNode : songNode.getVerseList()) {
        processVerse(verseNode, chapter);
    }
    return chapter;
}

From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableHeading.java

License:Open Source License

@Override
public void applyStyles(Style style) {
    super.applyStyles(style);

    int outlineLevel = headingNumbering.size();
    Map<Integer, StyleListProperties> listPropertiesMap = style.getOutlinePropertiesMap();
    if (listPropertiesMap != null) {
        int displayCount = 1;
        StyleListProperties listProperties = StylableList.getListProperties(listPropertiesMap, outlineLevel);
        if (listProperties != null) {
            // display-levels
            Integer displayLevels = listProperties.getDisplayLevels();
            if (displayLevels != null && displayLevels > 1) {
                displayCount = Math.min(displayLevels, outlineLevel);
            }//  www  . ja  v  a  2s .  c o m
        }

        // add leading numbering ie 1.2.3.
        addNumbering(listPropertiesMap, displayCount);
    }

    // add implicit bookmark
    Chunk chunk = new Chunk(ODFUtils.TAB_STR);
    chunk.setLocalDestination(generateImplicitDestination(headingNumbering));
    addElement(chunk);
}