Source Code Cross Referenced for PjScript.java in  » PDF » pjx » com » etymon » pj » tools » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » pjx » com.etymon.pj.tools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.etymon.pj.tools;
002:
003:        import java.io.*;
004:        import java.util.*;
005:        import com.etymon.pj.*;
006:        import com.etymon.pj.exception.*;
007:        import com.etymon.pj.object.*;
008:
009:        /**
010:         Implements a PDF scripting language.
011:
012:         Possible result codes are 0 (scripts executed normally), 1 (error
013:         reading script), 2 (syntax error in script), and 3 (error executing
014:         script command).
015:        
016:         @author Nassib Nassar
017:         */
018:        public class PjScript {
019:
020:            public static void main(String[] args) {
021:
022:                if (args.length < 1) {
023:                    System.out
024:                            .println("pjscript [script_file] [script_arguments]");
025:                    return;
026:                }
027:
028:                // open script file
029:                FileReader fr;
030:                BufferedReader br = null;
031:                try {
032:                    fr = new FileReader(args[0]);
033:                    br = new BufferedReader(fr);
034:                } catch (IOException e) {
035:                    System.out.println(new PjScriptException(
036:                            "No such file or directory.", -1, args[0], 1)
037:                            .getFullMessage());
038:                    Runtime.getRuntime().exit(1);
039:                }
040:                String[] scriptArgs = new String[args.length - 1];
041:                System.arraycopy(args, 1, scriptArgs, 0, scriptArgs.length);
042:                try {
043:                    script(args[0], br, scriptArgs);
044:                } catch (PjScriptException e) {
045:                    System.out.println(e.getFullMessage());
046:                    Runtime.getRuntime().exit(e.getErrorType());
047:                }
048:            }
049:
050:            /**
051:                  Creates or modifies PDF files based on a script.
052:                  @param source the file or program name where the script
053:                  originates.  This is used in printing error messages.
054:                  @param br the input stream containing the script.
055:                  @param args the arguments to the script.
056:                  @return the resultant PDF document.
057:                  @exception PjScriptException if an error occurs.
058:             */
059:            public static Pdf script(String source, BufferedReader br,
060:                    String[] args) throws PjScriptException {
061:                // read and process script commands
062:                String line;
063:                StringTokenizer tokenizer;
064:                String command;
065:                String endLine;
066:                Pdf pdf = new Pdf();
067:                int lineNumber = 0;
068:                boolean good;
069:                Hashtable vars = new Hashtable();
070:                Vector texts = new Vector();
071:                texts.setSize(2);
072:                Hashtable fonts = new Hashtable();
073:                // initialize some pjscript variables
074:                if (args != null) {
075:                    for (int x = 0; x < args.length; x++) {
076:                        vars.put("arg" + new Integer(x).toString(), args[x]);
077:                    }
078:                }
079:                vars.put("fontsize", "10");
080:                vars.put("font", "Courier");
081:                vars.put("linewidth", "1");
082:                vars.put("mediabox", "letter-portrait");
083:                vars.put("page", "1");
084:                vars.put("xinit", "72");
085:                vars.put("x", vars.get("xinit"));
086:                vars.put("x0", "0");
087:                vars.put("x1", "0");
088:                vars.put("yinit", "720");
089:                vars.put("y", vars.get("yinit"));
090:                vars.put("y0", "0");
091:                vars.put("y1", "0");
092:                vars.put("ylimit", "72");
093:                do {
094:                    // get a line from the script file
095:                    try {
096:                        line = br.readLine();
097:                    } catch (IOException e) {
098:                        throw new PjScriptException(
099:                                "I/O error reading input stream.", -1, source,
100:                                1);
101:                    }
102:                    if (line != null) {
103:                        lineNumber++;
104:                        // parse out the command at the beginning of the line
105:                        tokenizer = new StringTokenizer(line);
106:                        if (tokenizer.hasMoreTokens()) {
107:                            command = tokenizer.nextToken();
108:                        } else {
109:                            command = new String();
110:                        }
111:                        // parse out the argument portion of the line
112:                        endLine = getEndLine(line);
113:                        // process command
114:                        good = false;
115:                        if (command.equals("")) {
116:                            good = true;
117:                        }
118:                        if (command.startsWith("#")) {
119:                            good = true;
120:                        }
121:                        if (command.equals("newpdf")) {
122:                            good = true;
123:                            pdf = new Pdf();
124:                            texts = new Vector();
125:                            texts.setSize(2);
126:                            fonts = new Hashtable();
127:                            // set the media box
128:                            String mediaBox = (String) (vars.get("mediabox"));
129:                            if (mediaBox == null) {
130:                                throw new PjScriptException(
131:                                        "Media box was not specified.",
132:                                        lineNumber, source, 3);
133:                            }
134:                            float yinit = setMediaBox(pdf, 1, mediaBox, source,
135:                                    lineNumber);
136:                            vars.put("yinit", new PjNumber(yinit).toString());
137:                            vars.put("y", vars.get("yinit"));
138:                        }
139:                        if ((command.startsWith("$")) && (command.length() > 1)) {
140:                            good = true;
141:                            if (endLine.length() == 0) {
142:                                throw new PjScriptException(
143:                                        "Missing argument.", lineNumber,
144:                                        source, 2);
145:                            }
146:                            String value;
147:                            if (endLine.charAt(0) == '\"') {
148:                                if (endLine.charAt(endLine.length() - 1) != '\"') {
149:                                    throw new PjScriptException(
150:                                            "'\"' expected.", lineNumber,
151:                                            source, 2);
152:                                }
153:                                value = endLine.substring(1,
154:                                        endLine.length() - 1);
155:                            } else {
156:                                value = endLine;
157:                            }
158:                            vars.put(command.substring(1), value);
159:                        }
160:                        if ((command.startsWith("+")) && (command.length() > 1)) {
161:                            good = true;
162:                            if (endLine.length() == 0) {
163:                                throw new PjScriptException(
164:                                        "Missing argument.", lineNumber,
165:                                        source, 2);
166:                            }
167:                            String value;
168:                            if (endLine.charAt(0) == '\"') {
169:                                if (endLine.charAt(endLine.length() - 1) != '\"') {
170:                                    throw new PjScriptException(
171:                                            "'\"' expected.", lineNumber,
172:                                            source, 2);
173:                                }
174:                                value = endLine.substring(1,
175:                                        endLine.length() - 1);
176:                            } else {
177:                                value = endLine;
178:                            }
179:                            try {
180:                                float sum = new Float((String) (vars
181:                                        .get(command.substring(1))))
182:                                        .floatValue()
183:                                        + new Float(value).floatValue();
184:                                vars.put(command.substring(1),
185:                                        new PjNumber(sum).toString());
186:                            } catch (NumberFormatException e) {
187:                                throw new PjScriptException(
188:                                        "Argument must be numeric.",
189:                                        lineNumber, source, 2);
190:                            }
191:                        }
192:                        if ((command.startsWith("=")) && (command.length() > 1)) {
193:                            good = true;
194:                            if (endLine.length() == 0) {
195:                                throw new PjScriptException(
196:                                        "Missing argument.", lineNumber,
197:                                        source, 2);
198:                            }
199:                            String value = (String) (vars.get(endLine));
200:                            if (value == null) {
201:                                throw new PjScriptException(
202:                                        "Undefined variable.", lineNumber,
203:                                        source, 3);
204:                            }
205:                            vars.put(command.substring(1), value);
206:                        }
207:                        if ((command.startsWith("?")) && (command.length() > 1)) {
208:                            good = true;
209:                            String value = (String) (vars.get(command
210:                                    .substring(1)));
211:                            if (value == null) {
212:                                throw new PjScriptException(
213:                                        "Undefined variable.", lineNumber,
214:                                        source, 3);
215:                            }
216:                            System.out.print(value);
217:                        }
218:                        if (command.equals("dump")) {
219:                            good = true;
220:                            String key;
221:                            for (Enumeration m = vars.keys(); m
222:                                    .hasMoreElements();) {
223:                                key = (String) (m.nextElement());
224:                                System.out.println(key + ": \""
225:                                        + (String) (vars.get(key)) + "\"");
226:                            }
227:                        }
228:                        if (command.equals("print")) {
229:                            good = true;
230:                            if (!quoted(endLine)) {
231:                                throw new PjScriptException("'\"' expected.",
232:                                        lineNumber, source, 2);
233:                            }
234:                            System.out.print(endLine.substring(1, endLine
235:                                    .length() - 1));
236:                        }
237:                        if (command.equals("println")) {
238:                            good = true;
239:                            if (!quoted(endLine)) {
240:                                throw new PjScriptException("'\"' expected.",
241:                                        lineNumber, source, 2);
242:                            }
243:                            System.out.println(endLine.substring(1, endLine
244:                                    .length() - 1));
245:                        }
246:                        if (command.equals("readpdf")) {
247:                            good = true;
248:                            String fn = (String) (vars.get("file"));
249:                            if (fn == null) {
250:                                throw new PjScriptException(
251:                                        "File was not specified.", lineNumber,
252:                                        source, 3);
253:                            } else {
254:                                try {
255:                                    pdf = new Pdf(fn);
256:                                } catch (IOException ioe) {
257:                                    throw new PjScriptException(
258:                                            "Unable to read PDF file.",
259:                                            lineNumber, source, 3);
260:                                } catch (PjException pje) {
261:                                    throw new PjScriptException(
262:                                            "Error parsing PDF file.",
263:                                            lineNumber, source, 3);
264:                                }
265:                            }
266:                            texts = new Vector();
267:                            try {
268:                                texts.setSize(pdf.getPageCount() + 1);
269:                            } catch (InvalidPdfObjectException e) {
270:                                throw new PjScriptException("PDF error: "
271:                                        + e.getMessage(), lineNumber, source, 3);
272:                            }
273:                            fonts = new Hashtable();
274:                        }
275:                        if (command.equals("appendpdf")) {
276:                            good = true;
277:                            String fn = (String) (vars.get("file"));
278:                            if (fn == null) {
279:                                throw new PjScriptException(
280:                                        "File was not specified.", lineNumber,
281:                                        source, 3);
282:                            } else {
283:                                try {
284:                                    Pdf other = new Pdf(fn);
285:                                    pdf.appendPdfDocument(other);
286:                                } catch (IOException ioe) {
287:                                    throw new PjScriptException(
288:                                            "Unable to read PDF file.",
289:                                            lineNumber, source, 3);
290:                                } catch (PjException pje) {
291:                                    throw new PjScriptException(
292:                                            "Error parsing PDF file.",
293:                                            lineNumber, source, 3);
294:                                }
295:                            }
296:                            texts = new Vector();
297:                            try {
298:                                texts.setSize(pdf.getPageCount() + 1);
299:                            } catch (InvalidPdfObjectException e) {
300:                                throw new PjScriptException("PDF error: "
301:                                        + e.getMessage(), lineNumber, source, 3);
302:                            }
303:                            fonts = new Hashtable();
304:                        }
305:                        if (command.equals("appendpage")) {
306:                            good = true;
307:                            int pageNumber;
308:                            try {
309:                                pageNumber = appendPage(pdf, texts, vars);
310:                            } catch (PjException e) {
311:                                throw new PjScriptException("PDF error: "
312:                                        + e.getMessage(), lineNumber, source, 3);
313:                            }
314:                            // set the media box
315:                            String mediaBox = (String) (vars.get("mediabox"));
316:                            if (mediaBox == null) {
317:                                throw new PjScriptException(
318:                                        "Media box was not specified.",
319:                                        lineNumber, source, 3);
320:                            }
321:                            float yinit = setMediaBox(pdf, pageNumber,
322:                                    mediaBox, source, lineNumber);
323:                            vars.put("yinit", new PjNumber(yinit).toString());
324:                            vars.put("y", vars.get("yinit"));
325:                        }
326:                        if (command.equals("deletepage")) {
327:                            good = true;
328:                            String page = (String) (vars.get("page"));
329:                            int pageCount;
330:                            try {
331:                                pageCount = pdf.getPageCount();
332:                            } catch (InvalidPdfObjectException e) {
333:                                throw new PjScriptException("PDF error: "
334:                                        + e.getMessage(), lineNumber, source, 3);
335:                            }
336:                            if (pageCount <= 0) {
337:                                throw new PjScriptException(
338:                                        "No pages to delete.", lineNumber,
339:                                        source, 3);
340:                            }
341:                            if (pageCount == 1) {
342:                                throw new PjScriptException(
343:                                        "Cannot delete the only page.",
344:                                        lineNumber, source, 3);
345:                            }
346:                            int pageNumber = Integer.parseInt(page);
347:                            try {
348:                                pdf.deletePage(pageNumber);
349:                            } catch (IndexOutOfBoundsException e) {
350:                                throw new PjScriptException(
351:                                        "Page number out of range.",
352:                                        lineNumber, source, 3);
353:                            } catch (InvalidPdfObjectException pe) {
354:                                throw new PjScriptException("PDF error: "
355:                                        + pe.getMessage(), lineNumber, source,
356:                                        3);
357:                            }
358:                        }
359:                        if (command.equals("writepdf")) {
360:                            good = true;
361:                            String fn = (String) (vars.get("file"));
362:                            if (fn == null) {
363:                                throw new PjScriptException(
364:                                        "File was not specified.", lineNumber,
365:                                        source, 3);
366:                            } else {
367:                                // draw texts on the PDF file
368:                                drawText(pdf, source, lineNumber, fonts, texts);
369:
370:                                // write it out
371:                                if (fn.length() > 0) {
372:                                    try {
373:                                        pdf.writeToFile(fn);
374:                                    } catch (IOException ioe) {
375:                                        throw new PjScriptException(
376:                                                "Unable to write PDF file.",
377:                                                lineNumber, source, 3);
378:                                    }
379:                                }
380:                            }
381:                        }
382:                        if (command.equals("initxy")) {
383:                            good = true;
384:                            vars.put("x", vars.get("xinit"));
385:                            vars.put("y", vars.get("yinit"));
386:                        }
387:                        if (command.equals("setinfo")) {
388:                            good = true;
389:                            String key = (String) (vars.get("key"));
390:                            if (key == null) {
391:                                throw new PjScriptException(
392:                                        "Key was not specified.", lineNumber,
393:                                        source, 3);
394:                            }
395:                            String text = (String) (vars.get("text"));
396:                            if (text == null) {
397:                                throw new PjScriptException(
398:                                        "Text was not specified.", lineNumber,
399:                                        source, 3);
400:                            }
401:                            // get the Info dictionary
402:                            PjReference infoRef;
403:                            try {
404:                                infoRef = pdf.getInfoDictionary();
405:                            } catch (InvalidPdfObjectException e) {
406:                                throw new PjScriptException("PDF error: "
407:                                        + e.getMessage(), lineNumber, source, 3);
408:                            }
409:                            PjInfo info;
410:                            if (infoRef == null) {
411:                                // create a new Info dictionary and add it
412:                                info = new PjInfo();
413:                                int infoId = pdf.registerObject(info);
414:                                infoRef = new PjReference(new PjNumber(infoId),
415:                                        PjNumber.ZERO);
416:                                pdf.setInfoDictionary(infoRef);
417:                            } else {
418:                                PjDictionary d = (PjDictionary) (pdf
419:                                        .getObject(infoRef.getObjNumber()
420:                                                .getInt()));
421:                                info = new PjInfo(d.getHashtable());
422:                            }
423:                            // set the new value
424:                            info.getHashtable().put(new PjName(key),
425:                                    new PjString(text));
426:                        }
427:                        if (command.equals("getinfo")) {
428:                            good = true;
429:                            String key = (String) (vars.get("key"));
430:                            if (key == null) {
431:                                throw new PjScriptException(
432:                                        "Key was not specified.", lineNumber,
433:                                        source, 3);
434:                            }
435:                            // get the Info dictionary
436:                            PjReference infoRef;
437:                            try {
438:                                infoRef = pdf.getInfoDictionary();
439:                            } catch (InvalidPdfObjectException e) {
440:                                throw new PjScriptException("PDF error: "
441:                                        + e.getMessage(), lineNumber, source, 3);
442:                            }
443:                            // in case nothing is found, set text to ""
444:                            vars.put("text", "");
445:                            // retrieve the value
446:                            PjInfo info;
447:                            if (infoRef != null) {
448:                                PjDictionary d;
449:                                try {
450:                                    d = (PjDictionary) (pdf.getObject(infoRef
451:                                            .getObjNumber().getInt()));
452:                                } catch (ClassCastException e) {
453:                                    throw new PjScriptException(
454:                                            "PDF error: Info object is not a dictionary.",
455:                                            lineNumber, source, 3);
456:                                }
457:                                info = new PjInfo(d.getHashtable());
458:                                PjString str;
459:                                try {
460:                                    str = (PjString) (info.getHashtable()
461:                                            .get(new PjName(key)));
462:                                } catch (ClassCastException e) {
463:                                    throw new PjScriptException(
464:                                            "PDF error: Field in info object is not a string.",
465:                                            lineNumber, source, 3);
466:                                }
467:                                if (str != null) {
468:                                    vars.put("text", str.getString());
469:                                }
470:                            }
471:                        }
472:                        if (command.equals("nextxy")) {
473:                            good = true;
474:                            vars.put("x", vars.get("xinit"));
475:                            float y = new Float((String) (vars.get("y")))
476:                                    .floatValue()
477:                                    - new Float((String) (vars.get("fontsize")))
478:                                            .floatValue();
479:                            if (y < new Float((String) (vars.get("ylimit")))
480:                                    .floatValue()) {
481:                                int page = Integer.parseInt((String) (vars
482:                                        .get("page")));
483:                                int pageCount;
484:                                try {
485:                                    pageCount = pdf.getPageCount();
486:                                } catch (InvalidPdfObjectException e) {
487:                                    throw new PjScriptException("PDF error: "
488:                                            + e.getMessage(), lineNumber,
489:                                            source, 3);
490:                                }
491:                                if (page == pageCount) {
492:                                    int pageNumber;
493:                                    try {
494:                                        pageNumber = appendPage(pdf, texts,
495:                                                vars);
496:                                    } catch (PjException e) {
497:                                        throw new PjScriptException(
498:                                                "PDF error: " + e.getMessage(),
499:                                                lineNumber, source, 3);
500:                                    }
501:                                    // set the media box
502:                                    String mediaBox = (String) (vars
503:                                            .get("mediabox"));
504:                                    if (mediaBox == null) {
505:                                        throw new PjScriptException(
506:                                                "Media box was not specified.",
507:                                                lineNumber, source, 3);
508:                                    }
509:                                    float yinit = setMediaBox(pdf, pageNumber,
510:                                            mediaBox, source, lineNumber);
511:                                    vars.put("yinit", new PjNumber(yinit)
512:                                            .toString());
513:                                } else {
514:                                    vars.put("page", new Integer(page + 1)
515:                                            .toString());
516:                                }
517:                                vars.put("y", vars.get("yinit"));
518:                            } else {
519:                                vars.put("y", new PjNumber(y).toString());
520:                            }
521:                        }
522:                        if (command.equals("drawtext")) {
523:                            good = true;
524:                            String text = (String) (vars.get("text"));
525:                            if (text == null) {
526:                                throw new PjScriptException(
527:                                        "Text was not specified.", lineNumber,
528:                                        source, 3);
529:                            }
530:                            String page = (String) (vars.get("page"));
531:                            String x = (String) (vars.get("x"));
532:                            String y = (String) (vars.get("y"));
533:                            String font = (String) (vars.get("font"));
534:                            String fontsize = (String) (vars.get("fontsize"));
535:                            int pageNumber = Integer.parseInt(page);
536:                            int pageCount;
537:                            try {
538:                                pageCount = pdf.getPageCount();
539:                            } catch (InvalidPdfObjectException e) {
540:                                throw new PjScriptException("PDF error: "
541:                                        + e.getMessage(), lineNumber, source, 3);
542:                            }
543:                            if ((pageNumber < 1) || (pageNumber > pageCount)) {
544:                                throw new PjScriptException(
545:                                        "Page number out of range.",
546:                                        lineNumber, source, 3);
547:                            }
548:                            StringBuffer sb = (StringBuffer) (texts
549:                                    .elementAt(pageNumber));
550:                            if (sb == null) {
551:                                sb = new StringBuffer();
552:                                texts.setElementAt(sb, pageNumber);
553:                            }
554:                            sb.append("BT\n/Pj" + font + " " + fontsize
555:                                    + " Tf\n" + x + " " + y + " Td\n"
556:                                    + "0 Tc\n"
557:                                    +
558:                                    // kag - force black
559:                                    "/DeviceGray cs\n0 sc\n" + "(" + text
560:                                    + ") Tj\nET\n");
561:                            if (fonts.get(font) == null) {
562:                                fonts.put(font, font);
563:                            }
564:                        }
565:                        if (command.equals("drawline")) {
566:                            good = true;
567:                            String page = (String) (vars.get("page"));
568:                            String x0 = (String) (vars.get("x0"));
569:                            String y0 = (String) (vars.get("y0"));
570:                            String x1 = (String) (vars.get("x1"));
571:                            String y1 = (String) (vars.get("y1"));
572:                            String linewidth = (String) (vars.get("linewidth"));
573:                            int pageNumber = Integer.parseInt(page);
574:                            int pageCount;
575:                            try {
576:                                pageCount = pdf.getPageCount();
577:                            } catch (InvalidPdfObjectException e) {
578:                                throw new PjScriptException("PDF error: "
579:                                        + e.getMessage(), lineNumber, source, 3);
580:                            }
581:                            if ((pageNumber < 1) || (pageNumber > pageCount)) {
582:                                throw new PjScriptException(
583:                                        "Page number out of range.",
584:                                        lineNumber, source, 3);
585:                            }
586:                            StringBuffer sb = (StringBuffer) (texts
587:                                    .elementAt(pageNumber));
588:                            if (sb == null) {
589:                                sb = new StringBuffer();
590:                                texts.setElementAt(sb, pageNumber);
591:                            }
592:                            sb.append(linewidth + " w\n" + x0 + ' ' + y0
593:                                    + " m\n" + x1 + ' ' + y1 + " l\nS\n");
594:                        }
595:                        if (good == false) {
596:                            throw new PjScriptException("Unknown command.",
597:                                    lineNumber, source, 2);
598:                        }
599:                    }
600:                } while (line != null);
601:                try {
602:                    br.close();
603:                } catch (IOException e) {
604:                }
605:                return pdf;
606:            }
607:
608:            private static void drawText(Pdf pdf, String source,
609:                    int lineNumber, Hashtable fonts, Vector texts)
610:                    throws PjScriptException {
611:                int pageCount;
612:                try {
613:                    pageCount = pdf.getPageCount();
614:                } catch (InvalidPdfObjectException e) {
615:                    throw new PjScriptException("PDF error: " + e.getMessage(),
616:                            lineNumber, source, 3);
617:                }
618:                for (int x = 1; x <= pageCount; x++) {
619:                    StringBuffer sb = (StringBuffer) (texts.elementAt(x));
620:                    if (sb != null) {
621:
622:                        PjPage page;
623:                        PjResources resources;
624:                        try {
625:                            page = (PjPage) (pdf.getObject(pdf.getPage(x)));
626:                            // create a resources dictionary
627:                            resources = (PjResources) (pdf.resolve(page
628:                                    .getResources()));
629:                            if (resources == null) {
630:                                Vector v = new Vector();
631:                                v.addElement(PjName.PDF);
632:                                v.addElement(PjName.TEXT);
633:                                PjProcSet procSet = new PjProcSet(v);
634:                                resources = new PjResources();
635:                                resources.setProcSet(procSet);
636:                                page.setResources(resources);
637:                            }
638:                        } catch (InvalidPdfObjectException e) {
639:                            throw new PjScriptException("PDF error: "
640:                                    + e.getMessage(), lineNumber, source, 3);
641:                        }
642:
643:                        // add Font
644:                        PjDictionary fontDictionary;
645:                        try {
646:                            fontDictionary = (PjDictionary) (pdf
647:                                    .resolve(resources.getFont()));
648:                        } catch (InvalidPdfObjectException e) {
649:                            throw new PjScriptException("PDF error: "
650:                                    + e.getMessage(), lineNumber, source, 3);
651:                        }
652:                        if (fontDictionary == null) {
653:                            fontDictionary = new PjDictionary();
654:                            resources.setFont(fontDictionary);
655:                        }
656:                        Hashtable fontResHt = fontDictionary.getHashtable();
657:                        // create font objects
658:                        // I know this is not efficient, but we're not
659:                        // talking about very much data
660:                        for (Enumeration m = fonts.keys(); m.hasMoreElements();) {
661:                            String name = (String) (m.nextElement());
662:                            PjFontType1 font = new PjFontType1();
663:                            font.setBaseFont(new PjName(name));
664:                            font.setEncoding(new PjName("WinAnsiEncoding"));
665:                            int fontId = pdf.registerObject(font);
666:                            fontResHt.put(new PjName("Pj" + name),
667:                                    new PjReference(new PjNumber(fontId)));
668:                        }
669:                        int resourcesId = pdf.registerObject(resources);
670:
671:                        // add text
672:                        byte[] data = sb.toString().getBytes();
673:                        try {
674:                            PjStream stream = new PjStream(data)
675:                                    .flateCompress();
676:                            int streamId = pdf.registerObject(stream);
677:                            pdf.addToPage(page, streamId);
678:                        } catch (InvalidPdfObjectException e) {
679:                            throw new PjScriptException("PDF error: "
680:                                    + e.getMessage(), lineNumber, source, 3);
681:                        }
682:                    }
683:                }
684:            }
685:
686:            private static String getEndLine(String line) {
687:                int x = 0;
688:                int length = line.length();
689:                // move past command
690:                while ((x < length)
691:                        && (Character.isWhitespace(line.charAt(x)) == false)) {
692:                    x++;
693:                }
694:                // move past whitespace
695:                while ((x < length)
696:                        && (Character.isWhitespace(line.charAt(x)) == true)) {
697:                    x++;
698:                }
699:                return line.substring(x, length).trim();
700:            }
701:
702:            private static boolean quoted(String s) {
703:                int length = s.length();
704:                if (length < 2) {
705:                    return false;
706:                }
707:                return ((s.charAt(0) == '\"') && (s.charAt(length - 1) == '\"'));
708:            }
709:
710:            // returns page number of new page
711:            private static int appendPage(Pdf pdf, Vector texts, Hashtable vars)
712:                    throws PjException {
713:                // create a font object
714:                PjFontType1 font = new PjFontType1();
715:                font.setBaseFont(new PjName("Helvetica-Bold"));
716:                font.setEncoding(new PjName("WinAnsiEncoding"));
717:
718:                // create a resources dictionary
719:                PjResources resources = new PjResources();
720:                // add ProcSet
721:                Vector procsetVector = new Vector();
722:                procsetVector.addElement(new PjName("PDF"));
723:                procsetVector.addElement(new PjName("Text"));
724:                resources.setProcSet(new PjProcSet(procsetVector));
725:                int resourcesId = pdf.registerObject(resources);
726:
727:                // create a new page
728:                PjPage page = new PjPage();
729:                page.setResources(new PjReference(new PjNumber(resourcesId),
730:                        PjNumber.ZERO));
731:                int pageId = pdf.registerObject(page);
732:
733:                pdf.appendPage(pageId);
734:                int pageCount;
735:                pageCount = pdf.getPageCount();
736:                texts.setSize(pageCount + 1);
737:                vars.put("page", new Integer(pageCount).toString());
738:                return pageCount;
739:            }
740:
741:            // returns starting y value to be used as yinit
742:            private static float setMediaBox(Pdf pdf, int pageNumber,
743:                    String mediaBox, String source, int lineNumber)
744:                    throws PjScriptException {
745:                int pageId;
746:                try {
747:                    pageId = pdf.getPage(pageNumber);
748:                } catch (IndexOutOfBoundsException a) {
749:                    // pageNumber is assumed to be in bounds
750:                    return -1;
751:                } catch (InvalidPdfObjectException b) {
752:                    throw new PjScriptException("PDF error: " + b.getMessage(),
753:                            lineNumber, source, 3);
754:                }
755:                PjPage page = (PjPage) (pdf.getObject(pageId));
756:                PjRectangle rect = getMediaBoxArray(mediaBox);
757:                page.setMediaBox(rect);
758:                return ((PjNumber) (rect.getVector().lastElement())).getFloat() - 72;
759:            }
760:
761:            private static PjRectangle getMediaBoxArray(String mediaBox) {
762:                Vector v = new Vector();
763:                if (mediaBox.equalsIgnoreCase("legal-portrait")) {
764:                    v.addElement(PjNumber.ZERO);
765:                    v.addElement(PjNumber.ZERO);
766:                    v.addElement(new PjNumber(612));
767:                    v.addElement(new PjNumber(1008));
768:                    return new PjRectangle(v);
769:                }
770:                if (mediaBox.equalsIgnoreCase("legal-landscape")) {
771:                    v.addElement(PjNumber.ZERO);
772:                    v.addElement(PjNumber.ZERO);
773:                    v.addElement(new PjNumber(1008));
774:                    v.addElement(new PjNumber(612));
775:                    return new PjRectangle(v);
776:                }
777:                if (mediaBox.equalsIgnoreCase("letter-landscape")) {
778:                    v.addElement(PjNumber.ZERO);
779:                    v.addElement(PjNumber.ZERO);
780:                    v.addElement(new PjNumber(792));
781:                    v.addElement(new PjNumber(612));
782:                    return new PjRectangle(v);
783:                }
784:                // default to letter-portrait
785:                v.addElement(PjNumber.ZERO);
786:                v.addElement(PjNumber.ZERO);
787:                v.addElement(new PjNumber(612));
788:                v.addElement(new PjNumber(792));
789:                return new PjRectangle(v);
790:            }
791:
792:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.