private void exportVectorGraphics(String formatName, File outputFile) throws ImageExportException { Component component = printableComponent.getExportComponent(); int width = component.getWidth(); int height = component.getHeight(); try (FileOutputStream fs = new FileOutputStream(outputFile)) { switch (formatName) { case PDF: // create pdf document with slightly increased width and height // (otherwise the image gets cut off) Document document = new Document(new Rectangle(width + 5, height + 5)); PdfWriter writer = PdfWriter.getInstance(document, fs); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper()); component.print(g2); g2.dispose(); cb.addTemplate(tp, 0, 0); document.close(); break; case SVG: exportFreeHep(component, fs, new SVGGraphics2D(fs, new Dimension(width, height))); break; case EPS: exportFreeHep(component, fs, new PSGraphics2D(fs, new Dimension(width, height))); break; default: // cannot happen break; } } catch (Exception e) { throw new ImageExportException(I18N.getMessage(I18N.getUserErrorMessagesBundle(), "error.image_export.export_failed"), e); } }
@Override public void close() throws IOException { try { float width = 0; float[] w = new float[iMaxWidth.length - iHiddenColumns.size()]; int wi = 0; for (int i = 0; i < iMaxWidth.length; i++) if (!iHiddenColumns.contains(i)) { width += 15f + iMaxWidth[i]; w[wi++] = iMaxWidth[i]; } Document document = new Document(new Rectangle(60f + width, 60f + width * 0.75f), 30f, 30f, 30f, 30f); PdfWriter writer = PdfWriter.getInstance(document, iOutput); writer.setPageEvent(new PdfEventHandler()); document.open(); iTable.setWidths(w); document.add(iTable); document.close(); } catch (DocumentException e) { throw new IOException(e.getMessage(), e); } }
@Override public void createDocument(String documentName, String content) throws CitizenException { String realPath = FILE_PATH + documentName + ".pdf"; Document doc = new Document(); try { PdfWriter.getInstance(doc, new FileOutputStream(realPath)); doc.open(); addMetaData(doc); addTitlePage(doc); addContent(doc, content); } catch (DocumentException | FileNotFoundException e) { throw new CitizenException("Error al generar documento pdf" + " ["+ FILE_PATH+documentName+".pdf] | ["+this.getClass().getName()+"]"); } finally { if (doc != null) { doc.close(); } } }
public static void convertWriteToPdf(BufferedImage bufeBufferedImage, String path) { try { //Image img = Image.getInstance("C:\\Users\\SOFTWARE1\\Desktop\\boshtwain4JImages\\testcapture1507134499431.jpg"); Image img = Image.getInstance(bufeBufferedImage, null); Document document = new Document(img); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); //-- document.open(); img.setAbsolutePosition(0, 0); //-- document.add(img); //-- document.close(); } catch (DocumentException | IOException e) { System.out.println("Intern Log : " + e.getMessage()); } }
public void open(OutputStream out, int mode) throws DocumentException, IOException { iOut = out; if (mode==sModeText) { iPrint = new PrintWriter(iOut); } else { iNrLines = (mode==sModeLedger?116:50); iDoc = new Document(mode==sModeLedger?PageSize.LEDGER.rotate():PageSize.LETTER.rotate()); PdfWriter.getInstance(iDoc, iOut); iDoc.addTitle(iTitle); iDoc.addAuthor("UniTime "+Constants.getVersion()+", www.unitime.org"); iDoc.addSubject(iSubject); iDoc.addCreator("UniTime "+Constants.getVersion()+", www.unitime.org"); iDoc.open(); } iEmpty = true; iPageNo = 0; iLineNo = 0; }
/** * Print footer string on each page * @param writer * @param document */ public void onEndPage(PdfWriter writer, Document document) { if(getDateTime() == null) { setDateTime(new Date()); } PdfContentByte cb = writer.getDirectContent(); cb.beginText(); cb.setFontAndSize(getBaseFont(), getFontSize()); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, getDateFormat().format(getDateTime()), document.left(), 20, 0); cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, String.valueOf(document.getPageNumber()), document.right(), 20, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, MESSAGES.pdfCopyright(Constants.getVersion()), (document.left() + document.right()) / 2, 20, 0); cb.endText(); return; }
@Override protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception { List<Book> books = (List<Book>) model.get("book"); PdfPTable table = new PdfPTable(3); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); table.getDefaultCell().setBackgroundColor(Color.lightGray); table.addCell("Book Name"); table.addCell("Author Name"); table.addCell("Price"); for (Book book : books) { table.addCell(book.getBookName()); table.addCell(book.getAuthor()); table.addCell("" + book.getPrice()); } document.add(table); }
/** * Generates a PDF file with the given text * http://itext.ugent.be/itext-in-action/ * @return A PDF file as a byte array */ public FileTransfer downloadPdfFile(String contents) throws Exception { if (contents == null || contents.length() == 0) { contents = "[BLANK]"; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter.getInstance(document, buffer); document.addCreator("DWR.war using iText"); document.open(); document.add(new Paragraph(contents)); document.close(); return new FileTransfer("example.pdf", "application/pdf", buffer.toByteArray()); }
/** * Returns the PDF version as a name. * @param version the version character. */ public PdfName getVersionAsName(char version) { switch(version) { case PdfWriter.VERSION_1_2: return PdfWriter.PDF_VERSION_1_2; case PdfWriter.VERSION_1_3: return PdfWriter.PDF_VERSION_1_3; case PdfWriter.VERSION_1_4: return PdfWriter.PDF_VERSION_1_4; case PdfWriter.VERSION_1_5: return PdfWriter.PDF_VERSION_1_5; case PdfWriter.VERSION_1_6: return PdfWriter.PDF_VERSION_1_6; case PdfWriter.VERSION_1_7: return PdfWriter.PDF_VERSION_1_7; default: return PdfWriter.PDF_VERSION_1_4; } }
/** * Creates a document with a javascript action. * */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("JavaScriptAction.pdf")); // step 3: we add Javascript as Metadata and we open the document document.open(); // step 4: we add some content Paragraph p = new Paragraph(new Chunk("Click to say Hello").setAction(PdfAction.javaScript( "app.alert('Hello');\r", writer))); document.add(p); // step 5: we close the document document.close(); }
/** * Using FontSelector. */ @Test public void main() throws Exception { // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, PdfTestBase.getOutputStream("fontselection.pdf")); // step 3 document.open(); // step 4 String text = "This text is the first verse of \u275dThe Iliad\u275e. It's not polytonic as it should be " + "with \u2798 and \u279a entoation variants but that's all we have for now.\n\n" + "\u2766\u00a0\u00a0\u039c\u03b7\u03bd\u03b9\u03bd \u03b1\u03b5\u03b9\u03b4\u03b5, \u03b8\u03b5\u03b1, \u03a0\u03b7\u03bb\u03b7\u03b9\u03b1\u03b4\u03b5\u03c9 \u0391\u03c7\u03b9\u03bb\u03b7\u03bf\u03c2"; FontSelector sel = new FontSelector(); sel.addFont(new Font(Font.TIMES_ROMAN, 12)); sel.addFont(new Font(Font.ZAPFDINGBATS, 12)); sel.addFont(new Font(Font.SYMBOL, 12)); Phrase ph = sel.process(text); document.add(new Paragraph(ph)); // step 5 document.close(); }
/** * Creates a document. * * @param outputStream The output stream to write the document content. * @param pageSize the page size. * @return A Document. */ public static Document openDocument( OutputStream outputStream, Rectangle pageSize ) { try { Document document = new Document( pageSize ); PdfWriter.getInstance( document, outputStream ); document.open(); return document; } catch ( DocumentException ex ) { throw new RuntimeException( "Failed to open PDF document", ex ); } }
/** * Creates a document with Named Actions. * * @param args The file to open */ public void main(String... args) throws Exception { // step 1: creation of a document-object Document document = new Document(PageSize.A4, 50, 50, 50, 50); // step 2: we create a writer that listens to the document PdfWriter.getInstance(document, PdfTestBase.getOutputStream("OpenApplication.pdf")); // step 3: we open the document document.open(); // step 4: we add some content String application = args[0]; Paragraph p = new Paragraph(new Chunk("Click to open " + application).setAction( new PdfAction(application, null, null, null))); document.add(p); // step 5: we close the document document.close(); }
/** * Generates a PDF file with the text 'Hello World' * */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter.getInstance(document, PdfTestBase.getOutputStream("HelloWorld.pdf")); // step 3: we open the document document.open(); // step 4: we add a paragraph to the document document.add(new Paragraph("Hello World")); // step 5: we close the document document.close(); }
/** * Creates a PDF document with pages in portrait/landscape. */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(PageSize.A4.rotate()); // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter.getInstance(document, PdfTestBase.getOutputStream("LandscapePortrait.pdf")); // step 3: we open the document document.open(); // step 4: we add some content document.add(new Paragraph( "To create a document in landscape format, just make the height smaller than the width. For instance by rotating the PageSize Rectangle: PageSize.A4.rotate()")); document.setPageSize(PageSize.A4); document.newPage(); document.add(new Paragraph("This is portrait again")); // step 5: we close the document document.close(); }
public void onEndPage( PdfWriter writer, Document document ) { //Footer contains page numbers and date printed on all pages PdfContentByte cb = writer.getDirectContent(); cb.saveState(); String strFooter = promoTxt + " " + formatter.format(now); float textBase = document.bottom(); cb.beginText(); cb.setFontAndSize(font.getBaseFont(),FONTSIZE); Rectangle page = document.getPageSize(); float width = page.getWidth(); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, strFooter, (width/2.0f), textBase - 20, 0); strFooter = "-" + writer.getPageNumber() + "-"; cb.showTextAligned(PdfContentByte.ALIGN_CENTER, strFooter, (width/2.0f), textBase-10, 0); cb.endText(); cb.restoreState(); }
@Test public void testExtractFromEmptyPage2() throws Exception { Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); document.open(); document.add(new Paragraph("Example 1")); document.newPage(); document.add(new Paragraph(" ")); document.newPage(); document.add(new Paragraph("Example 3")); document.close(); byte[] data = baos.toByteArray(); String result1 = getString(data, 1); Assert.assertEquals("Example 1", result1); String result2 = getString(data, 2); Assert.assertEquals(" ", result2); String result3 = getString(data, 3); Assert.assertEquals("Example 3", result3); }
private void insertTable_HiddenValue( PdfPTable mainTable, Rectangle rectangle, PdfWriter writer, String fieldName, String value ) throws IOException, DocumentException { boolean hasBorder = false; // Add Organization ID/Period textfield // Create A table to add for each group AT HERE PdfPTable table = new PdfPTable( 1 ); // Code 1 addCell_WithTextField( table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), fieldName, value ); // Add to the main table PdfPCell cell_withInnerTable = new PdfPCell( table ); // cell_withInnerTable.setPadding(0); cell_withInnerTable.setBorder( Rectangle.NO_BORDER ); mainTable.addCell( cell_withInnerTable ); }
/** * Break a large table up into several smaller tables for memory management * purposes. * */ @Test public void main() throws Exception { // step1 Document document = new Document(PageSize.A4, 10, 10, 10, 10); // step2 PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("SplitTable.pdf")); // step3 document.open(); // step4 PdfContentByte cb = writer.getDirectContent(); PdfPTable table = new PdfPTable(10); for (int k = 1; k <= 100; ++k) { table.addCell("The number " + k); } table.setTotalWidth(800); table.writeSelectedRows(0, 5, 0, -1, 50, 650, cb); document.newPage(); table.writeSelectedRows(5, -1, 0, -1, 50, 650, cb); document.close(); // step5 document.close(); }
@RequestMapping( value = "/programStage/{programStageUid}", method = RequestMethod.GET ) public void getFormPdfProgramStage( @PathVariable String programStageUid, HttpServletRequest request, HttpServletResponse response, OutputStream out ) throws Exception { Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance( document, baos ); PdfFormFontSettings pdfFormFontSettings = new PdfFormFontSettings(); PdfDataEntryFormUtil.setDefaultFooterOnDocument( document, request.getServerName(), pdfFormFontSettings.getFont( PdfFormFontSettings.FONTTYPE_FOOTER ) ); pdfDataEntryFormService.generatePDFDataEntryForm( document, writer, programStageUid, PdfDataEntryFormUtil.DATATYPE_PROGRAMSTAGE, PdfDataEntryFormUtil.getDefaultPageSize( PdfDataEntryFormUtil.DATATYPE_PROGRAMSTAGE ), pdfFormFontSettings, i18nManager.getI18nFormat() ); String fileName = programStageService.getProgramStage( programStageUid ).getName() + " " + DateUtils.getMediumDateString() + ".pdf"; contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PDF, CacheStrategy.NO_CACHE, fileName, true ); response.setContentLength( baos.size() ); baos.writeTo( out ); }
/** * Creates a document with chained Actions. * */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("ChainedActions.pdf")); // step 3: we add Javascript as Metadata and we open the document document.open(); // step 4: we add some content PdfAction action = PdfAction.javaScript("app.alert('Welcome at my site');\r", writer); action.next(new PdfAction("http://www.lowagie.com/iText/")); Paragraph p = new Paragraph(new Chunk("Click to go to Bruno's site").setAction(action)); document.add(p); // step 5: we close the document document.close(); }
/** * Convert HTML to PDF */ public void html2pdf(File input, File output) throws ConversionException, DatabaseException, IOException { log.debug("** Convert from HTML to PDF **"); FileOutputStream fos = null; try { fos = new FileOutputStream(output); // Make conversion Document doc = new Document(PageSize.A4); PdfWriter.getInstance(doc, fos); doc.open(); HTMLWorker html = new HTMLWorker(doc); html.parse(new FileReader(input)); doc.close(); } catch (DocumentException e) { throw new ConversionException("Exception in conversion: " + e.getMessage(), e); } finally { IOUtils.closeQuietly(fos); } }
public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState(); String text = "Page " + writer.getPageNumber() + " of "; // height where text starts float textBase = document.bottom() - getBaseOffset(); float textSize = getFont().getWidthPoint(text, getFontSize()); float width = document.getPageSize().getWidth(); float center = width / 2.0f; cb.beginText(); cb.setFontAndSize(getFont(), getFontSize()); cb.setTextMatrix(document.left(), textBase); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text, center, textBase, 0); cb.endText(); cb.addTemplate(total, center + (textSize / 2.0f), textBase); cb.restoreState(); }
/** * A very simple Table example. * */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter.getInstance(document, PdfTestBase.getOutputStream("MyFirstTable.pdf")); // step 3: we open the document document.open(); // step 4: we create a table and add it to the document Table table = new Table(2, 2); // 2 rows, 2 columns table.addCell("0.0"); table.addCell("0.1"); table.addCell("1.0"); table.addCell("1.1"); document.add(table); document.add(new Paragraph("converted to PdfPTable:")); table.setConvert2pdfptable(true); document.add(table); // step 5: we close the document document.close(); }
/** * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ public void onStartPage(PdfWriter writer, Document document) { if (writer.getPageNumber() < 3) { PdfContentByte cb = writer.getDirectContentUnder(); cb.saveState(); cb.setColorFill(Color.pink); cb.beginText(); cb.setFontAndSize(helv, 48); cb.showTextAligned(Element.ALIGN_CENTER, "My Watermark Under " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45); cb.endText(); cb.restoreState(); } }
private void addCell_WithDropDownListField( PdfPTable table, Rectangle rect, PdfWriter writer, PdfPCell cell, String strfldName, String[] optionList, String[] valueList ) throws IOException, DocumentException { TextField textList = new TextField( writer, rect, strfldName ); textList.setChoices( optionList ); textList.setChoiceExports( valueList ); textList.setBorderWidth( 1 ); textList.setBorderColor( Color.BLACK ); textList.setBorderStyle( PdfBorderDictionary.STYLE_SOLID ); textList.setBackgroundColor( COLOR_BACKGROUDTEXTBOX ); PdfFormField dropDown = textList.getComboField(); cell.setCellEvent( new PdfFieldCell( dropDown, rect.getWidth(), rect.getHeight(), writer ) ); table.addCell( cell ); }
/** * Demonstrates the use of PageEvents. */ @Test public void main() throws Exception { Document document = new Document(PageSize.A4, 50, 50, 70, 70); PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("endpage.pdf")); writer.setPageEvent(new EndPageTest()); document.open(); String text = "Lots of text. "; for (int k = 0; k < 10; ++k) text += text; document.add(new Paragraph(text)); document.close(); }
/** * After the content of the page is written, we put page X of Y at the * bottom of the page and we add either "Romeo and Juliet" of the title * of the current act as a header. * * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, * com.lowagie.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { int pageN = writer.getPageNumber(); String text = "Page " + pageN + " of "; float len = bf.getWidthPoint(text, 8); cb.beginText(); cb.setFontAndSize(bf, 8); cb.setTextMatrix(280, 30); cb.showText(text); cb.endText(); cb.addTemplate(template, 280 + len, 30); cb.beginText(); cb.setFontAndSize(bf, 8); cb.setTextMatrix(280, 820); if (pageN % 2 == 1) { cb.showText("Romeo and Juliet"); } else { cb.showText(act); } cb.endText(); }
/** * Demonstrates the alignment method. */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: creation of a writer PdfWriter.getInstance(document, PdfTestBase.getOutputStream("alignment.pdf")); // step 3: we open the document document.open(); Image gif = Image.getInstance(PdfTestBase.RESOURCES_DIR + "vonnegut.gif"); gif.setAlignment(Image.RIGHT); Image jpeg = Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg"); jpeg.setAlignment(Image.MIDDLE); Image png = Image.getInstance(PdfTestBase.RESOURCES_DIR + "hitchcock.png"); png.setAlignment(Image.LEFT); document.add(gif); document.add(jpeg); document.add(png); // step 5: we close the document document.close(); }
/** * * @param pdfOut * @param pdfIn * @param pagesToDelete * @throws Exception */ public static void extractPages(OutputStream pdfOut,InputStream pdfIn, int[] pages) throws Exception{ if (pages.length <= 0) { throw new Exception("Debe eliminar al menos una p�gina"); } List pagesToKeep = new ArrayList(); for (int i=0;i<pages.length;i++){ pagesToKeep.add(new Integer(pages[i])); } PdfCopyFields writer = new PdfCopyFields(pdfOut); int permission=0; PdfReader reader = new PdfReader(pdfIn); permission = reader.getPermissions(); if (permission != 0){ writer.setEncryption(null, null,permission, PdfWriter.STRENGTH40BITS); } writer.addDocument(reader,pagesToKeep); writer.close(); }
/** * Specifying an encoding. */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: creation of the writer PdfWriter.getInstance(document, PdfTestBase.getOutputStream("fontencoding.pdf")); // step 3: we open the document document.open(); // step 4: we add content to the document BaseFont helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED); Font font = new Font(helvetica, 12, Font.NORMAL); Chunk chunk = new Chunk("Sponsor this example and send me 1\u20ac. These are some special characters: \u0152\u0153\u0160\u0161\u0178\u017D\u0192\u02DC\u2020\u2021\u2030", font); document.add(chunk); // step 5: we close the document document.close(); }
/** * How to substiture special characters with Phrase.getInstance. */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: // we create a writer that listens to the document PdfWriter.getInstance(document, PdfTestBase.getOutputStream("SymbolSubstitution.pdf")); // step 3: we open the document document.open(); // step 4: document.add(Phrase.getInstance("What is the " + (char) 945 + "-coefficient of the " + (char) 946 + "-factor in the " + (char) 947 + "-equation?\n")); for (int i = 913; i < 970; i++) { document.add(Phrase.getInstance(" " + i + ": " + (char) i)); } // step 5: we close the document document.close(); }
protected void buildPdfDocument(Map<String, Object> map, Document document, PdfWriter pdfWriter, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { Map<String,String> userData = (Map<String,String>) map.get("userData"); Table table = new Table(2); table.addCell("No."); table.addCell("User Name"); for (Map.Entry<String, String> entry : userData.entrySet()) { table.addCell(entry.getKey()); table.addCell(entry.getValue()); } document.add(table); }
@Override protected void buildPdfDocument(Map<String, Object> map, Document document, PdfWriter pdfWriter, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { Grade grade = (Grade) map.get("grade"); String validationLink = (String) map.get("validationLink"); document.add(generateTitle()); document.add(EMPTY_LINE); document.add(generateContent(grade)); document.add(EMPTY_LINE); document.add(generateValidation(validationLink)); }
/** * Initialize Pdf footer * @param document * @param outputStream * @return PdfWriter */ public static PdfWriter initFooter(Document document, OutputStream outputStream) throws DocumentException, IOException { PdfWriter iWriter = PdfWriter.getInstance(document, outputStream); iWriter.setPageEvent(new PdfEventHandler()); return iWriter; }
private PdfWorksheet(OutputStream out, Collection<SubjectArea> subjectAreas, String courseNumber) throws IOException, DocumentException { iUseCommitedAssignments = ApplicationProperty.WorksheetPdfUseCommittedAssignments.isTrue(); iSubjectAreas = new TreeSet<SubjectArea>(new Comparator<SubjectArea>() { @Override public int compare(SubjectArea s1, SubjectArea s2) { return s1.getSubjectAreaAbbreviation().compareTo(s2.getSubjectAreaAbbreviation()); } }); iSubjectAreas.addAll(subjectAreas); iCourseNumber = courseNumber; if (iCourseNumber!=null && (iCourseNumber.trim().length()==0 || "*".equals(iCourseNumber.trim().length()))) iCourseNumber = null; iDoc = new Document(PageSize.LETTER.rotate()); iOut = out; PdfWriter.getInstance(iDoc, iOut); String session = null; String subjects = ""; for (SubjectArea sa: iSubjectAreas) { if (subjects.isEmpty()) subjects += ", "; subjects += sa.getSubjectAreaAbbreviation(); if (session == null) session += sa.getSession().getLabel(); } iDoc.addTitle(subjects + (iCourseNumber==null?"":" "+iCourseNumber) + " Worksheet"); iDoc.addAuthor(ApplicationProperty.WorksheetPdfAuthor.value().replace("%", Constants.getVersion())); iDoc.addSubject(subjects + (session == null ? "" : " -- " + session)); iDoc.addCreator("UniTime "+Constants.getVersion()+", www.unitime.org"); if (!iSubjectAreas.isEmpty()) iCurrentSubjectArea = iSubjectAreas.first(); iDoc.open(); printHeader(); }
/** * SetSkew. * */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: // we create a writer that listens to the document PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Skew.pdf")); // step 3: we open the document document.open(); // step 4: Paragraph p = new Paragraph("Skew test:"); document.add(p); Chunk chunk = new Chunk("TESTING skew"); chunk.setSkew(45f, 0f); document.add(chunk); document.add(Chunk.NEWLINE); chunk.setSkew(0f, 45f); document.add(chunk); document.add(Chunk.NEWLINE); chunk.setSkew(-45f, 0f); document.add(chunk); document.add(Chunk.NEWLINE); chunk.setSkew(0f, -45f); document.add(chunk); document.add(Chunk.NEWLINE); chunk.setSkew(15f, 30f); document.add(chunk); document.add(Chunk.NEWLINE); Chunk italic = new Chunk("This looks like Font.ITALIC"); italic.setSkew(0f, 12f); document.add(italic); // step 5: we close the document document.close(); }
public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException { switch(annot.annotationType()) { case Annotation.URL_NET: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL) annot.attributes().get(Annotation.URL))); case Annotation.URL_AS_STRING: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE))); case Annotation.FILE_DEST: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), (String) annot.attributes().get(Annotation.DESTINATION))); case Annotation.SCREEN: boolean sparams[] = (boolean[])annot.attributes().get(Annotation.PARAMETERS); String fname = (String) annot.attributes().get(Annotation.FILE); String mimetype = (String) annot.attributes().get(Annotation.MIMETYPE); PdfFileSpecification fs; if (sparams[0]) fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null); else fs = PdfFileSpecification.fileExtern(writer, fname); PdfAnnotation ann = PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()), fname, fs, mimetype, sparams[1]); return ann; case Annotation.FILE_PAGE: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), ((Integer) annot.attributes().get(Annotation.PAGE)).intValue())); case Annotation.NAMED_DEST: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction(((Integer) annot.attributes().get(Annotation.NAMED)).intValue())); case Annotation.LAUNCH: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.APPLICATION),(String) annot.attributes().get(Annotation.PARAMETERS),(String) annot.attributes().get(Annotation.OPERATION),(String) annot.attributes().get(Annotation.DEFAULTDIR))); default: return new PdfAnnotation(writer, defaultRect.getLeft(), defaultRect.getBottom(), defaultRect.getRight(), defaultRect.getTop(), new PdfString(annot.title(), PdfObject.TEXT_UNICODE), new PdfString(annot.content(), PdfObject.TEXT_UNICODE)); } }
/** * Embedding True Type Fonts. */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "truetype.pdf")); // step 3: we open the document document.open(); // step 4: we add content to the document BaseFont bfComic = BaseFont.createFont(PdfTestBase.RESOURCES_DIR + "/liberation-fonts-ttf/LiberationSans-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font = new Font(bfComic, 12); String text1 = "This is the quite popular True Type font 'LiberationSans'."; String text2 = "Some greek characters: \u0393\u0394\u03b6"; String text3 = "Some cyrillic characters: \u0418\u044f"; document.add(new Paragraph(text1, font)); document.add(new Paragraph(text2, font)); document.add(new Paragraph(text3, font)); // step 5: we close the document document.close(); }