diff --git a/build.gradle b/build.gradle index cdde3de5..bd2806be 100644 --- a/build.gradle +++ b/build.gradle @@ -62,6 +62,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'io.micrometer:micrometer-core' + // https://mvnrepository.com/artifact/com.cybozu.labs/langdetect +implementation group: 'com.cybozu.labs', name: 'langdetect', version: '1.1-20120112' + + developmentOnly("org.springframework.boot:spring-boot-devtools") } diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java b/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java index 1b3d2a4d..2fcf21a1 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java @@ -18,6 +18,10 @@ import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import com.cybozu.labs.langdetect.Detector; +import com.cybozu.labs.langdetect.DetectorFactory; +import com.cybozu.labs.langdetect.LangDetectException; + import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import stirling.software.SPDF.utils.WebResponseUtils; @@ -55,7 +59,7 @@ public class WatermarkController { int widthSpacer, @RequestParam(defaultValue = "50", name = "heightSpacer") @Parameter(description = "The height spacer between watermark texts", example = "50") - int heightSpacer) throws IOException { + int heightSpacer) throws IOException, Exception { // Load the input PDF PDDocument document = PDDocument.load(pdfFile.getInputStream()); @@ -71,15 +75,43 @@ public class WatermarkController { graphicsState.setNonStrokingAlphaConstant(opacity); contentStream.setGraphicsStateParameters(graphicsState); + DetectorFactory.loadProfile("profiles"); + + Detector detector = DetectorFactory.create(); + detector.append(watermarkText); + String lang = detector.detect(); + + System.out.println("Detected lang" + lang); // Set font of watermark // Load NotoSans-Regular font from resources - ClassPathResource classPathResource = new ClassPathResource("static/fonts/NotoSans-Regular.ttf"); - File tempFile = File.createTempFile("NotoSans-Regular", ".ttf"); - try (InputStream is = classPathResource.getInputStream(); FileOutputStream os = new FileOutputStream(tempFile)) { - IOUtils.copy(is, os); + String resourceDir = ""; + PDFont font = PDType1Font.HELVETICA_BOLD; + switch (lang) { + case "ar": + resourceDir = "src/main/resources/static/fonts/NotoSansArabic-Regular.ttf"; + break; + case "ja": + resourceDir = "src/main/resources/static/fonts/NotoSansJP-Regular.otf"; + break; + case "ko": + resourceDir = "src/main/resources/static/fonts/NotoSansKR-Regular.otf"; + break; + case "zh-cn": + resourceDir = "src/main/resources/static/fonts/NotoSansSC-Regular.otf"; + break; + default: + font = PDType1Font.HELVETICA_BOLD; + } + + if(!resourceDir.equals("")) { + ClassPathResource classPathResource = new ClassPathResource("static/fonts/NotoSans-Regular.ttf"); + File tempFile = File.createTempFile("NotoSans-Regular", ".ttf"); + try (InputStream is = classPathResource.getInputStream(); FileOutputStream os = new FileOutputStream(tempFile)) { + IOUtils.copy(is, os); + } + + font = PDType0Font.load(document, tempFile); } - PDFont font = PDType0Font.load(document, tempFile); - contentStream.beginText(); contentStream.setFont(font, fontSize); contentStream.setNonStrokingColor(Color.LIGHT_GRAY); diff --git a/src/main/resources/static/fonts/NotoSansKR-Regular.otf b/src/main/resources/static/fonts/NotoSansKR-Regular.otf new file mode 100644 index 00000000..7c5c2fae Binary files /dev/null and b/src/main/resources/static/fonts/NotoSansKR-Regular.otf differ diff --git a/src/main/resources/static/fonts/NotoSansSC-Regular.otf b/src/main/resources/static/fonts/NotoSansSC-Regular.otf new file mode 100644 index 00000000..d350ffa7 Binary files /dev/null and b/src/main/resources/static/fonts/NotoSansSC-Regular.otf differ diff --git a/src/main/resources/static/fonts/static/NotoSansArabic-Regular.ttf b/src/main/resources/static/fonts/static/NotoSansArabic-Regular.ttf new file mode 100644 index 00000000..79359c46 Binary files /dev/null and b/src/main/resources/static/fonts/static/NotoSansArabic-Regular.ttf differ diff --git a/src/main/resources/static/fonts/static/NotoSansJP-Regular.ttf b/src/main/resources/static/fonts/static/NotoSansJP-Regular.ttf new file mode 100644 index 00000000..1583096a Binary files /dev/null and b/src/main/resources/static/fonts/static/NotoSansJP-Regular.ttf differ