Skip to content

Commit

Permalink
refactor: Incorporate the 'PagingSeal' type into the 'Image' type, an…
Browse files Browse the repository at this point in the history
…d add a checkbox to control the automatic cropping feature.
  • Loading branch information
hxlcw committed Aug 12, 2024
1 parent 309283d commit 02e43ec
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public ResponseEntity<byte[]> addStamp(@ModelAttribute AddStampRequest request)
float overrideY = request.getOverrideY(); // New field for Y override
int pagingPosition = request.getPagingPosition();
int firstPageRate = request.getFirstPageRate();
boolean autoCrop = request.isAutoCrop();

String customColor = request.getCustomColor();
float marginFactor;
Expand All @@ -92,7 +93,7 @@ public ResponseEntity<byte[]> addStamp(@ModelAttribute AddStampRequest request)
// Load the input PDF
PDDocument document = Loader.loadPDF(pdfFile.getBytes());

if ("pagingSeal".equalsIgnoreCase(stampType)){
if (autoCrop && "image".equalsIgnoreCase(stampType)){
addPagingSeal(
document,
stampImage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public class AddStampRequest extends PDFWithPageNums {
@Schema(description = "The color for stamp", defaultValue = "#d3d3d3")
private String customColor = "#d3d3d3";

@Schema(description = "The percentage of the seal to the first page", example = "30")
@Schema(description = "Auto crop the image or not.", defaultValue = "false",example = "false")
private boolean autoCrop;

@Schema(description = "The percentage of the seal to the first page", example = "20")
private int firstPageRate;

@Schema(
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/messages_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ AddStampRequest.customMargin=Custom Margin
AddStampRequest.customColor=Custom Text Color
AddStampRequest.firstPageRate=First Page Rate
AddStampRequest.submit=Submit

AddStampRequest.autoCrop=Auto Crop( crops an image and evenly distributes it along the edges of every page )

#sanitizePDF
sanitizePDF.title=Sanitize PDF
Expand Down Expand Up @@ -948,7 +948,6 @@ watermark.selectText.9=Watermark Image:
watermark.submit=Add Watermark
watermark.type.1=Text
watermark.type.2=Image
watermark.type.3=PagingSeal


#Change permissions
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/messages_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ AddStampRequest.customMargin=自定义外边距
AddStampRequest.customColor=自定义文本颜色
AddStampRequest.firstPageRate=首页盖章比例
AddStampRequest.submit=提交

AddStampRequest.autoCrop=自动裁剪(骑缝章)

#sanitizePDF
sanitizePDF.title=清理PDF
Expand Down Expand Up @@ -949,7 +949,6 @@ watermark.selectText.9=水印图片:
watermark.submit=添加水印
watermark.type.1=文字
watermark.type.2=图片
watermark.type.3=骑缝章

#Change permissions
permissions.title=更改权限
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/messages_zh_TW.properties
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ AddStampRequest.customMargin=自訂邊緣
AddStampRequest.customColor=自訂文字顏色
AddStampRequest.firstPageRate=首页盖章比例
AddStampRequest.submit=送出

AddStampRequest.autoCrop=自動裁剪(騎縫章)

#sanitizePDF
sanitizePDF.title=清理 PDF
Expand Down Expand Up @@ -949,7 +949,6 @@ watermark.selectText.9=浮水印影像:
watermark.submit=新增浮水印
watermark.type.1=文字
watermark.type.2=圖片
watermark.type.3=騎縫章

#Change permissions
permissions.title=變更權限
Expand Down
45 changes: 32 additions & 13 deletions src/main/resources/templates/misc/stamp.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@
<select class="form-select" id="stampType" name="stampType" onchange="toggleFileOption()">
<option value="text" th:text="#{watermark.type.1}">Text</option>
<option value="image" th:text="#{watermark.type.2}">Image</option>
<option value="pagingSeal" th:text="#{watermark.type.3}">PagingSeal</option>
</select>
</div>

<div class="form-check ms-3" id="autoCropGroups">
<input type="checkbox" id="autoCrop" name="autoCrop">
<label for="autoCrop" th:text="#{AddStampRequest.autoCrop}"></label>
</div>

<div class="mb-3" id="pageOrderGroup">
<label for="pageOrder" th:text="#{pageSelectionPrompt}"></label>
<input type="text" class="form-control" id="pageOrder" name="pageNumbers" value="1" placeholder="(e.g. 1,3,2 or 4-8,2,10-12 or 2n-1)" required>
Expand Down Expand Up @@ -164,7 +168,7 @@

<div class="mb-3" id="FirstPageRateGroup">
<label for="firstPageRate" class="form-label" th:text="#{AddStampRequest.firstPageRate}">First Page Rate</label>
<input type="number" class="form-control" id="firstPageRate" name="firstPageRate" value="50" step="1" max="90" min="0">
<input type="number" class="form-control" id="firstPageRate" name="firstPageRate" value="20" step="1" max="90" min="0">
</div>

<button type="submit" id="submitBtn" class="btn btn-primary" th:text="#{AddStampRequest.submit}"></button>
Expand All @@ -176,13 +180,25 @@
const stampFormElementIds = ['pageOrderGroup','customMarginGroup','positionGroup',
'stampTextGroup', 'stampImageGroup', 'alphabetGroup', 'sizeGroup', 'rotationGroup',
'opacityGroup', 'overrideXGroup', 'overrideYGroup', 'customColorGroup','pagingPositionGroup',
'FirstPageRateGroup'];
'FirstPageRateGroup','autoCropGroups'];
const stampFormElements = stampFormElementIds.map(it=>document.getElementById(it));

const autoCropGroupsElement = document.getElementById("autoCropGroups")
const autoCropElement = document.getElementById("autoCrop")
// init function
toggleFileOption();
bindSelectedPositionEvent(document.querySelectorAll('#positionGroup .pageNumber'), document.getElementById('numberInput'));
bindSelectedPositionEvent(document.querySelectorAll('#pagingPositionGroup .pageNumber'),document.getElementById('pagingNumberInput'));
autoCropGroupsElement.addEventListener('click',function(){
const allIds = ['pageOrderGroup','customMarginGroup','positionGroup','overrideXGroup',
'pagingPositionGroup','FirstPageRateGroup']
const allElements = allIds.map(it=>document.getElementById(it));
if(autoCropElement.checked){
showElementByIds(allElements,['pagingPositionGroup','FirstPageRateGroup']);
}else{
showElementByIds(allElements,['pageOrderGroup','customMarginGroup','positionGroup','overrideXGroup'])
}
})


function bindSelectedPositionEvent(cells,inputField){
cells.forEach(cell => {
Expand Down Expand Up @@ -214,20 +230,23 @@
function toggleFileOption() {
const stampType = document.getElementById('stampType').value;
if (stampType === 'text') {
showElementByIds('pageOrderGroup','customMarginGroup','positionGroup','stampTextGroup',
showElementByIds(stampFormElements,['pageOrderGroup','customMarginGroup','positionGroup','stampTextGroup',
'alphabetGroup','sizeGroup','rotationGroup','opacityGroup','overrideXGroup',
'overrideYGroup','customColorGroup' )
'overrideYGroup','customColorGroup'] )
} else if (stampType === 'image') {
showElementByIds('pageOrderGroup','customMarginGroup','positionGroup','stampImageGroup',
'sizeGroup','rotationGroup','opacityGroup','overrideXGroup','overrideYGroup')
} else if (stampType === 'pagingSeal'){
showElementByIds('stampImageGroup','pagingPositionGroup', 'sizeGroup','rotationGroup'
,'opacityGroup','overrideYGroup','FirstPageRateGroup')
showElementByIds(stampFormElements,['pageOrderGroup','customMarginGroup','positionGroup','stampImageGroup',
'sizeGroup','rotationGroup','opacityGroup','overrideXGroup','overrideYGroup','autoCropGroups'])
autoCropElement.checked = false
}
}

function showElementByIds(...showIds) {
stampFormElements.forEach(it=>{
/**
* control element show or hidden
* @param formElements all elements id
* @param showIds The IDs that needs to be displayed.
*/
function showElementByIds(formElements,showIds) {
formElements.forEach(it=>{
const id = it.id;
if(showIds && showIds.includes(id)){
it.style.display = 'block';
Expand Down

0 comments on commit 02e43ec

Please sign in to comment.