|
| 1 | +// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW |
| 4 | +// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. |
| 5 | +// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. |
| 6 | +// This notice may not be removed from this file. |
| 7 | + |
| 8 | +package com.compdfkit; |
| 9 | + |
| 10 | + |
| 11 | +import com.compdfkit.client.CPDFClient; |
| 12 | +import com.compdfkit.constant.CPDFConstant; |
| 13 | +import com.compdfkit.enums.CPDFDocumentEditorEnum; |
| 14 | +import com.compdfkit.param.CPDFAddWatermarkParameter; |
| 15 | +import com.compdfkit.pojo.comPdfKit.CPDFCreateTaskResult; |
| 16 | +import com.compdfkit.pojo.comPdfKit.CPDFFileInfo; |
| 17 | +import com.compdfkit.pojo.comPdfKit.CPDFTaskInfoResult; |
| 18 | +import com.compdfkit.pojo.comPdfKit.CPDFUploadFileResult; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileInputStream; |
| 22 | +import java.io.FileNotFoundException; |
| 23 | +import java.util.concurrent.Executors; |
| 24 | +import java.util.concurrent.ScheduledExecutorService; |
| 25 | +import java.util.concurrent.ScheduledFuture; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | + |
| 29 | +public class AddWatermark { |
| 30 | + |
| 31 | + private static final String publicKit = ""; |
| 32 | + private static final String secretKey = ""; |
| 33 | + private static final CPDFClient client = new CPDFClient(publicKit,secretKey); |
| 34 | + |
| 35 | + public static void main(String[] args) throws FileNotFoundException { |
| 36 | + AddWatermark.addWatermarkText(); |
| 37 | +// AddWatermark.addWatermarkImage(); |
| 38 | + } |
| 39 | + |
| 40 | + public static void addWatermarkText() throws FileNotFoundException { |
| 41 | + // create Task |
| 42 | + CPDFCreateTaskResult CPDFCreateTaskResult = client.createTask(CPDFDocumentEditorEnum.ADD_WATERMARK); |
| 43 | + // taskId |
| 44 | + String taskId = CPDFCreateTaskResult.getTaskId(); |
| 45 | + // upload File |
| 46 | + File file = new File("sample/test.pdf"); |
| 47 | + String filePassword = ""; |
| 48 | + CPDFAddWatermarkParameter fileParameter = new CPDFAddWatermarkParameter(); |
| 49 | + fileParameter.setType("text"); |
| 50 | + fileParameter.setScale("1"); |
| 51 | + fileParameter.setOpacity("0.5"); |
| 52 | + fileParameter.setRotation("0.785"); |
| 53 | + fileParameter.setTargetPages("1-2"); |
| 54 | + fileParameter.setVertalign("center"); |
| 55 | + fileParameter.setHorizalign("left"); |
| 56 | + fileParameter.setXoffset("100"); |
| 57 | + fileParameter.setYoffset("100"); |
| 58 | + fileParameter.setContent("test"); |
| 59 | + fileParameter.setTextColor("#59c5bb"); |
| 60 | + fileParameter.setFullScreen("111"); |
| 61 | + fileParameter.setHorizontalSpace("10"); |
| 62 | + fileParameter.setVerticalSpace("10"); |
| 63 | + CPDFUploadFileResult CPDFUploadFileResult = client.uploadFile(new FileInputStream(file),taskId,filePassword,fileParameter,file.getName()); |
| 64 | + String fileKey = CPDFUploadFileResult.getFileKey(); |
| 65 | + // perform tasks |
| 66 | + client.executeTask(taskId); |
| 67 | + // create a ScheduledExecutorService with a fixed thread pool |
| 68 | + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); |
| 69 | + // create an AtomicReference to hold the future |
| 70 | + AtomicReference<ScheduledFuture<?>> futureRef = new AtomicReference<>(); |
| 71 | + // schedule a task to check the task status at a fixed rate |
| 72 | + futureRef.set(executor.scheduleAtFixedRate(() -> { |
| 73 | + // get task processing information |
| 74 | + CPDFTaskInfoResult taskInfo = client.getTaskInfo(taskId); |
| 75 | + // determine whether the task status is "TaskFinish" |
| 76 | + if (taskInfo.getTaskStatus().equals(CPDFConstant.TASK_FINISH)) { |
| 77 | + System.out.println(taskInfo); |
| 78 | + // get the final file processing information |
| 79 | + CPDFFileInfo fileInfo = client.getFileInfo(fileKey); |
| 80 | + System.out.println(fileInfo); |
| 81 | + // if the task is finished, cancel the scheduled task |
| 82 | + futureRef.get().cancel(false); |
| 83 | + } |
| 84 | + }, 0, 5, TimeUnit.SECONDS)); |
| 85 | + // schedule a task to cancel the scheduled task after 15 minutes |
| 86 | + executor.schedule(() -> { |
| 87 | + futureRef.get().cancel(false); |
| 88 | + }, 15, TimeUnit.MINUTES); |
| 89 | + // shutdown the executor |
| 90 | + executor.shutdown(); |
| 91 | + } |
| 92 | + |
| 93 | + public static void addWatermarkImage() throws FileNotFoundException { |
| 94 | + // create Task |
| 95 | + CPDFCreateTaskResult CPDFCreateTaskResult = client.createTask(CPDFDocumentEditorEnum.ADD_WATERMARK); |
| 96 | + // taskId |
| 97 | + String taskId = CPDFCreateTaskResult.getTaskId(); |
| 98 | + // upload File |
| 99 | + File file = new File("sample/test.pdf"); |
| 100 | + String filePassword = ""; |
| 101 | + CPDFAddWatermarkParameter fileParameter = new CPDFAddWatermarkParameter(); |
| 102 | + fileParameter.setType("image"); |
| 103 | + fileParameter.setScale("1"); |
| 104 | + fileParameter.setOpacity("0.5"); |
| 105 | + fileParameter.setRotation("0.785"); |
| 106 | + fileParameter.setTargetPages("1-2"); |
| 107 | + fileParameter.setVertalign("center"); |
| 108 | + fileParameter.setHorizalign("left"); |
| 109 | + fileParameter.setXoffset("100"); |
| 110 | + fileParameter.setYoffset("100"); |
| 111 | + fileParameter.setFullScreen("111"); |
| 112 | + fileParameter.setHorizontalSpace("10"); |
| 113 | + fileParameter.setVerticalSpace("10"); |
| 114 | + CPDFUploadFileResult CPDFUploadFileResult = client.uploadFile(new FileInputStream(file),taskId,filePassword,fileParameter,file.getName(),new File("sample/test.jpg")); |
| 115 | + String fileKey = CPDFUploadFileResult.getFileKey(); |
| 116 | + // perform tasks |
| 117 | + client.executeTask(taskId); |
| 118 | + // create a ScheduledExecutorService with a fixed thread pool |
| 119 | + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); |
| 120 | + // create an AtomicReference to hold the future |
| 121 | + AtomicReference<ScheduledFuture<?>> futureRef = new AtomicReference<>(); |
| 122 | + // schedule a task to check the task status at a fixed rate |
| 123 | + futureRef.set(executor.scheduleAtFixedRate(() -> { |
| 124 | + // get task processing information |
| 125 | + CPDFTaskInfoResult taskInfo = client.getTaskInfo(taskId); |
| 126 | + // determine whether the task status is "TaskFinish" |
| 127 | + if (taskInfo.getTaskStatus().equals(CPDFConstant.TASK_FINISH)) { |
| 128 | + System.out.println(taskInfo); |
| 129 | + // get the final file processing information |
| 130 | + CPDFFileInfo fileInfo = client.getFileInfo(fileKey); |
| 131 | + System.out.println(fileInfo); |
| 132 | + // if the task is finished, cancel the scheduled task |
| 133 | + futureRef.get().cancel(false); |
| 134 | + } |
| 135 | + }, 0, 5, TimeUnit.SECONDS)); |
| 136 | + // schedule a task to cancel the scheduled task after 15 minutes |
| 137 | + executor.schedule(() -> { |
| 138 | + futureRef.get().cancel(false); |
| 139 | + }, 15, TimeUnit.MINUTES); |
| 140 | + // shutdown the executor |
| 141 | + executor.shutdown(); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments