<%@ jet 
imports="
    org.talend.core.model.process.ElementParameterParser
    org.talend.core.model.process.INode
    org.talend.designer.codegen.config.CodeGeneratorArgument
    java.util.ArrayList
    java.util.List
    java.util.Map
    java.net.URLEncoder
    java.io.UnsupportedEncodingException
    org.talend.core.model.metadata.IMetadataTable
    org.talend.core.model.metadata.IMetadataColumn
    org.talend.core.model.process.IConnection
    org.talend.core.model.process.IConnectionCategory
    org.talend.core.model.metadata.types.JavaTypesManager
    org.talend.core.model.metadata.types.JavaType
"
%>

<%
CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
INode node = (INode)codeGenArgument.getArgument();
String cid = node.getUniqueName();

String pdfIn = ElementParameterParser.getValue(node,"__PDF_INPUT__");
String pdfOut = ElementParameterParser.getValue(node, "__PDF_OUTPUT__");
String startPage = ElementParameterParser.getValue(node, "__PDF_PAGE_START__");
if(("").equals(startPage)){
    		startPage="0";
    	}
String endPage = ElementParameterParser.getValue(node, "__PDF_PAGE_END__");
if(("").equals(endPage)){
    		endPage="0";
    	}
String encoding = ElementParameterParser.getValue(node, "__PDF_ENCODING__");
boolean isArea = "true".equals(ElementParameterParser.getValue(node, "__PDF_BY_AREA__"));
int x = Integer.parseInt(ElementParameterParser.getValue(node, "__PDF_X__"));
int y = Integer.parseInt(ElementParameterParser.getValue(node, "__PDF_Y__"));
int width = Integer.parseInt(ElementParameterParser.getValue(node, "__PDF_WIDTH__"));
int height = Integer.parseInt(ElementParameterParser.getValue(node, "__PDF_HEIGHT__"));
%>

java.io.File file_<%=cid%> = new java.io.File(<%=pdfIn%>);

if (file_<%=cid%>.isFile()) {
	
	String text_<%=cid%> = "";
	org.apache.pdfbox.pdfparser.PDFParser parser_<%=cid%> = new org.apache.pdfbox.pdfparser.PDFParser(new java.io.FileInputStream(file_<%=cid%>));
	parser_<%=cid%>.parse();
	org.apache.pdfbox.cos.COSDocument cos_<%=cid%> = parser_<%=cid%>.getDocument();
	org.apache.pdfbox.pdmodel.PDDocument doc_<%=cid%> = new org.apache.pdfbox.pdmodel.PDDocument(cos_<%=cid%>);
	
<% if (!isArea) { %>
	
	org.apache.pdfbox.util.PDFTextStripper stripper_<%=cid%> = new org.apache.pdfbox.util.PDFTextStripper();
	 if (<%=startPage%> > 0 && <%=endPage%> > 0) {
	stripper_<%=cid%>.setStartPage(<%=startPage%>);
	stripper_<%=cid%>.setEndPage(<%=endPage%>);
	 }
	text_<%=cid%> += stripper_<%=cid%>.getText(doc_<%=cid%>);
	
<% } else { %>
		org.apache.pdfbox.util.PDFTextStripperByArea stripper_<%=cid%> = new org.apache.pdfbox.util.PDFTextStripperByArea();
	java.awt.Rectangle area_<%=cid%> = new java.awt.Rectangle(<%=x%>, <%=y%>, <%=width%>, <%=height%>);
	stripper_<%=cid%>.addRegion("content", area_<%=cid%>);
	java.util.List allPages_<%=cid%>= doc_<%=cid%>.getDocumentCatalog().getAllPages();
	
	int size_<%=cid%> = allPages_<%=cid%>.size();
	int i_<%=cid%> = -1;
	
	 if (<%=startPage%> < 0 && <%=endPage%> < 0) { 
	while(++i_<%=cid%> < size_<%=cid%>) {
		stripper_<%=cid%>.extractRegions((org.apache.pdfbox.pdmodel.PDPage)allPages_<%=cid%>.get(i_<%=cid%>));
		text_<%=cid%> += stripper_<%=cid%>.getTextForRegion("content");
	}
	 } else { 
	i_<%=cid%> = <%=startPage%> - 2;
	while(++i_<%=cid%> <= (<%=endPage%> - 1)) {
		stripper_<%=cid%>.extractRegions((org.apache.pdfbox.pdmodel.PDPage)allPages_<%=cid%>.get(i_<%=cid%>));
		text_<%=cid%> += stripper_<%=cid%>.getTextForRegion("content");
	}
	 } 

<% } %>	
	
	java.io.OutputStreamWriter writer_<%=cid%> = new java.io.OutputStreamWriter(new java.io.FileOutputStream(<%=pdfOut%>), <%=encoding%>); 
	writer_<%=cid%>.write(text_<%=cid%>);
	writer_<%=cid%>.flush();
	writer_<%=cid%>.close();
	doc_<%=cid%>.close();

}