List of usage examples for com.itextpdf.text.pdf PdfAction gotoLocalPage
public static PdfAction gotoLocalPage(String dest, boolean isName)
From source file:be.roots.taconic.pricingguide.service.PDFServiceImpl.java
License:Open Source License
private byte[] stampTableOfContents(byte[] pdf, Toc tableOfContents) throws IOException, DocumentException { try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final PdfReader reader = new PdfReader(pdf); final PdfStamper stamper = new PdfStamper(reader, bos); // stamp the named destinations for (int pageNumber = 1; pageNumber <= reader.getNumberOfPages(); pageNumber++) { stamper.addNamedDestination("page" + pageNumber, pageNumber, new PdfDestination(PdfDestination.XYZ, 80f, 800f, 0)); }// www. j a v a 2 s. c o m // create the table of contents final Chunk tocTitle = new Chunk("TABLE OF CONTENTS", iTextUtil.getFontTocTitle()); int currentTocPage = tableOfContents.getFirstPageOfToc(); int firstTocPage = currentTocPage; PdfContentByte canvas = stamper.getOverContent(currentTocPage); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(tocTitle), 55, 470, 0); final List<TocEntry> entriesSorted = tableOfContents.getEntriesSorted(); int tocEntryNumber = 0; for (TocEntry tocEntry : entriesSorted) { if (tocEntry.isIncludedInToc()) { tocEntryNumber++; // take the right TOC page to stamp the TOC entry on (needed for TOC's with multiple pages) if (tocEntryNumber == getNumberOfItemsPerTocPage(0) || (tocEntryNumber > getNumberOfItemsPerTocPage(0) && (tocEntryNumber - getNumberOfItemsPerTocPage(0)) % getNumberOfItemsPerTocPage(currentTocPage - firstTocPage) == 0)) { currentTocPage++; canvas = stamper.getOverContent(currentTocPage); } Font font = iTextUtil.getFontToc(); if (tocEntry.getLevel() == 1) { font = iTextUtil.getFontTocBold(); } final Phrase p = processHtmlCodes(tocEntry.getLevelString() + tocEntry.getName(), font, iTextUtil.getFontTocSymbol()); p.add(new Chunk("", iTextUtil.getFontToc())); if (tocEntry.isShowingPageNumber()) { p.add(new Chunk(new DottedLineSeparator())); p.add(new Chunk(" " + String.valueOf(tocEntry.getFinalPageNumber()), iTextUtil.getFontToc())); } for (Chunk chunk : p.getChunks()) { chunk.setAction(PdfAction.gotoLocalPage("page" + tocEntry.getFinalPageNumber(), false)); } int y; if (tocEntryNumber < getNumberOfItemsPerTocPage(0)) { y = 460 - (16 * (tocEntryNumber % getNumberOfItemsPerTocPage(0))); } else { y = 680 - (16 * ((tocEntryNumber - getNumberOfItemsPerTocPage(0)) % getNumberOfItemsPerTocPage(currentTocPage - firstTocPage))); } final ColumnText ct = new ColumnText(canvas); ct.setSimpleColumn(p, 52, y, 555, 70, 0, Element.ALIGN_JUSTIFIED); ct.go(); } } stamper.close(); reader.close(); return bos.toByteArray(); } }
From source file:com.athena.chameleon.engine.core.PDFCommonEventHelper.java
License:Apache License
/** * //from w w w. ja v a 2s.c o m * Paragraph ?.(Navigation ? title ) * * @param title * @param pageNumber * @param depth * @param x1 document left * @param x2 document right * @return Paragraph */ public Paragraph getTocParagraph(String title, int pageNumber, int depth, float x1, float x2) { Font tocFont = new Font(bfKorean, 10); if (depth == 0) tocFont.setStyle(Font.BOLD); Paragraph p = new Paragraph(); p.setSpacingAfter(5); Chunk tit = new Chunk(title + " ", tocFont); tit.setAction(PdfAction.gotoLocalPage(title, false)); Chunk point = new Chunk(".", tocFont); Chunk number = new Chunk(" " + pageNumber, tocFont); number.setAction(PdfAction.gotoLocalPage(title, false)); p.add(tit); float width = x2 - x1 - tit.getWidthPoint() - number.getWidthPoint() - (depth * 12); if ((x2 - x1) < tit.getWidthPoint()) width = x2 - x1 - (tit.getWidthPoint() - (x2 - x1)) - number.getWidthPoint() - (depth * 12) - 65; float i = point.getWidthPoint(); while (i < width) { p.add(point); i += point.getWidthPoint(); } p.add(number); return p; }
From source file:com.vectorprint.report.itext.debug.DebugHelper.java
License:Open Source License
/** * adding a link (annotation) to information about the styleClass used * * @param rectangle the value of rectangle * @param styleClass the value of styleClass * @param writer the value of writer/* w ww . java 2 s. com*/ */ public static void debugAnnotation(Rectangle rectangle, String styleClass, PdfWriter writer) { if (styleClass == null) { log.warning("not showing link to styleClass because there is no styleClass"); return; } // only now we can define a goto action, we know the position of the image PdfAction act = PdfAction.gotoLocalPage(styleClass, true); writer.getDirectContent().setAction(act, rectangle.getLeft(), rectangle.getBottom(), rectangle.getRight(), rectangle.getTop()); }
From source file:com.vectorprint.report.itext.debug.DebugHelper.java
License:Open Source License
public static void styleLink(PdfContentByte canvas, String styleClass, String extraInfo, float x, float y, EnhancedMap settings, LayerManager layerAware) { if (styleClass == null) { log.warning("not showing link to styleClass because there is no styleClass"); return;/*ww w .ja v a 2 s . c om*/ } Font dbf = DebugHelper.debugFontLink(canvas, settings); layerAware.startLayerInGroup(DEBUG, canvas); PdfAction act = PdfAction.gotoLocalPage(styleClass, true); Chunk c = new Chunk(styleClass + extraInfo, dbf); float w = ItextHelper.getTextWidth(c); float h = ItextHelper.getTextHeight(c); float tan = (float) Math.tan(Math.toRadians(8)); canvas.setAction(act, x, y, x + w, y + h + tan * w); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(c), x, y, 8); canvas.endLayer(); }
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 av a 2 s . c o 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; } }