Example usage for com.itextpdf.text Chunk setAnchor

List of usage examples for com.itextpdf.text Chunk setAnchor

Introduction

In this page you can find the example usage for com.itextpdf.text Chunk setAnchor.

Prototype


public Chunk setAnchor(final String url) 

Source Link

Document

Sets an anchor for this Chunk.

Usage

From source file:com.masscustsoft.service.ToPdf.java

License:Open Source License

/**
 * Adds a link to the current paragraph.
 * @since 5.0.6//  ww  w .j  a v  a2 s. com
 */
public void processLink() {
    if (currentParagraph == null) {
        currentParagraph = new Paragraph();
    }
    // The link provider allows you to do additional processing
    LinkProcessor i = (LinkProcessor) providers.get(HTMLWorker.LINK_PROVIDER);
    if (i == null || !i.process(currentParagraph, chain)) {
        // sets an Anchor for all the Chunks in the current paragraph
        String href = chain.getProperty(HtmlTags.HREF);
        if (href != null) {
            for (Chunk ck : currentParagraph.getChunks()) {
                ck.setAnchor(href);
            }
        }
    }
    // a link should be added to the current paragraph as a phrase
    if (stack.isEmpty()) {
        // no paragraph to add too, 'a' tag is first element
        Paragraph tmp = new Paragraph(new Phrase(currentParagraph));
        currentParagraph = tmp;
    } else {
        Paragraph tmp = (Paragraph) stack.pop();
        tmp.add(new Phrase(currentParagraph));
        currentParagraph = tmp;
    }
}

From source file:org.dspace.disseminate.CitationDocument.java

/**
 * Takes a DSpace {@link Bitstream} and uses its associated METADATA to
 * create a cover page./*  w w  w .jav  a 2s .  c o  m*/
 *
 * @param cDoc The cover page document to add cited information to.
 * @param writer
 * @param cMeta
 *            METADATA retrieved from the parent collection.
 * @throws IOException
 * @throws DocumentException
 */
private void generateCoverPage(Document cDoc, PdfWriter writer, CitationMeta cMeta) throws DocumentException {
    cDoc.open();
    writer.setCompressionLevel(0);

    Item item = cMeta.getItem();

    //Set up some fonts
    Font helv26 = FontFactory.getFont(FontFactory.HELVETICA, 26f, BaseColor.BLACK);
    Font helv16 = FontFactory.getFont(FontFactory.HELVETICA, 16f, BaseColor.BLACK);
    Font helv12 = FontFactory.getFont(FontFactory.HELVETICA, 12f, BaseColor.BLACK);
    Font helv12_italic = FontFactory.getFont(FontFactory.HELVETICA_OBLIQUE, 12f, BaseColor.BLACK);
    Font helv11_bold = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 11f, BaseColor.BLACK);
    Font helv9 = FontFactory.getFont(FontFactory.HELVETICA, 9f, BaseColor.BLACK);

    // 1 - Header:
    //  University Name
    //  Repository Name                                                        repository.url
    Paragraph university = new Paragraph("The Ohio State University", helv11_bold);
    cDoc.add(university);

    PdfPTable repositoryTable = new PdfPTable(2);
    repositoryTable.setWidthPercentage(100);

    Chunk repositoryName = new Chunk("Knowledge Bank", helv11_bold);
    PdfPCell nameCell = new PdfPCell();
    nameCell.setBorderWidth(0);
    nameCell.addElement(repositoryName);

    Chunk repositoryURL = new Chunk("kb.osu.edu", helv11_bold);
    repositoryURL.setAnchor("http://kb.osu.edu");

    PdfPCell urlCell = new PdfPCell();
    urlCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    urlCell.setBorderWidth(0);
    urlCell.addElement(repositoryURL);

    repositoryTable.addCell(nameCell);
    repositoryTable.addCell(urlCell);

    repositoryTable.setSpacingAfter(5);

    cDoc.add(repositoryTable);

    // Line Separator
    LineSeparator lineSeparator = new LineSeparator();
    cDoc.add(lineSeparator);

    // 2 - Bread Crumbs
    // Community Name                                                          Collection Name
    PdfPTable breadcrumbTable = new PdfPTable(2);
    breadcrumbTable.setWidthPercentage(100);

    Chunk communityName = new Chunk(getOwningCommunity(item), helv9);
    PdfPCell commCell = new PdfPCell();
    commCell.setBorderWidth(0);
    commCell.addElement(communityName);

    Chunk collectionName = new Chunk(getOwningCollection(item), helv9);
    PdfPCell collCell = new PdfPCell();
    collCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    collCell.setBorderWidth(0);
    collCell.addElement(collectionName);

    breadcrumbTable.addCell(commCell);
    breadcrumbTable.addCell(collCell);

    breadcrumbTable.setSpacingBefore(5);
    breadcrumbTable.setSpacingAfter(5);

    cDoc.add(breadcrumbTable);

    // Line Separator
    cDoc.add(lineSeparator);

    // 3 - Metadata
    // date.issued
    // dc.title
    // dc.creator; dc.creator
    Paragraph dateIssued = new Paragraph(getFirstMetadata(item, "dc.date.issued"), helv12);
    dateIssued.setSpacingBefore(20);
    cDoc.add(dateIssued);

    Paragraph title = new Paragraph(item.getName(), helv26);
    title.setSpacingBefore(15);
    cDoc.add(title);

    Paragraph creators = new Paragraph(getAllMetadataSeperated(item, "dc.creator"), helv16);
    creators.setSpacingBefore(30);
    creators.setSpacingAfter(20);
    cDoc.add(creators);

    // Line Separator
    cDoc.add(lineSeparator);

    // 4 - Citation
    // dc.identifier.citation
    // dc.identifier.uri
    Paragraph citation = new Paragraph(getFirstMetadata(item, "dc.identifier.citation"), helv12);

    Chunk identifierChunk = new Chunk(getFirstMetadata(item, "dc.identifier.uri"), helv12);
    identifierChunk.setAnchor(getFirstMetadata(item, "dc.identifier.uri"));

    Paragraph identifier = new Paragraph();
    identifier.add(identifierChunk);

    cDoc.add(citation);
    cDoc.add(identifier);

    // 5 - License
    // Downloaded from the Knowledge Bank, The Ohio State University's institutional repository
    Paragraph license = new Paragraph(
            "Downloaded from the Knowledge Bank, The Ohio State University's institutional repository",
            helv12_italic);
    license.setSpacingBefore(10);
    cDoc.add(license);

    cDoc.close();
}

From source file:org.jaqpot.core.service.data.ReportService.java

public void report2PDF(Report report, OutputStream os) {

    Document document = new Document();
    document.setPageSize(PageSize.A4);// w ww.ja  v  a 2 s  .  c  o m
    document.setMargins(50, 45, 80, 40);
    document.setMarginMirroring(false);

    try {
        PdfWriter writer = PdfWriter.getInstance(document, os);
        TableHeader event = new TableHeader();
        writer.setPageEvent(event);

    } catch (DocumentException ex) {
        throw new InternalServerErrorException(ex);
    }

    document.open();

    /** setup fonts for pdf */
    Font ffont = new Font(Font.FontFamily.UNDEFINED, 9, Font.ITALIC);
    Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC);
    Font paragraphFontBold = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
    Font paragraphFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
    Font tableFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);

    /** print link to jaqpot*/
    Chunk chunk = new Chunk(
            "This report has been automatically created by the JaqpotQuatro report service. Click here to navigate to our official webpage",
            ffont);
    chunk.setAnchor("http://www.jaqpot.org");

    Paragraph paragraph = new Paragraph(chunk);
    paragraph.add(Chunk.NEWLINE);

    Chapter chapter = new Chapter(paragraph, 1);
    chapter.setNumberDepth(0);

    /** get title */
    String title = null;
    if (report.getMeta() != null && report.getMeta().getTitles() != null
            && !report.getMeta().getTitles().isEmpty())
        title = report.getMeta().getTitles().iterator().next();

    /** print title aligned centered in page */
    if (title == null)
        title = "Report";
    chunk = new Chunk(title, chapterFont);
    paragraph = new Paragraph(chunk);
    paragraph.setAlignment(Element.ALIGN_CENTER);
    paragraph.add(Chunk.NEWLINE);
    paragraph.add(Chunk.NEWLINE);

    chapter.add(paragraph);

    /** report Description */
    if (report.getMeta() != null && report.getMeta().getDescriptions() != null
            && !report.getMeta().getDescriptions().isEmpty()
            && !report.getMeta().getDescriptions().toString().equalsIgnoreCase("null")) {
        paragraph = new Paragraph();
        paragraph.add(new Chunk("Description: ", paragraphFontBold));
        paragraph.add(new Chunk(report.getMeta().getDescriptions().toString().replaceAll(":http", ": http"),
                paragraphFont));
        chapter.add(paragraph);
        chapter.add(Chunk.NEWLINE);
    }

    /** report model, algorithm and/or dataset id */
    if (report.getMeta() != null && report.getMeta().getHasSources() != null
            && !report.getMeta().getHasSources().isEmpty() && !report.getMeta().getDescriptions().isEmpty()
            && !report.getMeta().getDescriptions().toString().equalsIgnoreCase("null")) {
        Iterator<String> sources = report.getMeta().getHasSources().iterator();
        sources.forEachRemaining(o -> {
            if (o != null) {
                String[] source = o.split("/");
                if (source[source.length - 2].trim().equals("model")
                        || source[source.length - 2].trim().equals("algorithm")
                        || source[source.length - 2].trim().equals("dataset")) {
                    Paragraph paragraph1 = new Paragraph();
                    paragraph1.add(new Chunk(source[source.length - 2].substring(0, 1).toUpperCase()
                            + source[source.length - 2].substring(1) + ": ", paragraphFontBold));
                    paragraph1.add(new Chunk(source[source.length - 1], paragraphFont));
                    chapter.add(paragraph1);
                    chapter.add(Chunk.NEWLINE);
                }
            }
        });
    }

    /** report single calculations */
    report.getSingleCalculations().forEach((key, value) -> {
        Paragraph paragraph1 = new Paragraph();
        paragraph1 = new Paragraph();
        paragraph1.add(new Chunk(key + ": ", paragraphFontBold));
        paragraph1.add(new Chunk(value.toString().trim().replaceAll(" +", " "), paragraphFont));
        chapter.add(paragraph1);
        chapter.add(Chunk.NEWLINE);
    });

    /** report date of completion */
    if (report.getMeta() != null && report.getMeta().getDate() != null) {
        Paragraph paragraph1 = new Paragraph();
        paragraph1.add(new Chunk("Procedure completed on: ", paragraphFontBold));
        paragraph1.add(new Chunk(report.getMeta().getDate().toString(), paragraphFont));
        chapter.add(paragraph1);
        chapter.add(Chunk.NEWLINE);
    }

    try {
        document.add(chapter);
    } catch (DocumentException ex) {
        throw new InternalServerErrorException(ex);
    }

    Integer chapterNumber = 0;

    /** report all_data */
    for (Entry<String, ArrayCalculation> entry : report.getArrayCalculations().entrySet()) {
        String label = entry.getKey();
        ArrayCalculation ac = entry.getValue();
        PdfPTable table = new PdfPTable(ac.getColNames().size() + 1);
        for (Entry<String, List<Object>> row : ac.getValues().entrySet()) {

            try {
                XMLWorkerHelper.getInstance().parseXHtml(w -> {
                    if (w instanceof WritableElement) {
                        List<Element> elements = ((WritableElement) w).elements();
                        for (Element element : elements) {
                            PdfPCell pdfCell = new PdfPCell();
                            pdfCell.addElement(element);
                            table.addCell(pdfCell);
                        }
                    }
                }, new StringReader(row.getKey()));
            } catch (IOException e) {
                e.printStackTrace();
            }

            for (Object o : row.getValue()) {
                table.addCell(o.toString());
            }
            table.completeRow();
        }
        try {
            Chunk tableChunk = new Chunk(label, tableFont);
            Chapter tableChapter = new Chapter(new Paragraph(tableChunk), ++chapterNumber);
            tableChapter.add(Chunk.NEWLINE);
            tableChapter.add(table);
            document.newPage();
            document.add(tableChapter);
        } catch (DocumentException ex) {
            throw new InternalServerErrorException(ex);
        }
    }

    /** report plots */
    for (Entry<String, String> entry : report.getFigures().entrySet()) {
        try {
            byte[] valueDecoded = Base64.decodeBase64(entry.getValue());
            Image l = Image.getInstance(valueDecoded);
            document.newPage();
            //image starts at the half's half of pdf page
            l.setAbsolutePosition(0, (document.getPageSize().getHeight() / 2 / 2));
            l.scaleToFit(document.getPageSize());

            Chunk tableChunk = new Chunk(entry.getKey(), tableFont);
            Chapter tableChapter = new Chapter(new Paragraph(tableChunk), ++chapterNumber);
            tableChapter.add(l);
            document.add(tableChapter);
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
    }
    document.close();
}