Example usage for com.itextpdf.text Chunk setLocalDestination

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

Introduction

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

Prototype

public Chunk setLocalDestination(final String name) 

Source Link

Document

Sets a local destination for this Chunk.

Usage

From source file:com.athena.chameleon.engine.utils.PDFWriterUtil.java

License:Apache License

/**
 * Chapter  ?// w  ww  .j a  v a2  s  .c  om
 *
 * @param text Chapter title
 * @param chapterNo Chapter Number
 * @return Chapter
 */
public static Chapter getChapter(String text, int chapterNo) {

    Chapter chapter = new Chapter(text, chapterNo);
    String title = chapter.getTitle().getContent();

    Chunk c = new Chunk(text, fnChapter);
    c.setLocalDestination(title);
    Paragraph chapterPh = new Paragraph();
    chapterPh.add(c);
    chapterPh.setSpacingAfter(12);
    chapter.setTitle(chapterPh);
    return chapter;

}

From source file:com.athena.chameleon.engine.utils.PDFWriterUtil.java

License:Apache License

/**
 * /*from  w  w  w  . j  a  v a2 s  .  com*/
 * Section  ?
 *
 * @param chapter section?  chapter ?
 * @param text section title
 * @param sectionNo section Number
 * @return Section
 */
public static Section getSection(Section chapter, String text) {
    Section section = chapter.addSection(text);
    String title = section.getTitle().getContent();

    Chunk c;
    if (section.getDepth() >= 3) {
        c = new Chunk(text, fnSection2);
    } else {
        c = new Chunk(text, fnSection);
    }
    c.setLocalDestination(title);

    Paragraph sectionPh = new Paragraph();
    sectionPh.add(c);
    sectionPh.setSpacingBefore(8);
    sectionPh.setSpacingAfter(3);
    if (section.getDepth() >= 3)
        sectionPh.setIndentationLeft(23);

    section.setTitle(sectionPh);
    return section;
}

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

License:Open Source License

private Paragraph getParagraph(Map it) throws Exception {
    Paragraph p = new Paragraph();
    String lb = MapUtil.getStr(it, "label", null);
    if (lb != null) {
        Chunk ch = new Chunk();
        ch.setLocalDestination(lb);
        p.add(ch);/*from  www.  ja v  a 2  s  .com*/
    }
    getChunks(p, it, null);
    List<Map> lst = (List) it.get("items");
    if (lst != null) {
        for (Map item : lst) {
            Element el = getElement(item);
            if (el != null)
                p.add(el);
        }
    }
    applyFont(p, it);

    return p;
}

From source file:com.vectorprint.report.itext.TocOutputStream.java

License:Open Source License

@Override
public void secondPass(InputStream firstPass, OutputStream orig) throws IOException {
    PdfReader reader = null;//from   w  w  w  . j  a  va  2 s. co  m
    VectorPrintDocument vpd = (VectorPrintDocument) outer.getDocument();
    try {
        reader = new PdfReader(firstPass);
        prepareToc();
        // init fresh components for second pass styling
        StylerFactory _stylerFactory = outer.getStylerFactory().getClass().newInstance();
        StylerFactoryHelper.SETTINGS_ANNOTATION_PROCESSOR.initSettings(_stylerFactory, outer.getSettings());
        _stylerFactory.setLayerManager(outer.getElementProducer());
        _stylerFactory.setImageLoader(outer.getElementProducer());
        outer.getStyleHelper().setStylerFactory(_stylerFactory);
        EventHelper event = outer.getEventHelper().getClass().newInstance();
        event.setItextStylerFactory(_stylerFactory);
        event.setElementProvider(outer.getElementProducer());
        ((DefaultElementProducer) outer.getElementProducer()).setPh(event);
        Document d = new VectorPrintDocument(event, _stylerFactory, outer.getStyleHelper());
        PdfWriter w = PdfWriter.getInstance(d, orig);
        w.setPageEvent(event);
        outer.getStyleHelper().setVpd((VectorPrintDocument) d);
        _stylerFactory.setDocument(d, w);
        DocumentStyler ds = _stylerFactory.getDocumentStyler();
        outer.getStyleHelper().style(d, null, StyleHelper.toCollection(ds));
        d.open();
        ds.styleAfterOpen(d, null);
        List outline = SimpleBookmark.getBookmark(reader);
        if (!ds.getValue(DocumentSettings.TOCAPPEND, Boolean.class)) {
            printToc(d, w, vpd);
            if (outline != null) {
                int cur = w.getCurrentPageNumber();
                SimpleBookmark.shiftPageNumbers(outline, cur, null);
            }
            d.newPage();
        }
        outer.getSettings().put(ReportConstants.DEBUG, Boolean.FALSE.toString());
        for (int p = 1; p <= reader.getNumberOfPages(); p++) {
            Image page = Image.getInstance(w.getImportedPage(reader, p));
            page.setAbsolutePosition(0, 0);
            d.setPageSize(page);
            d.newPage();
            Chunk i = new Chunk(" ");
            if (vpd.getToc().containsKey(p)) {
                Section s = null;
                for (Map.Entry<Integer, List<Section>> e : vpd.getToc().entrySet()) {
                    if (e.getKey() == p) {
                        s = e.getValue().get(0);
                        break;
                    }
                }
                i.setLocalDestination(s.getTitle().getContent());
            }
            d.add(i);
            w.getDirectContent().addImage(page);
            w.freeReader(reader);
        }
        if (_stylerFactory.getDocumentStyler().getValue(DocumentSettings.TOCAPPEND, Boolean.class)) {
            printToc(d, w, vpd);
        }
        w.setOutlines(outline);
        if (outer.isWasDebug()) {
            event.setLastPage(outer.getWriter().getCurrentPageNumber());
            d.setPageSize(new Rectangle(ItextHelper.mmToPts(297), ItextHelper.mmToPts(210)));
            d.setMargins(5, 5, 5, 5);
            d.newPage();
            outer.getSettings().put(ReportConstants.DEBUG, Boolean.TRUE.toString());
            event.setDebugHereAfter(true);
            DebugHelper.appendDebugInfo(w, d, outer.getSettings(), _stylerFactory);
        }
        d.close();
    } catch (VectorPrintException | DocumentException | InstantiationException | IllegalAccessException ex) {
        throw new VectorPrintRuntimeException(ex);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java

License:Apache License

/**
 * Alle Elemente, die sich fr das angegebene Schema in der entsprechenden
 * Queue befinden, werden behandelt.//from w  w w .j ava 2s . c  o  m
 * 
 * @param currSchemaPrefix
 *            Prfix des zu behandelnden Schemas
 * @param schemaQueues
 *            Map enthlt Queues fr jedes Schema (Key ist der
 *            Namepace-Prfix)
 * @param xmlSchemaSet
 *            SchemaSet, in dem alle Typen und Elemente definiert sind
 * @throws DocumentException
 */
private void behandleSchemaQueue(String currSchemaPrefix, Map<String, Queue<XSElementDecl>> schemaQueues,
        XSSchemaSet xmlSchemaSet) throws DocumentException {

    // Liste mit allen bereits behandelten Elementen
    java.util.List<XSElementDecl> listElementsInserted = new LinkedList<XSElementDecl>();

    // Aktuelle Queue
    Queue<XSElementDecl> currQueue = schemaQueues.get(currSchemaPrefix);

    if (currQueue != null) {

        // zur Sicherheit nochmal alle Elemente dieses Schema in Queue
        // einfgen
        // damit ist sichergestellt, dass auch nicht direkt verknpfte
        // Elemente (any => Plugins) behandelt werden
        XSSchema currSchema = xmlSchemaSet.getSchema(configurator.getPropertyNamespace(currSchemaPrefix));
        for (Entry<String, XSElementDecl> currElement : currSchema.getElementDecls().entrySet()) {
            currQueue.add(currElement.getValue());
        }

        if (currQueue.peek() != null) {
            // Kapitel inkl. Sprungmarke erstellen
            String targetNsUrl = currQueue.peek().getTargetNamespace();
            String chapterTitle = getNsPref(targetNsUrl) + ":" + targetNsUrl;
            chapterCounter++;
            Chunk chunkChapter = getChunkChapter(chapterTitle);
            chunkChapter.setLocalDestination(chapterTitle);
            Chapter currChapter = new Chapter(new Paragraph(chunkChapter), chapterCounter);

            // Kapitel dem Dokument hinzufgen
            docPdf.add(currChapter);

            // Eintrag fr Inhaltsverzeichnis (Chapter)
            ContPdfEntry currEntry = new ContPdfEntry(null, currChapter.getTitle().getContent(), chapterTitle,
                    currPageNumber);
            listEntries.add(currEntry);

            while (currQueue.peek() != null) {
                // Aktuelles Element aus Queue holen
                XSElementDecl currElement = currQueue.poll();
                if (!listElementsInserted.contains(currElement)) {
                    behandleElement(currElement, currChapter, currEntry, schemaQueues);
                    listElementsInserted.add(currElement);
                }
            }
        }
    }
}

From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java

License:Apache License

/**
 * Das Element wird mit Datentyp, seinen Attributen und Kindelementen
 * geschrieben.// w  w  w. j a va 2  s  .  c  o  m
 * 
 * @param currElement
 *            Zu behandelndes Element
 * @param currChapter
 *            Kapitel, zu dem das Element hinzugefgt wird
 * @param chapterEntry
 *            Eintrag des Kapitels fr das Inhaltsverzeichnis
 * @param schemaQueues
 *            Queues mit allen Elementen fr die einzelnen Schemas
 */
private void behandleElement(XSElementDecl currElement, Chapter currChapter, ContPdfEntry chapterEntry,
        Map<String, Queue<XSElementDecl>> schemaQueues) throws DocumentException {
    // Fr jedes Element eine Section; Titel ist der Elementname

    // eine Zeile Abstand
    docPdf.add(getEmptyLineText());

    // Referenz des Elements
    String currReferenzString = getReferenceForElement(currElement);

    // Titel des Elements inkl. Sprungmarke
    Chunk chunkElem = getChunkElement(currElement.getName());
    chunkElem.setLocalDestination(currReferenzString);

    Paragraph currElemPara = new Paragraph(getChunkElement(""));
    currElemPara.add(chunkElem);
    Section currSection = currChapter.addSection(currElemPara);

    // Eintrag fr Inhaltsverzeichnis (Element)
    listEntries.add(new ContPdfEntry(chapterEntry, currSection.getTitle().getContent(), currReferenzString,
            currPageNumber));

    // Sektion dem Dokument (vorlufig) hinzufgen (wichtig fr das
    // Aktualisieren der Seitenzahl)
    currSection.setComplete(false);
    docPdf.add(currSection);

    // Anmerkung zum Element
    XSAnnotation ann = currElement.getAnnotation();
    if (ann != null) {
        // Abstand
        currSection.add(getEmptyLineTextHalf());

        String beschreibung = ann.getAnnotation().toString();
        currSection.add(getParagraphTextItalic(beschreibung));
    }

    // Abstand
    currSection.add(getEmptyLineTextHalf());

    // Datentyp
    Paragraph paraTyp = new Paragraph();
    XSType currType = currElement.getType();
    paraTyp.add(getPhraseTextBold("Datentyp: "));
    String strTyp = getNameWithPrefix(currType);
    if (currType.isLocal()) {
        strTyp = "<lokal definiert>";
    }
    paraTyp.add(getPhraseText(strTyp + "\n"));

    // Basistyp
    if ((currType.getDerivationMethod() == XSType.RESTRICTION)
            || (currType.getDerivationMethod() == XSType.EXTENSION)) {
        paraTyp.add(getPhraseTextBold("basiert auf: "));
        paraTyp.add(getPhraseText(getNameWithPrefix(currType.getBaseType()) + "\n"));
    }
    currSection.add(paraTyp);

    // Attribute (falls vorhanden)
    behandleAttribute(currType, currSection);

    // Kind-Elemente (falls vorhanden)
    if (currType.isComplexType()) {
        // Gruppentyp (sequence oder choice)
        String strCompositor = getCompositorStr(currType.asComplexType());
        if (strCompositor.length() > 0) {
            // Abstand
            currSection.add(getEmptyLineTextHalf());

            currSection.add(getParagraphTextBold(strCompositor + "-Elemente:"));
            behandleKindElemente(currElement, currSection, schemaQueues);
        }
    }

    // Sektion dem Dokument (endgltig) hinzufgen
    currSection.setComplete(true);
    docPdf.add(currSection);
}

From source file:fr.ybonnel.breizhcamppdf.PdfRenderer.java

License:Apache License

private void createTalksPages(List<Talk> talksToExplain) throws DocumentException, IOException {
    document.setPageSize(PageSize.A4);//from  w  w  w .  ja va  2s .c o m
    document.newPage();

    Paragraph paragraph = new Paragraph("Liste des talks");
    paragraph.setSpacingAfter(25);
    paragraph.getFont().setSize(25);
    paragraph.setAlignment(Element.ALIGN_CENTER);
    document.add(paragraph);

    for (TalkDetail talk : Lists.transform(talksToExplain, new Function<Talk, TalkDetail>() {
        @Override
        public TalkDetail apply(Talk input) {
            return TalkService.INSTANCE.getTalkDetail(input);
        }
    })) {

        if (talk == null) {
            continue;
        }

        Paragraph empty = new Paragraph(" ");
        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100);
        table.setKeepTogether(true);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        PdfPCell cell;
        Chunk titleTalk = new Chunk(talk.getTitle(), talkFontTitle);
        titleTalk.setLocalDestination("talk" + talk.getId());
        float[] withTitle = { 0.05f, 0.95f };
        PdfPTable titleWithFormat = new PdfPTable(withTitle);
        titleWithFormat.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        titleWithFormat.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

        Image image = AvatarService.INSTANCE.getImage(PdfRenderer.class
                .getResource("/formats/" + talk.getTalk().getFormat().replaceAll(" ", "") + ".png"));
        titleWithFormat.addCell(image);
        titleWithFormat.addCell(new Paragraph(titleTalk));

        table.addCell(titleWithFormat);

        table.addCell(empty);

        table.addCell(new Paragraph("Salle " + talk.getTalk().getRoom() + " de " + talk.getTalk().getStart()
                + "  " + talk.getTalk().getEnd(), presentFont));

        table.addCell(empty);

        cell = new PdfPCell();
        cell.setBorder(0);
        cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
        for (Element element : HTMLWorker
                .parseToList(new StringReader(markdownProcessor.markdown(talk.getDescription())), null)) {
            if (element instanceof Paragraph) {
                ((Paragraph) element).setAlignment(Element.ALIGN_JUSTIFIED);
            }
            cell.addElement(element);
        }
        table.addCell(cell);

        table.addCell(empty);

        table.addCell(new Paragraph("Prsent par :", presentFont));

        float[] widthSpeaker = { 0.05f, 0.95f };
        for (Speaker speaker : talk.getSpeakers()) {
            PdfPTable speakerWithAvatar = new PdfPTable(widthSpeaker);
            speakerWithAvatar.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            speakerWithAvatar.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

            speakerWithAvatar.addCell(AvatarService.INSTANCE.getImage(speaker.getAvatar()));
            speakerWithAvatar.addCell(new Phrase(speaker.getFullname()));
            table.addCell(speakerWithAvatar);
        }

        table.addCell(empty);
        table.addCell(empty);
        document.add(table);
    }
}

From source file:nz.ac.waikato.cms.doc.HyperLinkGrades.java

License:Open Source License

/**
 * Adds the index with locations to the existing PDF.
 *
 * @param locations   the locations to index
 * @param input   the input PDF//ww w .  j  a  v a 2s .  co  m
 * @param output   the output PDF
 * @return      true if successfully generated
 */
public static boolean addIndex(List<Location> locations, File input, File output) {
    try {
        // copy pages, add target
        PdfReader reader = new PdfReader(input.getAbsolutePath());
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(output.getAbsolutePath()));
        document.open();
        PdfContentByte canvas = writer.getDirectContent();
        PdfImportedPage page;
        float height = 0;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            document.newPage();
            page = writer.getImportedPage(reader, i);
            canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
            Chunk loc = new Chunk(" ");
            loc.setLocalDestination("loc" + i);
            height = page.getHeight();
            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(loc), 50, height - 50, 0);
        }
        // add index
        for (int i = 0; i < locations.size(); i++) {
            Location loc = locations.get(i);
            if (i % MAX_ITEMS_PER_PAGE == 0)
                document.newPage();
            String text = loc.getID() + " " + (loc.getName() == null ? "???" : loc.getName());
            Chunk chunk = new Chunk("Page " + (loc.getPage() + 1) + ": " + text);
            chunk.setAction(PdfAction.gotoLocalPage("loc" + (loc.getPage() + 1), false));
            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(chunk), 50,
                    height - 100 - (i % MAX_ITEMS_PER_PAGE) * 20, 0);
        }
        document.close();

        return true;
    } catch (Exception e) {
        System.err.println("Failed to overlay locations!");
        e.printStackTrace();
        return false;
    }
}