notosans arabic JP, KR SC
This commit is contained in:
parent
9a74e81754
commit
d5fbe02149
6 changed files with 43 additions and 7 deletions
|
@ -62,6 +62,10 @@ dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
||||||
implementation 'io.micrometer:micrometer-core'
|
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")
|
developmentOnly("org.springframework.boot:spring-boot-devtools")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,10 @@ import org.springframework.web.bind.annotation.RequestPart;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
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.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
@ -55,7 +59,7 @@ public class WatermarkController {
|
||||||
int widthSpacer,
|
int widthSpacer,
|
||||||
@RequestParam(defaultValue = "50", name = "heightSpacer")
|
@RequestParam(defaultValue = "50", name = "heightSpacer")
|
||||||
@Parameter(description = "The height spacer between watermark texts", example = "50")
|
@Parameter(description = "The height spacer between watermark texts", example = "50")
|
||||||
int heightSpacer) throws IOException {
|
int heightSpacer) throws IOException, Exception {
|
||||||
|
|
||||||
// Load the input PDF
|
// Load the input PDF
|
||||||
PDDocument document = PDDocument.load(pdfFile.getInputStream());
|
PDDocument document = PDDocument.load(pdfFile.getInputStream());
|
||||||
|
@ -71,15 +75,43 @@ public class WatermarkController {
|
||||||
graphicsState.setNonStrokingAlphaConstant(opacity);
|
graphicsState.setNonStrokingAlphaConstant(opacity);
|
||||||
contentStream.setGraphicsStateParameters(graphicsState);
|
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
|
// Set font of watermark
|
||||||
// Load NotoSans-Regular font from resources
|
// Load NotoSans-Regular font from resources
|
||||||
ClassPathResource classPathResource = new ClassPathResource("static/fonts/NotoSans-Regular.ttf");
|
String resourceDir = "";
|
||||||
File tempFile = File.createTempFile("NotoSans-Regular", ".ttf");
|
PDFont font = PDType1Font.HELVETICA_BOLD;
|
||||||
try (InputStream is = classPathResource.getInputStream(); FileOutputStream os = new FileOutputStream(tempFile)) {
|
switch (lang) {
|
||||||
IOUtils.copy(is, os);
|
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.beginText();
|
||||||
contentStream.setFont(font, fontSize);
|
contentStream.setFont(font, fontSize);
|
||||||
contentStream.setNonStrokingColor(Color.LIGHT_GRAY);
|
contentStream.setNonStrokingColor(Color.LIGHT_GRAY);
|
||||||
|
|
BIN
src/main/resources/static/fonts/NotoSansKR-Regular.otf
Normal file
BIN
src/main/resources/static/fonts/NotoSansKR-Regular.otf
Normal file
Binary file not shown.
BIN
src/main/resources/static/fonts/NotoSansSC-Regular.otf
Normal file
BIN
src/main/resources/static/fonts/NotoSansSC-Regular.otf
Normal file
Binary file not shown.
Binary file not shown.
BIN
src/main/resources/static/fonts/static/NotoSansJP-Regular.ttf
Normal file
BIN
src/main/resources/static/fonts/static/NotoSansJP-Regular.ttf
Normal file
Binary file not shown.
Loading…
Reference in a new issue