[ 
https://issues.apache.org/jira/browse/PDFBOX-1518?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13585468#comment-13585468
 ] 

Thomas Chojecki commented on PDFBOX-1518:
-----------------------------------------

I've got the same error with your patch. The problem is that the List that was 
passed to the convertStringListToCOSNameCOSArray(List<String>) contains 
COSNames. This happen because a non generic List was casted to a List<String> 
and the exception appears right after the first get.

You can check it by running the example:
org.apache.pdfbox.example.pdmodel.HelloWorld.java

I would prefer to let the List non generic and check the input. A quickfix 
would be something like that:

----

    public static COSArray convertStringListToCOSNameCOSArray( List strings )
    {
        COSArray retval = new COSArray();
        for( int i=0; i<strings.size(); i++ )
        {
          Object object = strings.get( i );
          if (object instanceof COSName) {
            retval.add((COSName)object);
          } else {
            retval.add( COSName.getPDFName( strings.get( i ).toString() ) );
          }
          }
        return retval;
    }

----

My stacktrace was the same but the hint is:
PDStream:148 <- here you put a COSName in a List and pass it to 
PDStream:300 <- here you try to call convertStringListToCOSNameCOSArray

                
> 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
>            Assignee: Andreas Lehmkühler
>            Priority: Critical
>             Fix For: 1.8.0
>
>
> 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

Reply via email to