diff --git a/redaction-service-v1/redaction-service-api-v1/src/main/java/com/iqser/red/service/redaction/v1/model/Rectangle.java b/redaction-service-v1/redaction-service-api-v1/src/main/java/com/iqser/red/service/redaction/v1/model/Rectangle.java index 0ed06644..5a58efc4 100644 --- a/redaction-service-v1/redaction-service-api-v1/src/main/java/com/iqser/red/service/redaction/v1/model/Rectangle.java +++ b/redaction-service-v1/redaction-service-api-v1/src/main/java/com/iqser/red/service/redaction/v1/model/Rectangle.java @@ -1,8 +1,5 @@ package com.iqser.red.service.redaction.v1.model; -import com.dslplatform.json.JsonAttribute; -import com.fasterxml.jackson.annotation.JsonIgnore; - import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -18,100 +15,4 @@ public class Rectangle { private int page; - - - @JsonIgnore - @JsonAttribute(ignore = true) - public float getX1(int dir, float pageWidth, float pageHeight) { - - if (dir == 90) { - return topLeft.getX(); - } - else if (dir == 180) { - return pageWidth - topLeft.getX() + width; - - } - else if (dir == 270) { - - return topLeft.getY() + height; - } - else { - return topLeft.getX(); - } - } - - - @JsonIgnore - @JsonAttribute(ignore = true) - public float getX2(int dir, float pageWidth, float pageHeight) { - - if (dir == 90) { - return topLeft.getX() + width; - } - - else if (dir == 180) { - return pageWidth - topLeft.getX(); - } - - else if (dir == 270) { - return topLeft.getY() ; - - } - - else { - return topLeft.getX() + width; - } - } - - - - - - @JsonIgnore - @JsonAttribute(ignore = true) - public float getY1(int dir, float pageWidth, float pageHeight) { - - if (dir == 90 ) { - return topLeft.getY(); - } - - else if (dir == 180) { - return pageHeight - topLeft.getY() + height; - - } - - else if (dir == 270) { - return pageHeight - topLeft.getX() ; - - } - else { - return topLeft.getY(); - } - } - - - @JsonIgnore - @JsonAttribute(ignore = true) - public float getY2(int dir, float pageWidth, float pageHeight) { - - if (dir == 90) { - return topLeft.getY() + height; - } - - else if (dir == 180 ) { - - return pageHeight - topLeft.getY(); - } - - else if (dir == 270 ) { - return pageHeight -topLeft.getX() - width; - } - - else { - return topLeft.getY() + height; - } - } - - - } diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/classification/service/BlockificationService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/classification/service/BlockificationService.java index d1416a5b..69e29762 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/classification/service/BlockificationService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/classification/service/BlockificationService.java @@ -61,7 +61,7 @@ public class BlockificationService { || isSplittedByRuling(minX, minY, word.getX1(), word.getY2(), verticalRulingLines, word.getDir().getDegrees(), cropbox.getWidth(), cropbox.getHeight()); boolean splitByDir = prev != null && !prev.getDir().equals(word.getDir()); - if (prev != null && (lineSeparation || startFromTop || xIsBeforeFirstX || splitByX || splitByDir || startFromTop || splittedByRuling)) { + if (prev != null && (lineSeparation || startFromTop || xIsBeforeFirstX || splitByX || splitByDir || splittedByRuling)) { Orientation prevOrientation = null; if (!chunkBlockList1.isEmpty()) { @@ -332,32 +332,20 @@ public class BlockificationService { if (documentFontSizeCounter.getMostPopular() != null) { if (textBlock.getMostPopularWordFontSize() >= documentFontSizeCounter.getMostPopular()) { - if (textBlock.getX1() < minX) { - minX = textBlock.getX1(); - } - if (textBlock.getX2() > maxX) { - maxX = textBlock.getX2(); - } - if (textBlock.getY1() < minY) { - minY = textBlock.getY1(); - } - if (textBlock.getY2() > maxY) { - maxY = textBlock.getY2(); - } - - } - } - } - - if (container instanceof Table) { - Table table = (Table) container; - for (List row : table.getRows()) { - for (Cell cell : row) { - - if (cell == null || cell.getTextBlocks() == null) { - continue; - } - for (TextBlock textBlock : cell.getTextBlocks()) { + if(page.getCropBox().getWidth() > page.getCropBox().getHeight() && page.getRotation() != 0){ + if (textBlock.getY1() < minX) { + minX = textBlock.getY1(); + } + if (textBlock.getY2() > maxX) { + maxX = textBlock.getY2(); + } + if (textBlock.getX1() < minY) { + minY = textBlock.getX1(); + } + if (textBlock.getX2() > maxY) { + maxY = textBlock.getX2(); + } + } else { if (textBlock.getX1() < minX) { minX = textBlock.getX1(); } @@ -374,30 +362,56 @@ public class BlockificationService { } } } + + if (container instanceof Table) { + Table table = (Table) container; + for (List row : table.getRows()) { + for (Cell cell : row) { + + if (cell == null || cell.getTextBlocks() == null) { + continue; + } + for (TextBlock textBlock : cell.getTextBlocks()) { + if(page.getCropBox().getWidth() > page.getCropBox().getHeight() && page.getRotation() != 0){ + if (textBlock.getY1() < minX) { + minX = textBlock.getMinY(); + } + if (textBlock.getY2() > maxX) { + maxX = textBlock.getY2(); + } + if (textBlock.getX1() < minY) { + minY = textBlock.getX1(); + } + if (textBlock.getX2() > maxY) { + maxY = textBlock.getX2(); + } + } else { + if (textBlock.getX1() < minX) { + minX = textBlock.getX1(); + } + if (textBlock.getX2() > maxX) { + maxX = textBlock.getX2(); + } + if (textBlock.getY1() < minY) { + minY = textBlock.getY1(); + } + if (textBlock.getY2() > maxY) { + maxY = textBlock.getY2(); + } + } + } + } + } + } } + System.out.println("Page: " + page.getPageNumber() + " Landscape: " + page.isLandscape()+ " MinX: " + minX + " MaxX: " + maxX + " MinY: " +minY + " MaxY: " + maxY); } return new Rectangle(new Point(minX, minY), maxX - minX, maxY - minY, 0); } - private void sortRotatedSequences(List sequences) { - List rotatedWords = new ArrayList<>(); - Iterator itty = sequences.iterator(); - while (itty.hasNext()) { - var pos = itty.next(); - if (pos.getTextPositions().get(0).getDir() == 270) { - rotatedWords.add(pos); - itty.remove(); - } - } - - if (!rotatedWords.isEmpty() && !sequences.isEmpty()) { - rotatedWords.sort(Comparator.comparing(TextPositionSequence::getX1)); - } - sequences.addAll(rotatedWords); - } private double round(float value, int decimalPoints) { diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/classification/service/ClassificationService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/classification/service/ClassificationService.java index bd790c9a..bc0418ae 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/classification/service/ClassificationService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/classification/service/ClassificationService.java @@ -34,19 +34,21 @@ public class ClassificationService { for (Page page : document.getPages()) { Rectangle btf = page.isLandscape() ? landscapeBodyTextFrame : bodyTextFrame; - if (!page.isLandscape() && (page.getRotation() == 90 || page.getRotation() == 270)){ - var width = btf.getX2(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()) - btf.getX1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()); - var height = btf.getY2(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()) - btf.getY1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()); - var x1= btf.getX1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()); - var y1 = btf.getY1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()); + if(page.getCropBox().getWidth() > page.getCropBox().getHeight() && page.getRotation() == 270) { + page.setBodyTextFrame(new Rectangle(new Point(btf.getTopLeft().getY(),page.getCropBox().getHeight() - btf.getTopLeft().getX()), btf.getHeight(), -btf.getWidth(), 0)); + } + else if(page.getCropBox().getWidth() > page.getCropBox().getHeight() && page.getRotation() != 0) { + page.setBodyTextFrame(new Rectangle(new Point(btf.getTopLeft().getY(), btf.getTopLeft().getX()), btf.getHeight(), btf.getWidth(), 0)); + } + + else if(page.getRotation() == 180){ + page.setBodyTextFrame(new Rectangle(new Point( btf.getTopLeft().getX(), page.getCropBox().getHeight() - btf.getTopLeft().getY()), btf.getWidth(), -btf.getHeight(), 0)); + } + else { + page.setBodyTextFrame(btf); + } - page.setBodyTextFrame(new Rectangle(new Point(x1, y1), width, height,0)); - } else if (page.getRotation() == 180) { - page.setBodyTextFrame(new Rectangle(new Point(btf.getTopLeft().getX(), page.getCropBox().getHeight() - btf.getTopLeft().getY() -btf.getHeight()), btf.getWidth(), btf.getHeight(),0)); - } else { - page.setBodyTextFrame(btf); - } diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/segmentation/PdfSegmentationService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/segmentation/PdfSegmentationService.java index 0640e7cc..0cf72b50 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/segmentation/PdfSegmentationService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/segmentation/PdfSegmentationService.java @@ -112,6 +112,8 @@ public class PdfSegmentationService { float cropboxArea = cropbox.getHeight() * cropbox.getWidth(); page.setCropBoxArea(cropboxArea); + System.out.println("CropBox: w:" + cropbox.getWidth() + " h:" +cropbox.getHeight() + "MediaBox: w:" + pdPage.getMediaBox().getWidth() + " h:" +pdPage.getMediaBox().getHeight()); + page.setRotation(rotation); page.setLandscape(isLandscape); page.setPageNumber(pageNumber); diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/visualization/service/PdfVisualisationService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/visualization/service/PdfVisualisationService.java index 778aaf41..49b5f89f 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/visualization/service/PdfVisualisationService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/visualization/service/PdfVisualisationService.java @@ -79,10 +79,13 @@ public class PdfVisualisationService { } contentStream.setStrokingColor(Color.YELLOW); -// var widhth = analyzedPage.getBodyTextFrame().getX2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getX1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()); +// var width = analyzedPage.getBodyTextFrame().getX2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getX1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()); // var height = analyzedPage.getBodyTextFrame().getY2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getY1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()); +// +// contentStream.addRect(analyzedPage.getBodyTextFrame().getX1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()), analyzedPage.getBodyTextFrame().getY1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()), width, height); + + contentStream.addRect(analyzedPage.getBodyTextFrame().getTopLeft().getX(), analyzedPage.getBodyTextFrame().getTopLeft().getY(), analyzedPage.getBodyTextFrame().getWidth(), analyzedPage.getBodyTextFrame().getHeight()); - contentStream.addRect(analyzedPage.getBodyTextFrame().getTopLeft().getX(), analyzedPage.getBodyTextFrame().getTopLeft().getY(), analyzedPage.getBodyTextFrame().getWidth(), analyzedPage.getBodyTextFrame().getHeight()); contentStream.stroke(); contentStream.close(); @@ -97,17 +100,18 @@ public class PdfVisualisationService { contentStream.addRect(textBlock.getX1(), textBlock.getY1(), textBlock.getX2() - textBlock.getX1(), textBlock.getY2() - textBlock.getY1()); contentStream.stroke(); -// if (textBlock.getClassification() != null) { -// contentStream.beginText(); -// -// contentStream.setFont(PDType1Font.TIMES_ROMAN, 6f); -// -// contentStream.newLineAtOffset(textBlock.getX1(), textBlock.getY2() + 2); -// contentStream.showText(textBlock.getClassification() + textBlock.getOrientation() + "-->" + textBlock.getSequences().get(0).getDir()); -// -// contentStream.endText(); -// -// + if (textBlock.getClassification() != null) { + contentStream.beginText(); + + contentStream.setNonStrokingColor(Color.BLUE); + contentStream.setFont(PDType1Font.TIMES_ROMAN, 9f); + + contentStream.newLineAtOffset(textBlock.getX1(), textBlock.getY2() + 2); + contentStream.showText(textBlock.getClassification() + textBlock.getOrientation() + "-->" + textBlock.getSequences().get(0).getDir()); + + contentStream.endText(); + + // contentStream.setNonStrokingColor(Color.BLUE); // contentStream.setFont(PDType1Font.TIMES_ROMAN, 2f); // @@ -131,7 +135,7 @@ public class PdfVisualisationService { // // // contentStream.endText(); -// } + } } diff --git a/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java b/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java index 9396f82a..e0276888 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java +++ b/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java @@ -1098,7 +1098,7 @@ public class RedactionIntegrationTest { System.out.println("classificationTest"); - AnalyzeRequest request = prepareStorage("files/Metolachlor/S-Metolachlor_RAR_01_Volume_1_2018-09-06.pdf"); + AnalyzeRequest request = prepareStorage("files/Primicarb/74 Pirimicarb_RAR_01_Volume_1_2017-12-04.pdf"); RedactionRequest redactionRequest = RedactionRequest.builder() .dossierId(request.getDossierId()) diff --git a/redaction-service-v1/redaction-service-server-v1/src/test/resources/files/new/S241.pdf b/redaction-service-v1/redaction-service-server-v1/src/test/resources/files/new/S241.pdf new file mode 100644 index 00000000..bb09979d Binary files /dev/null and b/redaction-service-v1/redaction-service-server-v1/src/test/resources/files/new/S241.pdf differ