List of usage examples for com.lowagie.text.pdf PdfDestination FITH
int FITH
To view the source code for com.lowagie.text.pdf PdfDestination FITH.
Click Source Link
From source file:ambit.data.qmrf.Qmrf_Xml_Pdf.java
License:Open Source License
public void xml2pdf(InputSource xml, OutputStream pdf) { try {/* w ww. j a v a 2s . com*/ 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 {// www . j a v 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://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:org.kuali.coeus.common.committee.impl.web.struts.action.CommitteeActionsActionBase.java
License:Open Source License
/** * This method merged the pdf bytes without creating page numbers and dates. * // w w w .jav a 2s . c o m * (This is a slimed down version of MergePdfBytes() in PrintingServiceImpl.java) * * @param pdfBytesList * List containing the PDF data bytes * @param bookmarksList * List of bookmarks corresponding to the PDF bytes. * @return * @throws PrintingException */ private byte[] mergePdfBytes(List<byte[]> pdfBytesList, List<String> bookmarksList) throws PrintingException { Document document = null; PdfWriter writer = null; ByteArrayOutputStream mergedPdfReport = new ByteArrayOutputStream(); for (int count = 0; count < pdfBytesList.size(); count++) { PdfReader reader; try { reader = new PdfReader(pdfBytesList.get(count)); } catch (IOException e) { LOG.error(e.getMessage(), e); break; // throw new PrintingException(e.getMessage(), e); } int nop; if (reader == null) { LOG.debug("Empty PDF bytes found for " + bookmarksList.get(count)); continue; } else { nop = reader.getNumberOfPages(); } if (count == 0) { document = nop > 0 ? new com.lowagie.text.Document(reader.getPageSizeWithRotation(1)) : new com.lowagie.text.Document(); try { writer = PdfWriter.getInstance(document, mergedPdfReport); } catch (DocumentException e) { LOG.error(e.getMessage(), e); throw new PrintingException(e.getMessage(), e); } document.open(); } PdfContentByte cb = writer.getDirectContent(); int pageCount = 0; while (pageCount < nop) { document.setPageSize(reader.getPageSize(++pageCount)); document.newPage(); PdfImportedPage page = writer.getImportedPage(reader, pageCount); cb.addTemplate(page, 1, 0, 0, 1, 0, 0); PdfOutline root = cb.getRootOutline(); if (pageCount == 1) { String pageName = bookmarksList.get(count); cb.addOutline(new PdfOutline(root, new PdfDestination(PdfDestination.FITH), pageName), pageName); } } } if (document != null) { document.close(); return mergedPdfReport.toByteArray(); } return null; }
From source file:org.kuali.coeus.common.impl.print.PrintingServiceImpl.java
License:Open Source License
/** * @param pdfBytesList List containing the PDF data bytes * @param bookmarksList List of bookmarks corresponding to the PDF bytes. * @return/*from w w w . j a v a 2 s . c o m*/ * @throws PrintingException */ protected byte[] mergePdfBytes(List<byte[]> pdfBytesList, List<String> bookmarksList, boolean headerFooterRequired) throws PrintingException { Document document = null; PdfWriter writer = null; ByteArrayOutputStream mergedPdfReport = new ByteArrayOutputStream(); int totalNumOfPages = 0; PdfReader[] pdfReaderArr = new PdfReader[pdfBytesList.size()]; int pdfReaderCount = 0; for (byte[] fileBytes : pdfBytesList) { LOG.debug("File Size " + fileBytes.length + " For " + bookmarksList.get(pdfReaderCount)); PdfReader reader = null; try { reader = new PdfReader(fileBytes); pdfReaderArr[pdfReaderCount] = reader; pdfReaderCount = pdfReaderCount + 1; totalNumOfPages += reader.getNumberOfPages(); } catch (IOException e) { LOG.error(e.getMessage(), e); } } HeaderFooter footer = null; if (headerFooterRequired) { Calendar calendar = dateTimeService.getCurrentCalendar(); String dateString = formateCalendar(calendar); StringBuilder footerPhStr = new StringBuilder(); footerPhStr.append(" of "); footerPhStr.append(totalNumOfPages); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_76)); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_76)); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_60)); footerPhStr.append(dateString); Font font = FontFactory.getFont(FontFactory.TIMES, 8, Font.NORMAL, Color.BLACK); Phrase beforePhrase = new Phrase("Page ", font); Phrase afterPhrase = new Phrase(footerPhStr.toString(), font); footer = new HeaderFooter(beforePhrase, afterPhrase); footer.setAlignment(Element.ALIGN_BASELINE); footer.setBorderWidth(0f); } for (int count = 0; count < pdfReaderArr.length; count++) { PdfReader reader = pdfReaderArr[count]; int nop; if (reader == null) { LOG.debug("Empty PDF byetes found for " + bookmarksList.get(count)); continue; } else { nop = reader.getNumberOfPages(); } if (count == 0) { document = nop > 0 ? new com.lowagie.text.Document(reader.getPageSizeWithRotation(1)) : new com.lowagie.text.Document(); try { writer = PdfWriter.getInstance(document, mergedPdfReport); } catch (DocumentException e) { LOG.error(e.getMessage(), e); throw new PrintingException(e.getMessage(), e); } if (footer != null) { document.setFooter(footer); } // writer.setPageEvent(new Watermark()); // add watermark object here document.open(); } PdfContentByte cb = writer.getDirectContent(); int pageCount = 0; while (pageCount < nop) { document.setPageSize(reader.getPageSize(++pageCount)); document.newPage(); if (footer != null) { document.setFooter(footer); } PdfImportedPage page = writer.getImportedPage(reader, pageCount); cb.addTemplate(page, 1, 0, 0, 1, 0, 0); PdfOutline root = cb.getRootOutline(); if (pageCount == 1) { String pageName = bookmarksList.get(count); cb.addOutline(new PdfOutline(root, new PdfDestination(PdfDestination.FITH), pageName), pageName); } } } if (document != null) { try { document.close(); return mergedPdfReport.toByteArray(); } catch (Exception e) { LOG.error("Exception occured because the generated PDF document has no pages", e); } } return null; }
From source file:org.kuali.coeus.s2sgen.impl.print.S2SPrintingServiceImpl.java
License:Educational Community License
/** * @param pdfBytesList List containing the PDF data bytes * @param bookmarksList List of bookmarks corresponding to the PDF bytes. *//*w w w .j a v a2 s .c o m*/ protected byte[] mergePdfBytes(List<byte[]> pdfBytesList, List<String> bookmarksList, boolean headerFooterRequired) { Document document = null; PdfWriter writer = null; ByteArrayOutputStream mergedPdfReport = new ByteArrayOutputStream(); int totalNumOfPages = 0; PdfReader[] pdfReaderArr = new PdfReader[pdfBytesList.size()]; int pdfReaderCount = 0; for (byte[] fileBytes : pdfBytesList) { LOG.debug("File Size " + fileBytes.length + " For " + bookmarksList.get(pdfReaderCount)); PdfReader reader = null; try { reader = new PdfReader(fileBytes); pdfReaderArr[pdfReaderCount] = reader; pdfReaderCount = pdfReaderCount + 1; totalNumOfPages += reader.getNumberOfPages(); } catch (IOException e) { LOG.error(e.getMessage(), e); } } HeaderFooter footer = null; if (headerFooterRequired) { Calendar calendar = Calendar.getInstance(); String dateString = formateCalendar(calendar); StringBuilder footerPhStr = new StringBuilder(); footerPhStr.append(" of "); footerPhStr.append(totalNumOfPages); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_76)); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_76)); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_60)); footerPhStr.append(dateString); Font font = FontFactory.getFont(FontFactory.TIMES, 8, Font.NORMAL, Color.BLACK); Phrase beforePhrase = new Phrase("Page ", font); Phrase afterPhrase = new Phrase(footerPhStr.toString(), font); footer = new HeaderFooter(beforePhrase, afterPhrase); footer.setAlignment(Element.ALIGN_BASELINE); footer.setBorderWidth(0f); } for (int count = 0; count < pdfReaderArr.length; count++) { PdfReader reader = pdfReaderArr[count]; int nop; if (reader == null) { LOG.debug("Empty PDF byetes found for " + bookmarksList.get(count)); continue; } else { nop = reader.getNumberOfPages(); } if (count == 0) { document = nop > 0 ? new Document(reader.getPageSizeWithRotation(1)) : new Document(); try { writer = PdfWriter.getInstance(document, mergedPdfReport); } catch (DocumentException e) { LOG.error(e.getMessage(), e); throw new S2SException(e.getMessage(), e); } if (footer != null) { document.setFooter(footer); } document.open(); } PdfContentByte cb = writer.getDirectContent(); int pageCount = 0; while (pageCount < nop) { document.setPageSize(reader.getPageSize(++pageCount)); document.newPage(); if (footer != null) { document.setFooter(footer); } PdfImportedPage page = writer.getImportedPage(reader, pageCount); cb.addTemplate(page, 1, 0, 0, 1, 0, 0); PdfOutline root = cb.getRootOutline(); if (pageCount == 1) { String pageName = bookmarksList.get(count); cb.addOutline(new PdfOutline(root, new PdfDestination(PdfDestination.FITH), pageName), pageName); } } } if (document != null) { try { document.close(); return mergedPdfReport.toByteArray(); } catch (Exception e) { LOG.error("Exception occured because the generated PDF document has no pages", e); } } return null; }
From source file:org.kuali.kra.printing.service.impl.PrintingServiceImpl.java
License:Educational Community License
/** * @param pdfBytesList//from w w w. j a va 2s . co m * List containing the PDF data bytes * @param bookmarksList * List of bookmarks corresponding to the PDF bytes. * @return * @throws PrintingException */ protected byte[] mergePdfBytes(List<byte[]> pdfBytesList, List<String> bookmarksList, boolean headerFooterRequired) throws PrintingException { Document document = null; PdfWriter writer = null; ByteArrayOutputStream mergedPdfReport = new ByteArrayOutputStream(); int totalNumOfPages = 0; PdfReader[] pdfReaderArr = new PdfReader[pdfBytesList.size()]; int pdfReaderCount = 0; for (byte[] fileBytes : pdfBytesList) { LOG.debug("File Size " + fileBytes.length + " For " + bookmarksList.get(pdfReaderCount)); PdfReader reader = null; try { reader = new PdfReader(fileBytes); pdfReaderArr[pdfReaderCount] = reader; pdfReaderCount = pdfReaderCount + 1; totalNumOfPages += reader.getNumberOfPages(); } catch (IOException e) { LOG.error(e.getMessage(), e); } } HeaderFooter footer = null; if (headerFooterRequired) { Calendar calendar = dateTimeService.getCurrentCalendar(); String dateString = formateCalendar(calendar); StringBuilder footerPhStr = new StringBuilder(); footerPhStr.append(" of "); footerPhStr.append(totalNumOfPages); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_76)); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_76)); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_60)); footerPhStr.append(dateString); Font font = FontFactory.getFont(FontFactory.TIMES, 8, Font.NORMAL, Color.BLACK); Phrase beforePhrase = new Phrase("Page ", font); Phrase afterPhrase = new Phrase(footerPhStr.toString(), font); footer = new HeaderFooter(beforePhrase, afterPhrase); footer.setAlignment(Element.ALIGN_BASELINE); footer.setBorderWidth(0f); } for (int count = 0; count < pdfReaderArr.length; count++) { PdfReader reader = pdfReaderArr[count]; int nop; if (reader == null) { LOG.debug("Empty PDF byetes found for " + bookmarksList.get(count)); continue; } else { nop = reader.getNumberOfPages(); } if (count == 0) { document = nop > 0 ? new com.lowagie.text.Document(reader.getPageSizeWithRotation(1)) : new com.lowagie.text.Document(); try { writer = PdfWriter.getInstance(document, mergedPdfReport); } catch (DocumentException e) { LOG.error(e.getMessage(), e); throw new PrintingException(e.getMessage(), e); } if (footer != null) { document.setFooter(footer); } // writer.setPageEvent(new Watermark()); // add watermark object here document.open(); } PdfContentByte cb = writer.getDirectContent(); int pageCount = 0; while (pageCount < nop) { document.setPageSize(reader.getPageSize(++pageCount)); document.newPage(); if (footer != null) { document.setFooter(footer); } PdfImportedPage page = writer.getImportedPage(reader, pageCount); cb.addTemplate(page, 1, 0, 0, 1, 0, 0); PdfOutline root = cb.getRootOutline(); if (pageCount == 1) { String pageName = bookmarksList.get(count); cb.addOutline(new PdfOutline(root, new PdfDestination(PdfDestination.FITH), pageName), pageName); } } } if (document != null) { try { document.close(); return mergedPdfReport.toByteArray(); } catch (Exception e) { LOG.error("Exception occured because the generated PDF document has no pages", e); } } return null; }
From source file:org.xhtmlrenderer.pdf.ITextOutputDevice.java
License:Open Source License
public void initializePage(PdfContentByte currentPage, float height) { _currentPage = currentPage;/*w w w . j a v a 2 s . c om*/ _pageHeight = height; _currentPage.saveState(); _transform = new AffineTransform(); _transform.scale(1.0d / _dotsPerPoint, 1.0d / _dotsPerPoint); _stroke = transformStroke(STROKE_ONE); _originalStroke = _stroke; _oldStroke = _stroke; setStrokeDiff(_stroke, null); if (_defaultDestination == null) { _defaultDestination = new PdfDestination(PdfDestination.FITH, height); _defaultDestination.addPage(_writer.getPageReference(1)); } _linkTargetAreas = new HashSet(); }
From source file:questions.stamppages.BookmarksToTOC1.java
@SuppressWarnings("unchecked") public static void main(String[] args) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//from w w w.j a va 2s . c o m Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); writer.setPageEvent(new ParagraphBookmarkEvents(false)); document.open(); BufferedReader reader = new BufferedReader(new FileReader(RESOURCE)); String line; Paragraph p; while ((line = reader.readLine()) != null) { p = new Paragraph(line); p.setAlignment(Element.ALIGN_JUSTIFIED); document.add(p); document.add(Chunk.NEWLINE); } reader.close(); document.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } try { PdfReader reader = new PdfReader(baos.toByteArray()); Rectangle rect = reader.getPageSizeWithRotation(1); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); stamper.insertPage(1, rect); ColumnText column = new ColumnText(stamper.getOverContent(1)); column.setSimpleColumn(rect.getLeft(36), rect.getBottom(36), rect.getRight(36), rect.getTop(36)); column.addElement(new Paragraph("TABLE OF CONTENTS")); List<Map> list = SimpleBookmark.getBookmark(reader); Chunk link; PdfAction action; String info; int p = 1; float y = 10; for (Map<String, String> bookmark : list) { link = new Chunk(bookmark.get("Title")); info = bookmark.get("Page"); p = Integer.parseInt(info.substring(0, info.indexOf(' '))); y = Float.parseFloat(info.substring(info.lastIndexOf(' ') + 1) + "f"); action = PdfAction.gotoLocalPage(p, new PdfDestination(PdfDestination.FITH, y), stamper.getWriter()); link.setAction(action); column.addElement(new Paragraph(link)); } column.go(); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.stamppages.ParagraphBookmarkEvents.java
/** * Adds an outline for every new Paragraph * //www. j av a2 s .c o m * @param writer * @param document * @param position */ public void onParagraph(PdfWriter writer, Document document, float position) { n++; PdfContentByte cb = writer.getDirectContent(); PdfOutline root = cb.getRootOutline(); PdfDestination destination = new PdfDestination(PdfDestination.FITH, position); if (named) { cb.localDestination("p" + n, destination); new PdfOutline(root, PdfAction.gotoLocalPage("p" + n, false), "paragraph " + n); } else { new PdfOutline(root, destination, "paragraph " + n); } }
From source file:webBoltOns.server.reportWriter.JRivetWriter.java
License:Open Source License
/** * <h2><code>fireNewSection</code></h2> * //from w w w. ja va2 s .c o m * <p> * Executed by <code>ReportAccumulator</code> when a new section is * detected * </p> * * @param ReportAccumulator ac - Source creating the new section. * @param String newValue - the value of the new section. * */ public void fireNewSection(ReportAccumulator ac, String newValue) { PdfOutline outLine = new PdfOutline(ac.getParentAccumulator().getOutline(), new PdfDestination(PdfDestination.FITH, 50), newValue); outLine.setOpen(false); ac.setOutline(outLine); }