4 Replies Latest reply on Aug 5, 2010 3:15 AM by sebekk23

    How to validate PDF document with JAVA

    sebekk23

      Hi,

      Is there any way to write JAVA app that will validate PDFs through/with LC API ? I was traying with code:

       

      ...

      ServiceClientFactory myFactory = ServiceClientFactory.createInstance(connectionProps);
      FormsServiceClient formsClient = new FormsServiceClient(myFactory);
            
      FormsResult renderedForm = renderPDF(formName, xmlData, url, formsClient);        
                 
      RenderOptionsSpec processSpec = new RenderOptionsSpec();
      //processSpec.setValidationReporting(5);
      processSpec.setLocale("pl_PL");
      FormsResult formOut = formsClient.processFormSubmission(renderedForm.getOutputContent(),"CONTENT_TYPE=applicati on/pdf","",processSpec);

      System.out.println(formOut.getValidationErrorsList().toString());

      ...

      //Rendering method

      private static FormsResult renderPDF(String formName,String  xmlData, String url, FormsServiceClient formsClient) throws RenderFormException, DSCException, UnsupportedEncodingException {
             
               PDFFormRenderSpec pdfFormRenderSpec = new PDFFormRenderSpec();
               pdfFormRenderSpec.setCacheEnabled(new Boolean(false));
               pdfFormRenderSpec.setXMLData(true);
               pdfFormRenderSpec.setStandAlone(true);
               pdfFormRenderSpec.setLinearizedPDF(true);
                       
               URLSpec urlSpec = new URLSpec();
               urlSpec.setContentRootURI(url);

       

               String credentialAlias = CREDENTIAL_ALIAS;
               String credentialPassword = CREDENTIAL_PASSWORD;
             
               ReaderExtensionSpec reOptions = new ReaderExtensionSpec();
               reOptions.setReCredentialAlias(credentialAlias);
               reOptions.setReCredentialPassword(credentialPassword);
               reOptions.setReExpImp(true);
               reOptions.setReFillIn(true);
               reOptions.setReDigSig(true);

       

               Document inputData = new Document(xmlData.getBytes("UTF-8"));
               /*return renderedForm = formsClient.renderPDFForm(formName,inputData,pdfFormRenderSpec,urlSpec,null);*/
               return formsClient.renderPDFFormWithUsageRights(formName,inputData,pdfFormRenderSpec,reOptions,u rlSpec);
          }

       

      but in formOut.getValidationErrorsList() i still get:

       

      [8/2/10 12:03:46:728 GMT+01:00] 0000002c SystemOut     O --------------------Validation------------
      [8/2/10 12:03:46:728 GMT+01:00] 0000002c SystemOut     O <document state="active" senderVersion="3" persistent="false" senderPersistent="false" passivated="false" senderPassivated="false" deserialized="true" senderHostId="127.0.0.1/172.16.133.24/172.16.134.24" callbackId="0" senderCallbackId="0" callbackRef="null" isLocalizable="false" isTransactionBound="false" defaultDisposalTimeout="600" disposalTimeout="600" maxInlineSize="65536" defaultMaxInlineSize="65536" inlineSize="0" contentType="null" length="-1"><cacheId/><localBackendId/><globalBackendId/><senderLocalBackendId/><senderGl obalBackendId/><inline></inline><senderPullServantJndiName>adobe/idp/DocumentPullServant/a dobews_anathNode01Cell_anathNode02_server1</senderPullServantJndiName><attributes/></docum ent>

       

      without any errors. I was looking around (google,forum and Adobe Develop Connection) but no luck . Is it posible to make it that way ?

       

      Ps.

      It's my first post so (if i did something wrong ) be gentle. And ... sorry for my poor english .

        • 1. Re: How to validate PDF document with JAVA
          Steve L Walker Level 5

          Is it possible there are no errors to report?

           

          Steve

          • 2. Re: How to validate PDF document with JAVA
            sebekk23 Level 1

            I test it on wrong PDF too (Acrobat Reader declare errors) and stiil the same

            • 3. Re: How to validate PDF document with JAVA
              sebekk23 Level 1

              I modified code for rendering form and for validating and I start to get erros on output, but that is not complete list

               

              <document state="active" senderVersion="3" persistent="false" senderPersistent="false" passivated="false" senderPassivated="false"
              deserialized="true" senderHostId="127.0.0.1/172.16.133.24/172.16.134.24" callbackId="0" senderCallbackId="0" callbackRef="null"
              isLocalizable="false" isTransactionBound="false" defaultDisposalTimeout="600" disposalTimeout="600" maxInlineSize="65536"
              defaultMaxInlineSize="65536" inlineSize="342" contentType="text/xml" length="-1">
              <cacheId/>
              <localBackendId/>
              <globalBackendId/>
              <senderLocalBackendId/>
              <senderGlobalBackendId/>
              <inline>
                  <?xml version="1.0" encoding="UTF-8"?>
                  <validationErrors>
                  <m d="2010-08-04T10:05:48.897+02:00" ref="...
              </inline>
              <senderPullServantJndiName>adobe/idp/DocumentPullServant/adobews_anathNode01Cell_anathNode 02_server1</senderPullServantJndiName>
              <attributes/>
              </document>

               

              it is cut in place where should be reference to wrong field.

               

              <inline>
                   <?xml version="1.0" encoding="UTF-8"?>
                   <validationErrors>
                   <m d="2010-08-04T10:05:48.897+02:00" ref="...
              </inline>

               

              I attach whole class after changes:

               

              public class AdobeForm {
                  private static final String CREDENTIAL_ALIAS  = "READER_EXC_CERT";
                  private static final String IIOP = "iiop://localhost:2810";
                  private static final String CREDENTIAL_PASSWORD = "pass";
                  private static final String AS_USERNAME = "log";
                  private static final String AS_PASSWORD = "pass";
                  private static final String NO_ERRORS = "";
                  public static byte[] renderForm(String formName,String  xmlData, String url){
                      byte[] data = null;
                      try
                      {
                          //JBOSS
              /*            Properties connectionProps = new Properties();
                          connectionProps.setProperty("DSC_DEFAULT_EJB_ENDPOINT", "jnp://localhost:1099");
                          connectionProps.setProperty("DSC_TRANSPORT_PROTOCOL", "EJB");
                          connectionProps.setProperty("DSC_SERVER_TYPE", "JBoss");
                          connectionProps.setProperty("DSC_CREDENTIAL_USERNAME", "log");
                          connectionProps.setProperty("DSC_CREDENTIAL_PASSWORD", "pass");*/

               

                         // dla WebSphere
                         
                          Properties connectionProps = new Properties();
                          connectionProps.setProperty("DSC_DEFAULT_EJB_ENDPOINT", IIOP);
                          connectionProps.setProperty("DSC_TRANSPORT_PROTOCOL","EJB");         
                          connectionProps.setProperty("DSC_SERVER_TYPE", "WebSphere");
                          connectionProps.setProperty("DSC_CREDENTIAL_USERNAME", AS_USERNAME);
                          connectionProps.setProperty("DSC_CREDENTIAL_PASSWORD", AS_PASSWORD);
                         

               

                          ServiceClientFactory myFactory = ServiceClientFactory.createInstance(connectionProps);
                          FormsServiceClient formsClient = new FormsServiceClient(myFactory);

               

                          FormsResult renderedForm = renderPDF(formName, xmlData, url, formsClient, false);
                         
                          //test(renderedForm);
                         
                          Document renderedFormData = renderedForm.getOutputContent();
                          InputStream inputStream = renderedFormData.getInputStream();
                         
                          // to pewnie trzeba poprawic na przepisywanie duzych danych
                          int size = inputStream.available();
                          data = new byte[size];
                          inputStream.read(data);
                          inputStream.close();
                      }
                      catch (Exception e)
                      {
                          e.printStackTrace();
                      }
                      return data;

               

                  }

               

                  public static byte[] validateForm(String formName, String xmlData, String url){
                      byte[] data = null;
                      try
                      {
                          //JBOSS
                          /*
                          Properties connectionProps = new Properties();
                          connectionProps.setProperty("DSC_DEFAULT_EJB_ENDPOINT", "jnp://localhost:1099");
                          connectionProps.setProperty("DSC_TRANSPORT_PROTOCOL", "EJB");
                          connectionProps.setProperty("DSC_SERVER_TYPE", "JBoss");
                          connectionProps.setProperty("DSC_CREDENTIAL_USERNAME", "administrator");
                          connectionProps.setProperty("DSC_CREDENTIAL_PASSWORD", "password");*/

               

                         
                          // dla WebSphere
                          Properties connectionProps = new Properties();
                          connectionProps.setProperty("DSC_DEFAULT_EJB_ENDPOINT", IIOP);
                          connectionProps.setProperty("DSC_TRANSPORT_PROTOCOL","EJB");         
                          connectionProps.setProperty("DSC_SERVER_TYPE", "WebSphere");
                          connectionProps.setProperty("DSC_CREDENTIAL_USERNAME", AS_USERNAME);
                          connectionProps.setProperty("DSC_CREDENTIAL_PASSWORD", AS_PASSWORD);
                         
                         
                          ServiceClientFactory myFactory = ServiceClientFactory.createInstance(connectionProps);
                          FormsServiceClient formsClient = new FormsServiceClient(myFactory);
                         
                          FormsResult renderedForm = renderPDF(formName, xmlData, url, formsClient, true);        
                         
                          RenderOptionsSpec processSpec = new RenderOptionsSpec();
                          processSpec.setValidationUI(0);
                          processSpec.setValidationReporting(10);
                          processSpec.setCacheEnabled(true);
                          //processSpec.setXMLData(true);
                          //processSpec.setDebugEnabled(true);
                          /*processSpec.setLocale("pl_PL");
                         
                          processSpec.setStandAlone(true);*/
                         
                          FormsResult formOut = formsClient.processFormSubmission(renderedForm.getXMLData(),
                                  "I=application/pdf&CONTENT_TYPE=application/vnd.adobe.xdp+xml",
                                  "",processSpec);
              /*            Document inputData = new Document(xmlData.getBytes("UTF-8"));
                          FormsResult formOut = formsClient.processFormSubmission(inputData,
                                  "CONTENT_TYPE=application/pdf&CONTENT_TYPE=application/vnd.adobe.xdp+xml&CONTENT_TYPE=tex t/xml",
                                  "",processSpec);*/
                          test(formOut);
                          // to pewnie trzeba poprawic na przepisywanie duzych danych
                          InputStream inputStream = formOut.getOutputContent().getInputStream();
                          int size = inputStream.available();
                          data = new byte[size];
                          inputStream.read(data);
                          inputStream.close();

               

                          System.out.println("Dane: "+(new String(data)));
                          System.out.println("------------------------------------------");
                          System.out.println("OutputXML: "+formOut.getOutputXML());
                         
                          return NO_ERRORS.getBytes();
                      }
                      catch (Exception e)
                      {
                          e.printStackTrace();
                      }
                      return NO_ERRORS.getBytes();
                  }
                 
                  private static void test(FormsResult formOut) {
                      if(formOut != null)
                      {
                          System.out.println("------------------------------------------");
                          System.out.println("--------------------Validation------------");
                          System.out.println(formOut.getValidationErrorsList());
                          System.out.println("------------------------------------------");
                          System.out.println("------------------------------------------");   
                      }
                      else
                          System.out.println("-------------------FORMOUT NULL-----------------------");   
                  }
                 
                  @SuppressWarnings("unused")
                  private static void showData(String xmlData, String formName, String url) {
                      System.out.println("------------------XML------------------------");
                      System.out.println("XML: "+xmlData);
                      System.out.println("Form: "+formName);
                      System.out.println("URL: "+url);
                      System.out.println("------------------XML------------------------");
                  }
                 
                  private static FormsResult renderPDF(String formName,String  xmlData, String url, FormsServiceClient formsClient, boolean renderedForm) throws RenderFormException, DSCException, UnsupportedEncodingException {
                     
                       URLSpec urlSpec = new URLSpec();
                       urlSpec.setContentRootURI(url);
                     
                       PDFFormRenderSpec pdfFormRenderSpec = new PDFFormRenderSpec();
                       pdfFormRenderSpec.setCacheEnabled(true);
                       pdfFormRenderSpec.setXMLData(true);
              /*       pdfFormRenderSpec.setDebugEnabled(true);
                       pdfFormRenderSpec.setLocale("pl_PL");
                       //without rights
                       if(renderedForm)//see bellow
                           pdfFormRenderSpec.setLinearizedPDF(true); */

               

                       System.out.println("RenderServlet formName "+formName);  

               

                       String credentialAlias = CREDENTIAL_ALIAS;
                       String credentialPassword = CREDENTIAL_PASSWORD;
                     

               


                       Document inputData = new Document(xmlData.getBytes("UTF-8"));
                       if(renderedForm)
                           return formsClient.renderPDFForm(formName,inputData,pdfFormRenderSpec,urlSpec,null);
                       else
                       {
                           ReaderExtensionSpec reOptions = new ReaderExtensionSpec();
                           reOptions.setReCredentialAlias(credentialAlias);
                           reOptions.setReCredentialPassword(credentialPassword);
                           reOptions.setReExpImp(true);
                           reOptions.setReFillIn(true);
                           reOptions.setReDigSig(true);
                           pdfFormRenderSpec.setStandAlone(true);

               

                           return formsClient.renderPDFFormWithUsageRights(formName,inputData,pdfFormRenderSpec,reOptions,u rlSpec);
                       }
                       //[8/4/10 11:10:55:928 GMT+01:00] 0000002a CommonGibsonU W com.adobe.livecycle.formsservice.logging.FormsLogger logMessage ALC-FRM-001-048: Forms with Rights cannot be linearized.
                  }
              }

              • 4. Re: How to validate PDF document with JAVA
                sebekk23 Level 1

                And BTW, maybe someone knows example with this kind of validation.