-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
158 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
writers/src/main/scala/edu/psu/sagnik/research/pdwriters/impl/CreateResultsBatchFiles.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package edu.psu.sagnik.research.pdwriters.impl | ||
|
||
import java.io.{ BufferedWriter, File, FileWriter } | ||
|
||
import edu.psu.sagnik.research.pdsimplify.impl.ProcessDocument | ||
import edu.psu.sagnik.research.pdsimplify.model.PDDocumentSimple | ||
import edu.psu.sagnik.research.pdwriters.writers.json.CreateTextLinesJSON | ||
import org.apache.pdfbox.pdmodel.PDDocument | ||
|
||
/** | ||
* Created by sagnik on 7/7/17. | ||
*/ | ||
|
||
object CreateResultsBatchFiles { | ||
|
||
def writeTextLinesJson(fileName: String, simplifiedDocument: PDDocumentSimple) = { | ||
val file = new File(fileName) | ||
val bw = new BufferedWriter(new FileWriter(file)) | ||
bw.write(CreateTextLinesJSON(simplifiedDocument)) | ||
println(s"JSON file written at: ${fileName}") | ||
bw.close() | ||
} | ||
|
||
import scala.util.matching.Regex | ||
def recursiveListFiles(f: File, r: Regex): Array[File] = { | ||
val these = f.listFiles | ||
val good = these.filter(f => r.findFirstIn(f.getName).isDefined) | ||
good ++ these.filter(_.isDirectory).flatMap(recursiveListFiles(_, r)) | ||
} | ||
|
||
/* | ||
def main(args: Array[String]): Unit = { | ||
val DEFAULT_DIR = "/media/sagnik/OS_Install/data/pdfsamples/nuance/nuancepdf/" | ||
//"/home/szr163/Downloads/f706gsd1.pdf" | ||
//"/Users/schoudhury/hassan/C10-2042.pdf" | ||
val pdDir = if (args.length > 1) args(0) else DEFAULT_DIR | ||
val docs = recursiveListFiles(new File(pdDir), "pdf".r).map(_.getAbsolutePath) | ||
docs.foreach { x => | ||
val document = PDDocument.load(new File(x)) | ||
val jsonLoc = s"${x.substring(0, x.length - 3)}json" | ||
val simplifiedDocument = ProcessDocument(document) | ||
writeTextLinesJson(jsonLoc, simplifiedDocument) | ||
document.close() | ||
} | ||
} | ||
*/ | ||
|
||
def main(args: Array[String]): Unit = { | ||
val DEFAULT_DIR = "/media/sagnik/OS_Install/data/pdfsamples/nuance/nuancepdf/" | ||
//"/home/szr163/Downloads/f706gsd1.pdf" | ||
//"/Users/schoudhury/hassan/C10-2042.pdf" | ||
val pdDir = if (args.length > 1) args(0) else DEFAULT_DIR | ||
|
||
val docs = recursiveListFiles(new File(pdDir), "pdf".r).map(_.getAbsolutePath) | ||
docs.foreach { x => | ||
var document = PDDocument.load(new File(x)) | ||
val simplifiedDocument = ProcessDocument(document) | ||
(0 until simplifiedDocument.pages.size).foreach(pageNum => | ||
document = CreateResultsBatch.createMarkedResult( | ||
document, | ||
simplifiedDocument, | ||
pageNum = pageNum, | ||
pdLoc = x | ||
)) | ||
document.save(s"${x.dropRight(4)}-marked.pdf") | ||
println(s"[marked document written to]: ${x.dropRight(4)}-marked.pdf") | ||
document.close() | ||
document.close() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,7 @@ object CreateJSON { | |
("content" -> pp.content) | ||
) | ||
}) | ||
|
||
) | ||
} | ||
|
||
|
64 changes: 64 additions & 0 deletions
64
...s/src/main/scala/edu/psu/sagnik/research/pdwriters/writers/json/CreateTextLinesJSON.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package edu.psu.sagnik.research.pdwriters.writers.json | ||
|
||
import java.awt.geom.Point2D | ||
|
||
import edu.psu.sagnik.research.data.RectangleOTL | ||
import edu.psu.sagnik.research.pdsimplify.model.PDDocumentSimple | ||
import edu.psu.sagnik.research.pdsimplify.path.model.PDLine | ||
import org.json4s.JsonDSL._ | ||
import org.json4s.JsonDSL._ | ||
import org.json4s.jackson.JsonMethods._ | ||
|
||
/** | ||
* Created by schoudhury on 7/6/16. | ||
*/ | ||
object CreateTextLinesJSON { | ||
|
||
def precReduce(d: Float): Float = BigDecimal(d).setScale(2, BigDecimal.RoundingMode.HALF_UP).toFloat | ||
|
||
def precReduce(point: Point2D.Float): List[Float] = List(precReduce(point.x), precReduce(point.y)) | ||
|
||
def precReduce(r: RectangleOTL): List[Float] = List(precReduce(r.xTopLeft), precReduce(r.yTopLeft), precReduce(r.widthRight), precReduce(r.heightDown)) | ||
|
||
case class PathStyle( | ||
fill: Option[String], | ||
fillRule: Option[String], | ||
fillOpacity: Option[String], | ||
stroke: Option[String], | ||
strokeWidth: Option[String], | ||
strokeLineCap: Option[String], | ||
strokeLineJoin: Option[String], | ||
strokeMiterLimit: Option[String], | ||
strokeDashArray: Option[String], | ||
strokeDashOffset: Option[String], | ||
strokeOpacity: Option[String] | ||
) | ||
|
||
def apply(pDS: PDDocumentSimple): String = { | ||
val jsonContent = | ||
"pages" -> | ||
pDS.pages.map { | ||
p => | ||
( | ||
("pageNumber" -> p.pNum) ~ | ||
("pageBB" -> precReduce(p.bb)) ~ | ||
("textparagraphs" -> p.paragraphs.map { | ||
pp => | ||
( | ||
("bb" -> precReduce(pp.bb)) ~ | ||
("textlines" -> pp.tLines.map { | ||
tl => | ||
( | ||
("bb" -> precReduce(tl.bb)) ~ | ||
("content" -> tl.content) | ||
) | ||
}) | ||
) | ||
}) | ||
|
||
) | ||
} | ||
pretty(render(jsonContent)) | ||
} | ||
|
||
} |
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.