package tutorial.chapter01; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.LinkedList; import java.util.List; import com.itextpdf.barcodes.BarcodeQRCode; import com.itextpdf.io.image.ImageDataFactory; import com.itextpdf.kernel.colors.ColorConstants; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfNumber; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.borders.SolidBorder; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Image; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.property.TextAlignment; import com.itextpdf.layout.property.UnitValue; import com.wrapper.spotify.SpotifyApi; import com.wrapper.spotify.exceptions.SpotifyWebApiException; import com.wrapper.spotify.model_objects.credentials.ClientCredentials; import com.wrapper.spotify.model_objects.specification.Album; import com.wrapper.spotify.model_objects.specification.Track; import com.wrapper.spotify.requests.authorization.client_credentials.ClientCredentialsRequest; public class SimpleTable { public static final String[] albums = new String[] { "spotify:album:1X2HXE2lAqdmD8VjiBX8Tn", "spotify:album:5nCA2mAr3rFzk1A6LZXDYm", "spotify:album:1Vc2BZntRGIdv3Ul8AM6cz", "spotify:album:0H3rE11E9DrD3j2gEvQJ5R", "spotify:album:0YSWLbCg5SpxAb2VOON3mX", "spotify:track:7oolFzHipTMg2nL7shhdz2", "http://streamtdy.ir-media-tec.com/live/mp3-128/web/play.mp3", "hidrive/Kinder/Astrid Lindgren - Lotta zieht um", "hidrive/Singles/04 - The Funeral.mp3" }; public static final int cardsPerLine = 9; public static final boolean retrieveImageFiles = true; public static final String DEST = "results/simple_table.pdf"; public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180); public static final PdfNumber LANDSCAPE = new PdfNumber(90); public static final PdfNumber PORTRAIT = new PdfNumber(0); public static final PdfNumber SEASCAPE = new PdfNumber(270); private static final String clientId = "d104434912874fbf9ff42e5a91db5179"; private static final String clientSecret = "6acdb874210942128ba4991c4f7228b5"; private static final SpotifyApi spotifyApi = new SpotifyApi.Builder().setClientId(clientId) .setClientSecret(clientSecret).build(); private static final ClientCredentialsRequest clientCredentialsRequest = spotifyApi.clientCredentials().build(); protected List fetchSpotifyData(String[] links) throws SpotifyWebApiException, IOException { List cards = new LinkedList(); for (String link : links) { System.out.println(link); Card card = new Card(link); card.qrcode = new BarcodeQRCode(link); cards.add(card); if (link.startsWith("spotify:album:")) { String spotifyId = link.substring(14); Album album = spotifyApi.getAlbum(spotifyId).build().execute(); card.title = album.getName(); card.artist = album.getArtists()[0].getName(); URL url = new URL(album.getImages()[0].getUrl()); card.cover = new Image(ImageDataFactory.create(url)); } else if (link.startsWith("spotify:track:") ) { String spotifyId = link.substring(14); Track track = spotifyApi.getTrack(spotifyId).build().execute(); card.title = track.getName(); card.artist = track.getArtists()[0].getName(); URL url = new URL(track.getAlbum().getImages()[0].getUrl()); card.cover = new Image(ImageDataFactory.create(url)); } else if (link.startsWith("http://")) { card.title = link; card.artist = ""; } else { card.title = link; card.artist = ""; } } return cards; } public static void main(String[] args) throws Exception { SimpleTable creator = new SimpleTable(); List cards; try { final ClientCredentials clientCredentials = clientCredentialsRequest.execute(); // Set access token for further "spotifyApi" object usage spotifyApi.setAccessToken(clientCredentials.getAccessToken()); System.out.println("Spotify token expires in: " + clientCredentials.getExpiresIn() + " s"); cards = creator.fetchSpotifyData(albums); File file = new File(DEST); file.getParentFile().mkdirs(); creator.manipulatePdf(DEST, cards); } catch (IOException | SpotifyWebApiException e) { System.out.println("Error: " + e.getMessage()); } } protected void manipulatePdf(String dest, List cards) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); pdfDoc.setDefaultPageSize(PageSize.A4.rotate()); Document doc = new Document(pdfDoc); doc.setMargins(0, 0, 0, 0); UnitValue[] colWidths = new UnitValue[9]; for (int i = 0; i < colWidths.length; i++) colWidths[i] = new UnitValue(UnitValue.PERCENT, 11.10f); Table table = new Table(colWidths); table.useAllAvailableWidth(); // for (int i = 0; i < 17; i++) { // table.addCell("hi"); // } // TODO // if (cardsPerLine < 9) { // int i = 0; // // do { // table.addCell(""); // i++; // table.addCell(""); // i++; // } while (cardsPerLine + i < 9); // } for (Card card : cards) { Cell cell = new Cell(); cell.setPadding(0); Paragraph p = new Paragraph(); p.setTextAlignment(TextAlignment.CENTER); p.setPaddingTop(43.62f); Image qrcode = new Image(card.qrcode.createFormXObject(pdfDoc)); qrcode.setWidth(10 / 25.4f * 72); p.add(qrcode); cell.add(p); p = new Paragraph(); p.setTextAlignment(TextAlignment.CENTER); p.setPaddingTop(5 / 25.4f * 72); if (card.cover != null) { Image cover = card.cover; cover.setWidth(30 / 25.4f * 72); p.add(cover); } else { p.setPaddingBottom(30 / 25.4f * 72); } cell.add(p); p = new Paragraph(); p.setTextAlignment(TextAlignment.CENTER); p.setPaddingTop(5 / 25.4f * 72); p.setFontSize(8f); p.add(card.artist); cell.add(p); p = new Paragraph(); p.setTextAlignment(TextAlignment.CENTER); p.setPaddingTop(3 / 25.4f * 72); p.setFontSize(9f); p.setBold(); p.add(card.title); cell.setHeight(105 / 25.4f * 72); cell.add(p); cell.setBorder(new SolidBorder(ColorConstants.GRAY, 0.5f, 0.5f)); table.addCell(cell); } doc.add(table); // create backside // doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE)); doc.close(); } } class Card { String title; String artist; BarcodeQRCode qrcode; Image cover; String link; public Card(String link) { this.link = link; } }