List of usage examples for com.itextpdf.text Document setMarginMirroring
public boolean setMarginMirroring(boolean marginMirroring)
From source file:com.bdaum.zoom.email.internal.job.PdfJob.java
License:Open Source License
private Document createDocument() { Document document = new Document(); document.addCreationDate();// w w w.j ava 2 s . c o m document.addCreator(Constants.APPLICATION_NAME); document.addAuthor(System.getProperty("user.name")); //$NON-NLS-1$ document.setPageSize(format); document.setMargins(Math.max(0, leftMargins - horizontalGap / 2), Math.max(0, rightMargins - horizontalGap / 2), topMargins, 0); document.setMarginMirroring(layout.getFacingPages()); return document; }
From source file:com.ideationdesignservices.txtbook.pdf.TxtBookPdf.java
public void createPDF(Context context) throws DocumentException, IOException { LicenseKey.loadLicenseFile(context.getAssets().open("itextkey.xml")); this.mContext = context; Map<String, String> pdfParams = new HashMap(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_Hmmss", Locale.US); dateFormat.setTimeZone(TimeZone.getDefault()); this.filename = "txtbook_" + dateFormat.format(new Date()) + ".pdf"; float leftMargin = Txtbook.RIGHT_MARGIN_EVEN; float rightMargin = Txtbook.RIGHT_MARGIN_ODD; if (this.settings.addFrontCover.booleanValue()) { leftMargin = Txtbook.RIGHT_MARGIN_ODD; rightMargin = Txtbook.RIGHT_MARGIN_EVEN; }/*from w w w .j av a2 s .co m*/ Document document = new Document(PageSize.LETTER, leftMargin, rightMargin, Txtbook.TOP_MARGIN, Txtbook.TOP_MARGIN); File file = new File(Environment.getExternalStorageDirectory(), this.filename); if (!(file.exists() && file.canRead())) { file.createNewFile(); } this.writer = PdfWriter.getInstance(document, new FileOutputStream(file)); this.writer.setCompressionLevel(this.settings.compressionLevel); this.writer.setStrictImageSequence(true); FontFactory.register("assets/fonts/DroidSans.ttf"); this.sansFont6Gray = FontFactory.getFont("DroidSans", BaseFont.WINANSI, true, 6.0f, 0, new BaseColor(152, 152, 152)); this.sansFont9 = FontFactory.getFont("DroidSans", BaseFont.WINANSI, true, 9.0f); this.sansFont9Gray = FontFactory.getFont("DroidSans", BaseFont.WINANSI, true, 9.0f, 0, new BaseColor(103, 103, 103)); this.sansFont11Gray = FontFactory.getFont("DroidSans", BaseFont.WINANSI, true, 11.0f, 0, new BaseColor(152, 152, 152)); FontFactory.register("assets/fonts/CourierNew.ttf"); this.serifFont8Gray = FontFactory.getFont("Courier New", BaseFont.WINANSI, true, 8.0f, 0, new BaseColor(103, 103, 103)); this.serifFont11 = FontFactory.getFont("Courier New", BaseFont.WINANSI, true, 11.0f); this.serifFont14 = FontFactory.getFont("Courier New", BaseFont.WINANSI, true, 14.0f); this.serifFont24 = FontFactory.getFont("Courier New", BaseFont.WINANSI, true, 24.0f); document.open(); document.setMarginMirroring(true); document.addCreationDate(); document.addCreator("Created with txt-book for Android www.txt-book.com"); document.addTitle("txt-book for Android"); if (this.settings.bookStyle == 1) { ConversationHeaderFooter hf = new ConversationHeaderFooter(); hf.footerFont = this.sansFont9; hf.hasFrontCover = this.settings.addFrontCover; this.writer.setPageEvent(hf); } else { StorybookHeaderFooter hf2 = new StorybookHeaderFooter(); hf2.title = this.settings.bookCoverTitle; hf2.headerFont = this.serifFont8Gray; hf2.hasFrontCover = this.settings.addFrontCover; this.writer.setPageEvent(hf2); } if (this.settings.addFrontCover.booleanValue()) { createFrontCoverPage(document, this.settings.bookCoverTitle, this.settings.addFrontCoverImage.booleanValue() ? this.settings.coverPhoto : null); pdfParams.put("PDF Front Cover", "YES"); } else { pdfParams.put("PDF Front Cover", "NO"); } if (this.settings.addFrontCoverImage.booleanValue()) { pdfParams.put("PDF Front Cover Image", "YES"); } else { pdfParams.put("PDF Front Cover Image", "NO"); } if (this.settings.bookStyle == 1) { createContentPagesConversation(document); } else { createContentPagesStorybook(document, this.settings.bookCoverTitle); } int pages = this.writer.getPageNumber(); if (this.settings.bookStyle == 2) { ((StorybookHeaderFooter) this.writer.getPageEvent()).reachedEndOfContent = Boolean.valueOf(true); } else { ((ConversationHeaderFooter) this.writer.getPageEvent()).reachedEndOfContent = Boolean.valueOf(true); } if (this.settings.addBackCover.booleanValue()) { pages++; pdfParams.put("PDF Back Cover", "YES"); if (pages % 2 == 1) { createBlankSpacerPage(document); } createBackCoverPage(document, this.settings.backCoverNote); } else { pdfParams.put("PDF Back Cover", "NO"); if (pages % 2 == 1) { createBlankSpacerPage(document); } } if (this.settings.bookStyle == 2) { pdfParams.put("PDF Style", "Storybook"); } else { pdfParams.put("PDF Style", "Conversation"); } pdfParams.put("PDF Num Pages", Integer.valueOf(this.writer.getPageNumber()).toString()); document.close(); FlurryAgent.logEvent("PDF_CREATED", (Map) pdfParams); }
From source file:library.Report.java
public void createPdf(String filename) throws DocumentException, IOException { // step 1/*from w w w . ja v a 2 s. co m*/ Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)); document.addAuthor("Admin"); document.addTitle("Library report"); document.setPageSize(PageSize.A4); document.setMargins(36, 72, 108, 180); document.setMarginMirroring(true); document.open(); Font normal = new Font(Font.FontFamily.HELVETICA, 36f, Font.NORMAL, BaseColor.DARK_GRAY); Font normal1 = new Font(Font.FontFamily.HELVETICA, 18f, Font.NORMAL); Font normal2 = new Font(Font.FontFamily.HELVETICA, 12f, Font.NORMAL); document.add(new Paragraph("KNOWLEDGICA LIBRARY REPORT", normal)); document.add(new Phrase("\n")); String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()); document.add(new Paragraph(timeStamp, normal1)); document.add(new Paragraph("Amount of books " + DF.getText() + "\nAmount of Amount of checked in books " + jTextField1.getText() + "\nAmount of checked out books " + jTextField3.getText() + "" + "\nNunber of users " + jTextField4.getText() + "\nNumber of admins " + jTextField5.getText() + "\nNumber of librarians " + jTextField6.getText() + "\nNumber of guest " + jTextField7.getText() + "\nMost logged user " + jTextField8.getText() + "\nAmount of males " + jTextField9.getText() + "\nAmount of female " + jTextField10.getText() + "", normal2)); document.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);/*from www. j a v a2 s. co 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(); }
From source file:storehausimport.SaveToPdf.java
public boolean save(String inputFilePath, String outputFilePath) throws Exception { if (outputFilePath.isEmpty()) { throw new Exception("Trkst parametrs outputFilePath"); }//from www . j a v a 2 s. c o m if (inputFilePath.isEmpty()) { throw new Exception("Trkst parametrs inputFilePath"); } File inputFile = new File(inputFilePath); Document pdfDoc = new Document(); try { PdfWriter writer = PdfWriter.getInstance(pdfDoc, new FileOutputStream(outputFilePath)); pdfDoc.open(); pdfDoc.setMarginMirroring(true); pdfDoc.setMargins(36, 72, 108, 180); pdfDoc.topMargin(); BaseFont helvetica = BaseFont.createFont("Helvetica", BaseFont.CP1257, BaseFont.NOT_EMBEDDED); Font normal_font = new Font(helvetica, 10, Font.NORMAL); Font bold_font = new Font(); bold_font.setStyle(Font.BOLD); bold_font.setSize(10); pdfDoc.add(new Paragraph("\n")); if (inputFile.exists()) { iStream = new FileInputStream(inputFile); in = new DataInputStream(iStream); is = new InputStreamReader(in); br = new BufferedReader(is); String strLine; while ((strLine = br.readLine()) != null) { Paragraph para = new Paragraph(strLine + "\n", normal_font); para.setAlignment(Element.ALIGN_LEFT); pdfDoc.add(para); } } else { pdfDoc.close(); throw new Exception("Trkst parametrs inputFilePath"); } pdfDoc.close(); } catch (Exception ex) { throw new Exception(ex); } return true; }