From 4cc5b53313deca81fe2e3e4fdb73f3e5908baab4 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Fri, 17 Nov 2023 14:44:30 +0100 Subject: [PATCH] Added watermarker as parameter --- .../Geoportal_PDF_Exporter.java | 86 ++++++++++++++++--- .../shared/ExporterProjectSource.java | 3 + .../java/Geoportal_Export_To_PDF_Tests.java | 10 ++- 3 files changed, 84 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/gcube/application/geoportaldatamapper/Geoportal_PDF_Exporter.java b/src/main/java/org/gcube/application/geoportaldatamapper/Geoportal_PDF_Exporter.java index 65632ac..8fea085 100644 --- a/src/main/java/org/gcube/application/geoportaldatamapper/Geoportal_PDF_Exporter.java +++ b/src/main/java/org/gcube/application/geoportaldatamapper/Geoportal_PDF_Exporter.java @@ -83,6 +83,7 @@ import com.itextpdf.layout.element.Table; import com.itextpdf.layout.element.Text; import com.itextpdf.layout.properties.HorizontalAlignment; import com.itextpdf.layout.properties.TextAlignment; +import com.itextpdf.layout.properties.VerticalAlignment; import com.itextpdf.layout.renderer.CellRenderer; import com.itextpdf.layout.renderer.DrawContext; import com.itextpdf.layout.renderer.IRenderer; @@ -245,6 +246,8 @@ public class Geoportal_PDF_Exporter { } + private ExporterProjectSource exportSource; + /** * Read credits. * @@ -295,7 +298,8 @@ public class Geoportal_PDF_Exporter { * @throws Exception the exception */ public String createPDFFile(ExporterProjectSource exportSource) throws Exception { - + this.exportSource = exportSource; + String pdfFileURL = null; Exporter_PDF_Credits credits = readCredits(); @@ -328,11 +332,19 @@ public class Geoportal_PDF_Exporter { } if (exportSource.isAddPageNumbers()) { - // Creates a header for every page in the document. The HeaderHandler implements + // Creates a header for every page in the document. The PageNumberHandler + // implements // add page numbers. - pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new HeaderHandler()); + pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new PageNumberHandler()); + } + + if (exportSource.getWatermarkText() != null) { + // pdfDocument = addStringAsWatermark(pdfDocument, document, + // exportSource.getWatermarkText()); + pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new WaterMarkerHandler()); } + // Initialize document Document document = new Document(pdfDocument); @@ -419,6 +431,8 @@ public class Geoportal_PDF_Exporter { // Credits Table tableCredits = addCredits(document, credits); document.add(tableCredits); + + document.close(); // Copying OutputStream into Temp File on Storage @@ -460,13 +474,13 @@ public class Geoportal_PDF_Exporter { } /** - * The Class HeaderHandler. + * The Class PageNumberHandler. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * - * Nov 15, 2023 + * Nov 17, 2023 */ - public class HeaderHandler implements IEventHandler { + public class PageNumberHandler implements IEventHandler { /** * Handle event. @@ -491,13 +505,63 @@ public class Geoportal_PDF_Exporter { // TODO Auto-generated catch block e1.printStackTrace(); } - int x = 565; - int y = 25; - canvas.showTextAligned(p, x, y, TextAlignment.RIGHT); +// int x = 565; +// int y = 25; + float xR = pageSize.getRight() - 30; + float yB = pageSize.getBottom() + 30; + canvas.showTextAligned(p, xR, yB, TextAlignment.RIGHT); pdfCanvas.release(); } } + /** + * The Class WaterMarkerHandler. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Nov 17, 2023 + */ + public class WaterMarkerHandler implements IEventHandler { + + /** + * Handle event. + * + * @param event the event + */ + @Override + public void handleEvent(Event event) { + PdfDocumentEvent docEvent = (PdfDocumentEvent) event; + PdfDocument pdf = docEvent.getDocument(); + PdfPage page = docEvent.getPage(); + Rectangle pageSize = page.getPageSize(); + PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdf); + int pageNumber = pdf.getPageNumber(page); + + page.setIgnorePageRotationForContent(true); + + PdfFont font; + try { + font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN); + + Paragraph paragraph = new Paragraph(exportSource.getWatermarkText()).setFont(font).setFontSize(130) + .setFontColor(ColorConstants.RED).setOpacity(new Float(0.2)); + + //PdfExtGState gs1 = new PdfExtGState().setFillOpacity(0.5f); + + Canvas canvas = new Canvas(pdfCanvas, pageSize); + + float x = (pageSize.getLeft() + pageSize.getRight()) / 2; + float y = (pageSize.getTop() + pageSize.getBottom()) / 2; + //pdfCanvas.saveState(); + //pdfCanvas.setExtGState(gs1); + canvas.showTextAligned(paragraph, x, y, pageNumber, TextAlignment.CENTER, VerticalAlignment.TOP, 45); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + /** * Link to marker URL via map box static map. * @@ -652,6 +716,7 @@ public class Geoportal_PDF_Exporter { * @param subDocuments the sub documents * @param gisLink the gis link * @param sap the sap + * @param credits the credits * @return the document */ private static Document appendSubDocumentToPDF(Document document, SectionView sectionView, @@ -847,7 +912,6 @@ public class Geoportal_PDF_Exporter { for (int i = 1; i < rows; i++) { Cell cell1 = table.getCell(i, 0); cell1.setMinWidth(80); - // cell1.setMaxWidth(100); } document.add(table); @@ -1204,8 +1268,6 @@ public class Geoportal_PDF_Exporter { } else { Cell cell1 = createContentCell(2); cell1.setFontColor(ColorConstants.GRAY); -// cell1.setMinWidth(100); -// cell1.setMaxWidth(150); cell1.add(new Paragraph(key)); table.addCell(cell1); // row.add(key); diff --git a/src/main/java/org/gcube/application/geoportaldatamapper/shared/ExporterProjectSource.java b/src/main/java/org/gcube/application/geoportaldatamapper/shared/ExporterProjectSource.java index 2a8dbbb..4187fe0 100644 --- a/src/main/java/org/gcube/application/geoportaldatamapper/shared/ExporterProjectSource.java +++ b/src/main/java/org/gcube/application/geoportaldatamapper/shared/ExporterProjectSource.java @@ -11,12 +11,15 @@ public class ExporterProjectSource { String profileID; String projectID; String scope; + //Optional fields String profileTitle; String accountname; //if null the exporter will use PUBLIC ACCESS String gisLink; + String watermarkText; boolean addTimpestampCreatedAt = true; boolean addPageNumbers = true; + } diff --git a/src/test/java/Geoportal_Export_To_PDF_Tests.java b/src/test/java/Geoportal_Export_To_PDF_Tests.java index 6c7672e..b87136a 100644 --- a/src/test/java/Geoportal_Export_To_PDF_Tests.java +++ b/src/test/java/Geoportal_Export_To_PDF_Tests.java @@ -49,7 +49,10 @@ public class Geoportal_Export_To_PDF_Tests { // 6388d9b3a1e60e66b7b5843a // 646353c5d7fb4a4633022803 Villa Romana del Capo di Sorrento // 63f8c481e9d6ac16f394f1e6 - //638888fba1e60e66b7b581dd + // 638888fba1e60e66b7b581dd + // 650304487814b87373631242 Scavi a Calvatone-Bedriacum + // 6388ea17a1e60e66b7b584dd Indagini archeologiche presso la pieve di San Pietro in Bossolo, Barberino Tavarnelle (FI) + // 64537208d7fb4a4633022039 PArCo di Poggio del Molino - Progetto Archeodig // PRE // 63d011c4dcac4551b9a6b930 @@ -93,7 +96,7 @@ public class Geoportal_Export_To_PDF_Tests { * * @return the client */ - @Before + //@Before public void getClient() { readContextSettings(); // assumeTrue(GCubeTest.isTestInfrastructureEnabled()); @@ -120,7 +123,7 @@ public class Geoportal_Export_To_PDF_Tests { /** * Test read project view. */ - @Test + //@Test public void testExportToPDF() { ScopeProvider.instance.set(CONTEXT); @@ -134,6 +137,7 @@ public class Geoportal_Export_To_PDF_Tests { exporterSource.setProjectID(PROJECT_ID); exporterSource.setProfileTitle("Regime di Concessione"); exporterSource.setScope(CONTEXT); + exporterSource.setWatermarkText("DRAFT"); try { pdfExporter.createPDFFile(exporterSource); } catch (Exception e) {