LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  reading PDF

    Employee
    Posted 12-20-2016 06:01

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: jegnj

    CAN Anyone help with this? It runs okay and returns no text, just nulls

    package {{^JavaPackage^}};
    import java.util.List;
    import java.io.File;
    import java.io.IOException;
    import org.apache.pdfbox.exceptions.*;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.pdmodel.PDPage;
    import org.apache.pdfbox.util.PDFTextStripper;
    import org.apache.pdfbox.cos.COSDocument;
    import org.apache.pdfbox.pdfparser.PDFParser;
    import org.apache.pdfbox.pdmodel.PDDocument;


    import com.lavastorm.brain.node.SimplifiedNode;
    import com.lavastorm.brain.node.NodeFailedException;
    import com.lavastorm.io.data.Record;
    import com.lavastorm.io.data.RecordMetadata;
    import com.lavastorm.io.data.RecordOutput;
    import com.lavastorm.io.data.SimpleFieldMetadata;
    import com.lavastorm.logging.Logger;
    import com.lavastorm.logging.LogLevel;
    import com.lavastorm.util.Properties;
    import com.lavastorm.util.PropertyException;
    import static com.lavastorm.brain.node.ErrorCodes.*;

    public class {{^JavaClass^}} extends SimplifiedNode
    {
    private String m_outputFieldName;
    PDFTextStripper pdfStripper = null;
    PDDocument sourceDocument = null;
    COSDocument cosDoc = null;
    PDDocument document = null;

    @Override
    public void setup() throws NodeFailedException
    {

    setupOutput();

    }


    @Override
    public void processAll() throws NodeFailedException
    {
    try{
    m_outputFieldName = properties().getString( propertyBase() + ".outputFieldName");
    String text=null;
    processDocuments(text);
    //String fileName="/space/brain/data/RUNAREAS/Joanne_temp/junk.pdf".replaceAll("/", "\\\\/");

    writeRecord(text);

    cleanup();
    }
    catch (PropertyException ex)
    {
    logger().error(ex, Logger.CHAIN_END, "Error reading node properties.");
    throw fail();
    }

    }

    public String processDocuments(String text) throws NodeFailedException {
    String fileName="/space/brain/data/RUNAREAS/Joanne_temp/junk.pdf".replaceAll("/", "\\\\/");
    //String fileName="/space/brain/data/RUNAREAS/Joanne_temp/junk.pdf";
    File file =new File(fileName);


    try{
    PDDocument document = PDDocument.load(file);

    PDFTextStripper pdfStripper = new PDFTextStripper();
    text = pdfStripper.getText(document);


    }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return String.format("%s", text);
    }
    public void cleanup() throws NodeFailedException
    {

    if ( ! failed() ) {
    cleanupIo();
    // complete any necessary node logic
    }

    }



    public String propertyBase() {
    return "ls.brain.node";
    }

    private void setupOutput() throws NodeFailedException {
    //Check that the output is correct.
    if (numOutputs() != 1) {
    logger().error("SumExample node is only able to operate with 1 output. "+numOutputs()+" found.");
    throw fail();
    }

    RecordMetadata metadata = output(0).newMetadata();
    metadata.add(new SimpleFieldMetadata("Output state", java.lang.String.class));
    output(0).metadata(metadata);
    openOutput(0);
    }


    private void writeRecord(String text) throws NodeFailedException {
    Record record = output(0).metadata().newRecord();

    //String output = text;

    String output = "Nothing should run. Node should fail";
    if(text != null && !text.isEmpty())
    record.field(0, text);

    write(0, record);
    }
    }