This gist contains code snippets related to the feature of adding watermark to PDF files using Aspose.PDF Cloud SDK for Java. For more information on steps and related details, please visit
Last active
September 25, 2022 21:31
-
-
Save aspose-cloud/fcbf3275865b1af6a1e1797d6aff21fa to your computer and use it in GitHub Desktop.
Add watermark to PDF online using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This Gist contains code snippets related to Adding watermarks to PDF file using Aspose.PDF Cloud SDK for Java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and source code, please visit https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-java | |
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/ | |
String clientId = "c235e685-1aab-4cda-a95b-54afd63eb87f"; | |
String clientSecret = "b8da4ee37494f2ef8da3c727f3a0acb9"; | |
// create an instance of PdfApii | |
PdfApi pdfApi = new PdfApi(clientSecret,clientId); | |
// input PDF document | |
String file = "PdfWithTable.pdf"; | |
// Source image to be used for watermark | |
String image = "Koala.jpg"; | |
// page number where watermark will be added | |
int pageNumber = 1; | |
// create an instance of ImageStamp object | |
ImageStamp stampObject = new ImageStamp() | |
.fileName(image) | |
.leftMargin(1.) | |
.rightMargin(2.) | |
.topMargin(3.) | |
.bottomMargin(214.) | |
.verticalAlignment(com.aspose.asposecloudpdf.model.VerticalAlignment.CENTER); | |
// add watermark behind page content | |
stampObject.background(true) | |
.horizontalAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.CENTER) | |
.opacity(1.) | |
.rotate(com.aspose.asposecloudpdf.model.Rotation.NONE) | |
.rotateAngle(0.) | |
.xindent(0.) | |
.yindent(0.) | |
.zoom(0.2); | |
// create a list of ImageStamp object | |
List<com.aspose.asposecloudpdf.model.ImageStamp> stamps = new ArrayList<>(); | |
// Add ImageStamp to List of ImageStamps | |
stamps.add(stampObject); | |
// call API method to add watermark image to PDF | |
AsposeResponse response = pdfApi.postPageImageStamps(file, pageNumber, stamps,null, null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and source code, please visit https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-java | |
// Get ClientID and ClientSecret from https://cloud.aspose.com | |
String clientId = "c235e685-1aab-4cda-a95b-54afd63eb87f"; | |
String clientSecret = "b8da4ee37494f2ef8da3c727f3a0acb9"; | |
// create PdfApi instance | |
PdfApi pdfApi = new PdfApi(clientSecret,clientId); | |
// input PDF document | |
String sourcePDF = "PdfWithAnnotations.pdf"; | |
// Source PDF to be used for watermark | |
String stampPDF = "rusdoc.pdf"; | |
// page number where watermark will be added | |
int pageNumber = 1; | |
// create an instance of PdfPageStamp object | |
PdfPageStamp stampObject = new PdfPageStamp() | |
// PDF file to be used as watermark | |
.fileName(stampPDF) | |
// specify the page index which you want to add as watermark | |
.pageIndex(1) | |
.leftMargin(150.) | |
.rightMargin(2.) | |
// we have set -ve top margin to move up the watermark PDF | |
.topMargin(-330.) | |
.bottomMargin(414.) | |
// set vertical alignment of watermark as Top | |
.verticalAlignment(com.aspose.asposecloudpdf.model.VerticalAlignment.TOP); | |
// bring the watermark to foreground/visible | |
stampObject.background(false) | |
.horizontalAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.CENTER) | |
.opacity(1.) | |
.rotate(com.aspose.asposecloudpdf.model.Rotation.NONE) | |
.rotateAngle(0.) | |
.xindent(0.) | |
.yindent(0.) | |
// set zoom factor as 0.7 so that its squeezed rather it appears in 100% | |
.zoom(0.7); | |
// create a list of PdfPageStamp object | |
List<com.aspose.asposecloudpdf.model.PdfPageStamp> stamps = new ArrayList<>(); | |
// Add PdfPageStamp to List of ImageStamps | |
stamps.add(stampObject); | |
// call API method to add PDF as watermark | |
AsposeResponse response = pdfApi.postPagePdfPageStamps(sourcePDF, pageNumber, stamps,null, null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and source code, please visit https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-java | |
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/ | |
String clientId = "29ac1517-753f-4303-b755-7185e35cf939"; | |
String clientSecret = "c537caf71eafc8a75a5ee7813b703276"; | |
String sourceFileName = "PdfWithTable.pdf"; | |
int pageNumber = 1; | |
String cloudStorage = ""; | |
String folder = ""; | |
// create an instance of PdfApi using Client ID and Client Secret details | |
com.aspose.asposecloudpdf.api.PdfApi pdfApi = new com.aspose.asposecloudpdf.api.PdfApi(clientSecret,clientId); | |
// create an object of TextState and specify font formatting details | |
com.aspose.asposecloudpdf.model.TextState textState = new com.aspose.asposecloudpdf.model.TextState().fontSize(24.).font("Arial"); | |
com.aspose.asposecloudpdf.model.TextStamp stamp = new com.aspose.asposecloudpdf.model.TextStamp() | |
.textAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.CENTER) | |
.value("Text Stamp") | |
.textState(textState) | |
.leftMargin(1.) | |
.rightMargin(2.) | |
.topMargin(3.) | |
.bottomMargin(4.) | |
.verticalAlignment(com.aspose.asposecloudpdf.model.VerticalAlignment.CENTER); | |
stamp.background(true) | |
.horizontalAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.CENTER) | |
.opacity(1.) | |
.rotate(com.aspose.asposecloudpdf.model.Rotation.ON90) | |
.rotateAngle(45.) | |
.xindent(0.) | |
.yindent(0.) | |
.zoom(1.); | |
// create a List object with type TextStamp | |
List<com.aspose.asposecloudpdf.model.TextStamp> stamps = new ArrayList<>(); | |
// add TextStamp instance to list of TextStamps | |
stamps.add(stamp); | |
// call method to postTextStamp to first page of PDF file | |
com.aspose.asposecloudpdf.model.AsposeResponse response = pdfApi.postPageTextStamps(sourceFileName, pageNumber, stamps,cloudStorage, folder); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and source code, please visit https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-java | |
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/ | |
String clientId = "c235e685-1aab-4cda-a95b-54afd63eb87f"; | |
String clientSecret = "b8da4ee37494f2ef8da3c727f3a0acb9"; | |
PdfApi pdfApi = new PdfApi(clientSecret,clientId); | |
// input PDF document | |
String sourcePDF = "PdfWithTable.pdf"; | |
// create an instance of PdfPageStamp object | |
PageNumberStamp stampObject = new PageNumberStamp() | |
// Format for Page Number watermark | |
.value("Page #") | |
.leftMargin(1.) | |
.rightMargin(2.) | |
.topMargin(3.) | |
.bottomMargin(4.) | |
// set vertical alignment of watermark as Bottom | |
.verticalAlignment(com.aspose.asposecloudpdf.model.VerticalAlignment.BOTTOM); | |
// specify the starting number for counter | |
stampObject.setStartingNumber(3); | |
// bring the watermark to foreground/visible | |
stampObject.background(false) | |
.horizontalAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.CENTER) | |
.opacity(1.) | |
.rotate(com.aspose.asposecloudpdf.model.Rotation.NONE) | |
.rotateAngle(0.) | |
.xindent(0.) | |
.yindent(0.) | |
.zoom(1.0); | |
// staring page form where to put watermark | |
int startPageNumber = 2; | |
// page index towards which watermark shall be placed | |
int endPageNumber = 3; | |
// call API method to add PDF as watermark | |
AsposeResponse response = pdfApi.postDocumentPageNumberStamps(sourcePDF, stampObject,startPageNumber, endPageNumber, null, null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment