1 50 package com.lowagie.tools.plugins; 51 52 import java.awt.Color; 53 import java.io.File; 54 import java.io.FileOutputStream; 55 56 import javax.swing.JInternalFrame; 57 import javax.swing.JOptionPane; 58 59 import com.lowagie.text.Document; 60 import com.lowagie.text.DocumentException; 61 import com.lowagie.text.Element; 62 import com.lowagie.text.Image; 63 import com.lowagie.text.Rectangle; 64 import com.lowagie.text.pdf.BaseFont; 65 import com.lowagie.text.pdf.PdfContentByte; 66 import com.lowagie.text.pdf.PdfWriter; 67 import com.lowagie.tools.arguments.FileArgument; 68 import com.lowagie.tools.arguments.ImageArgument; 69 import com.lowagie.tools.arguments.PdfFilter; 70 import com.lowagie.tools.arguments.ToolArgument; 71 72 75 public class DvdCover extends AbstractTool { 76 77 80 public DvdCover() { 81 menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW | MENU_EXECUTE_PRINT; 82 arguments.add(new FileArgument(this, "destfile", "The file to which the PDF has to be written", true, new PdfFilter())); 83 arguments.add(new ToolArgument(this, "title", "The title of the DVD", String.class.getName())); 84 arguments.add(new ToolArgument(this, "backgroundcolor", "The backgroundcolor of the DVD Cover (for instance 0xFFFFFF)", Color.class.getName())); 85 arguments.add(new ImageArgument(this, "front", "The front image of the DVD Cover")); 86 arguments.add(new ImageArgument(this, "back", "The back image of the DVD Cover")); 87 arguments.add(new ImageArgument(this, "side", "The side image of the DVD Cover")); 88 } 89 90 93 protected void createFrame() { 94 internalFrame = new JInternalFrame("Make your own DVD Cover", true, true, true); 95 internalFrame.setSize(500, 300); 96 internalFrame.setJMenuBar(getMenubar()); 97 } 98 99 102 public void execute() { 103 try { 104 Rectangle pageSize = new Rectangle(792, 525); 106 if (getValue("backgroundcolor") != null) pageSize.setBackgroundColor((Color)getValue("backgroundcolor")); 107 Document document = new Document(pageSize); 108 if (getValue("destfile") == null) throw new DocumentException("You must provide a destination file!"); 112 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream((File)getValue("destfile"))); 113 114 document.open(); 116 117 PdfContentByte cb = writer.getDirectContent(); 119 if (getValue("title") != null) { 120 cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24); 121 cb.beginText(); 122 if (getValue("front") == null) { 123 cb.showTextAligned(Element.ALIGN_CENTER, (String)getValue("title"), 600f, 262f, 0f); 124 } 125 if (getValue("side") == null) { 126 cb.showTextAligned(Element.ALIGN_CENTER, (String)getValue("title"), 390f, 262f, 270f); 127 } 128 cb.endText(); 129 } 130 cb.moveTo(376, 0); 131 cb.lineTo(376, 525); 132 cb.moveTo(416, 525); 133 cb.lineTo(416, 0); 134 cb.stroke(); 135 if (getValue("front") != null) { 136 Image front = (Image)getValue("front"); 137 front.scaleToFit(376, 525); 138 front.setAbsolutePosition(410f + (376f - front.scaledWidth()) / 2f, (525f - front.scaledHeight()) / 2f); 139 document.add(front); 140 } 141 if (getValue("back") != null) { 142 Image back = (Image)getValue("back"); 143 back.scaleToFit(376, 525); 144 back.setAbsolutePosition((376f - back.scaledWidth()) / 2f, (525f - back.scaledHeight()) / 2f); 145 document.add(back); 146 } 147 if (getValue("side") != null) { 148 Image side = (Image)getValue("side"); 149 side.scaleToFit(40, 525); 150 side.setAbsolutePosition(376 + (40f - side.scaledWidth()) / 2f, (525f - side.scaledHeight()) / 2f); 151 document.add(side); 152 } 153 154 document.close(); 156 } 157 catch(Exception e) { 158 JOptionPane.showMessageDialog(internalFrame, 159 e.getMessage(), 160 e.getClass().getName(), 161 JOptionPane.ERROR_MESSAGE); 162 System.err.println(e.getMessage()); 163 } 164 } 165 166 169 public void valueHasChanged(ToolArgument arg) { 170 if (internalFrame == null) { 171 return; 173 } 174 } 176 177 181 public static void main(String[] args) { 182 DvdCover tool = new DvdCover(); 183 if (args.length == 0) { 184 System.err.println(tool.getUsage()); 185 } 186 tool.setArguments(args); 187 tool.execute(); 188 } 189 190 193 protected File getDestPathPDF() throws InstantiationException { 194 return (File)getValue("destfile"); 195 } 196 } | Popular Tags |