From 49139ee603c87f3e2a99a96998ba03a0e189ee82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominique=20Eifl=C3=A4nder?= Date: Thu, 2 May 2024 12:53:55 +0200 Subject: [PATCH] RED-9103: Fixed save of document viewer file --- .../visualization/ViewerDocumentService.java | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/layoutparser-service/layoutparser-service-processor/src/main/java/com/knecon/fforesight/service/layoutparser/processor/services/visualization/ViewerDocumentService.java b/layoutparser-service/layoutparser-service-processor/src/main/java/com/knecon/fforesight/service/layoutparser/processor/services/visualization/ViewerDocumentService.java index 2290eed..386002d 100644 --- a/layoutparser-service/layoutparser-service-processor/src/main/java/com/knecon/fforesight/service/layoutparser/processor/services/visualization/ViewerDocumentService.java +++ b/layoutparser-service/layoutparser-service-processor/src/main/java/com/knecon/fforesight/service/layoutparser/processor/services/visualization/ViewerDocumentService.java @@ -4,11 +4,9 @@ import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.io.IOException; import java.io.OutputStream; -import java.util.HashSet; -import java.util.Set; -import org.apache.pdfbox.cos.COSDictionary; import org.apache.pdfbox.cos.COSName; +import org.apache.pdfbox.pdfwriter.compress.CompressParameters; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDPage; @@ -40,7 +38,6 @@ import lombok.extern.slf4j.Slf4j; @RequiredArgsConstructor public class ViewerDocumentService { - private static final String LAYER_NAME = "Layout grid"; private static final int FONT_SIZE = 10; public static final float LINE_WIDTH = 1f; @@ -54,8 +51,7 @@ public class ViewerDocumentService { LayoutGrid layoutGrid = layoutGridService.createLayoutGrid(document); // PDDocument.save() is very slow, since it actually traverses the entire pdf and writes a new one. // If we collect all COSDictionaries we changed and tell it explicitly to only add the changed ones by using saveIncremental it's very fast. - Set dictionariesToUpdate = new HashSet<>(); - PDOptionalContentGroup layer = addLayerToDocument(pdDocument, dictionariesToUpdate, layerVisibilityDefaultValue); + PDOptionalContentGroup layer = addLayerToDocument(pdDocument, layerVisibilityDefaultValue); PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA); for (int pageNumber = 0; pageNumber < pdDocument.getNumberOfPages(); pageNumber++) { @@ -68,7 +64,8 @@ public class ViewerDocumentService { // e.g. not escaped matrix transformations. escapePreviousContents(pdDocument, pdPage); - VisualizationsOnPage visualizationsOnPage = layoutGrid.getVisualizationsPerPages().get(pageNumber); + VisualizationsOnPage visualizationsOnPage = layoutGrid.getVisualizationsPerPages() + .get(pageNumber); assert pageNumber == visualizationsOnPage.getPageNumber(); // We need to append to the content stream, otherwise the content could be overlapped by following content. try (var contentStream = new PDPageContentStream(pdDocument, pdPage, PDPageContentStream.AppendMode.APPEND, true)) { @@ -102,11 +99,11 @@ public class ViewerDocumentService { contentStream.setFont(font, FONT_SIZE); contentStream.beginText(); Matrix textMatrix = new Matrix((float) textDeRotationMatrix.getScaleX(), - (float) textDeRotationMatrix.getShearX(), - (float) textDeRotationMatrix.getShearY(), - (float) textDeRotationMatrix.getScaleY(), - (float) placedText.lineStart().getX(), - (float) placedText.lineStart().getY()); + (float) textDeRotationMatrix.getShearX(), + (float) textDeRotationMatrix.getShearY(), + (float) textDeRotationMatrix.getScaleY(), + (float) placedText.lineStart().getX(), + (float) placedText.lineStart().getY()); textMatrix.translate(-((font.getStringWidth(placedText.text()) / 1000) * FONT_SIZE + (2 * LINE_WIDTH) + 4), -FONT_SIZE); contentStream.setTextMatrix(textMatrix); contentStream.showText(placedText.text()); @@ -115,12 +112,9 @@ public class ViewerDocumentService { contentStream.restoreGraphicsState(); contentStream.endMarkedContent(); } - dictionariesToUpdate.add(pdPage.getCOSObject()); - dictionariesToUpdate.add(pdPage.getResources().getCOSObject()); } - dictionariesToUpdate.add(pdDocument.getDocumentInformation().getCOSObject()); // dictionariesToUpdate.add(pdDocument.getDocument().getTrailer()); - pdDocument.saveIncremental(outputStream, dictionariesToUpdate); + pdDocument.save(outputStream, CompressParameters.NO_COMPRESSION); } @@ -145,7 +139,7 @@ public class ViewerDocumentService { } - private static PDOptionalContentGroup addLayerToDocument(PDDocument pdDocument, Set dictionariesToUpdate, boolean layerVisibilityDefaultValue) { + private static PDOptionalContentGroup addLayerToDocument(PDDocument pdDocument, boolean layerVisibilityDefaultValue) { PDDocumentCatalog catalog = pdDocument.getDocumentCatalog(); PDOptionalContentProperties ocprops = catalog.getOCProperties(); @@ -161,7 +155,6 @@ public class ViewerDocumentService { ocprops.addGroup(layer); } ocprops.setGroupEnabled(layer, layerVisibilityDefaultValue); - dictionariesToUpdate.add(catalog.getCOSObject()); return layer; }