I ran into this same exception last weekend. You may be getting this for a slightly different reason than I was, but I will relate my experience in case it helps you. It really stumped me for a while.
My problem was that I could build only one PDF file with JPEGs in it. If I tried to build a second document, I would have to recycle my JVM, or I'd get the exception you report. It turned out that I was caching PDJpeg's and I forgot to invalidate my cache between PDDocument's. The PDF files I was making used certain images many times and caching them brought the document size from 13MB down to 1MB, so I didn't want to give up caching. My problem was that I passed drawXObject() a PDJpeg that was associated with a different document. Of course, this would never work, but the exception that gets thrown didn't make any sense to me. Anyway I hope that helps. I'm new to this list. Hi. My name is Glen Peterson and PDFBox is working great for me - reliably building 300+ page PDFs in about 3 seconds on a single-processor 3GHz machine (the database queries take probably half of that time). I have written a few wrapper-classes for PDFBox that do line breaking, page breaking, tables, and transliterations of some Unicode characters to the default PDF character set. The transliterations should be a trivial patch to existing PDFBox code. They make PDFBox support "smart quotes," "em-dashes," and the Euro Symbol pasted from office, plus a few dozen languages encoded with Unicode (as Java does internally). I also mapped Cyrillic letters to their closest Romanizations according to ISO 9:1995, so it should be fairly complete. I have permission from my employer to contribute these extensions to PDFBox under the Apache License, copyright myself and my employer. Maybe someone can let me know how to proceed? On Fri, Feb 15, 2013 at 4:39 AM, Thomas Jodes (JIRA) <[email protected]> wrote: > > [ > https://issues.apache.org/jira/browse/PDFBOX-1518?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel > ] > > Thomas Jodes updated PDFBOX-1518: > --------------------------------- > > Description: > I switched from 1.7.1 to 1.8.0-SNAPSHOT. Following code did not work any > longer: > > @Test > public void testGenerateNewDocument() throws IOException, > COSVisitorException { > PDDocument doc = null; > try { > doc = new PDDocument(); > > PDPage page = new PDPage(); > doc.addPage(page); > PDFont font = PDType1Font.HELVETICA_BOLD; > > PDJpeg image = new PDJpeg(doc, getLogoInputStream() ); > > PDPageContentStream contentStream = new > PDPageContentStream(doc, page, true, true); > > contentStream.beginText(); > contentStream.setFont(font, 12); > contentStream.moveTextPositionByAmount(100, 700); > contentStream.drawString("Hello World!"); > contentStream.endText(); > > contentStream.drawImage(image, 20, 20); > > contentStream.close(); > > doc.save( getWorkDirectory() + File.separator + > "pdfbox-testGenerateNewDocument.pdf" ); > } finally { > if (doc != null) { > doc.close(); > } > } > } > > java.lang.ClassCastException: org.apache.pdfbox.cos.COSName cannot be cast to > java.lang.String > at > org.apache.pdfbox.pdmodel.common.COSArrayList.convertStringListToCOSNameCOSArray(COSArrayList.java:364) > at > org.apache.pdfbox.pdmodel.common.PDStream.setFilters(PDStream.java:300) > at > org.apache.pdfbox.pdmodel.edit.PDPageContentStream.<init>(PDPageContentStream.java:258) > at > org.apache.pdfbox.pdmodel.edit.PDPageContentStream.<init>(PDPageContentStream.java:159) > at > tests.evaluation.pdfbox.PdfboxEvaluationTest.testGenerateNewDocument(PdfboxEvaluationTest.java:47) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) > at > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) > at java.lang.reflect.Method.invoke(Method.java:597) > at > org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) > at org.testng.internal.Invoker.invokeMethod(Invoker.java:701) > at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:893) > at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1218) > at > org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) > at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) > at org.testng.TestRunner.privateRun(TestRunner.java:768) > at org.testng.TestRunner.run(TestRunner.java:617) > at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) > at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) > at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) > at org.testng.SuiteRunner.run(SuiteRunner.java:240) > at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) > at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87) > at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185) > at org.testng.TestNG.runSuitesLocally(TestNG.java:1110) > at org.testng.TestNG.run(TestNG.java:1022) > at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109) > at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202) > at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173) > > > was: > I switched from 1.7.1 to 1.8.0-SNAPSHOT. Following code did not work any > longer: > > @Test > public void testGenerateNewDocument() throws IOException, > COSVisitorException { > PDDocument doc = null; > try { > doc = new PDDocument(); > > PDPage page = new PDPage(); > doc.addPage(page); > PDFont font = PDType1Font.HELVETICA_BOLD; > > PDJpeg image = new PDJpeg(doc, getLogoInputStream() ); > > PDPageContentStream contentStream = new > PDPageContentStream(doc, page, true, true); > > contentStream.beginText(); > contentStream.setFont(font, 12); > contentStream.moveTextPositionByAmount(100, 700); > contentStream.drawString("Hello World!"); > contentStream.endText(); > > contentStream.drawImage(image, 20, 20); > > contentStream.close(); > > doc.save( getWorkDirectory() + File.separator + > "pdfbox-testGenerateNewDocument.pdf" ); > } finally { > if (doc != null) { > doc.close(); > } > } > } > > >> ClassCastException writing text to a page >> ----------------------------------------- >> >> Key: PDFBOX-1518 >> URL: https://issues.apache.org/jira/browse/PDFBOX-1518 >> Project: PDFBox >> Issue Type: Bug >> Components: Writing >> Affects Versions: 1.8.0 >> Environment: Windows x86_64, jdk1.6.0u31 >> Reporter: Thomas Jodes >> Priority: Critical >> >> I switched from 1.7.1 to 1.8.0-SNAPSHOT. Following code did not work any >> longer: >> @Test >> public void testGenerateNewDocument() throws IOException, >> COSVisitorException { >> PDDocument doc = null; >> try { >> doc = new PDDocument(); >> PDPage page = new PDPage(); >> doc.addPage(page); >> PDFont font = PDType1Font.HELVETICA_BOLD; >> >> PDJpeg image = new PDJpeg(doc, getLogoInputStream() ); >> PDPageContentStream contentStream = new >> PDPageContentStream(doc, page, true, true); >> contentStream.beginText(); >> contentStream.setFont(font, 12); >> contentStream.moveTextPositionByAmount(100, 700); >> contentStream.drawString("Hello World!"); >> contentStream.endText(); >> contentStream.drawImage(image, 20, 20); >> contentStream.close(); >> doc.save( getWorkDirectory() + File.separator + >> "pdfbox-testGenerateNewDocument.pdf" ); >> } finally { >> if (doc != null) { >> doc.close(); >> } >> } >> } >> java.lang.ClassCastException: org.apache.pdfbox.cos.COSName cannot be cast >> to java.lang.String >> at >> org.apache.pdfbox.pdmodel.common.COSArrayList.convertStringListToCOSNameCOSArray(COSArrayList.java:364) >> at >> org.apache.pdfbox.pdmodel.common.PDStream.setFilters(PDStream.java:300) >> at >> org.apache.pdfbox.pdmodel.edit.PDPageContentStream.<init>(PDPageContentStream.java:258) >> at >> org.apache.pdfbox.pdmodel.edit.PDPageContentStream.<init>(PDPageContentStream.java:159) >> at >> tests.evaluation.pdfbox.PdfboxEvaluationTest.testGenerateNewDocument(PdfboxEvaluationTest.java:47) >> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) >> at >> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) >> at >> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) >> at java.lang.reflect.Method.invoke(Method.java:597) >> at >> org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) >> at org.testng.internal.Invoker.invokeMethod(Invoker.java:701) >> at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:893) >> at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1218) >> at >> org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) >> at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) >> at org.testng.TestRunner.privateRun(TestRunner.java:768) >> at org.testng.TestRunner.run(TestRunner.java:617) >> at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) >> at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) >> at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) >> at org.testng.SuiteRunner.run(SuiteRunner.java:240) >> at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) >> at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87) >> at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185) >> at org.testng.TestNG.runSuitesLocally(TestNG.java:1110) >> at org.testng.TestNG.run(TestNG.java:1022) >> at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109) >> at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202) >> at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173) > > -- > This message is automatically generated by JIRA. > If you think it was sent incorrectly, please contact your JIRA administrators > For more information on JIRA, see: http://www.atlassian.com/software/jira -- Glen K. Peterson (828) 393-0081
