Skip to content

Instantly share code, notes, and snippets.

@aspose-cloud
Last active May 17, 2021 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-cloud/ad5d50a0b4add100859bcbc44e21fec0 to your computer and use it in GitHub Desktop.
Save aspose-cloud/ad5d50a0b4add100859bcbc44e21fec0 to your computer and use it in GitHub Desktop.
Add Header Footer in PDF using Java
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/
String clientId = "29ac1517-753f-4303-b755-7185e35cf939";
String clientSecret = "c537caf71eafc8a75a5ee7813b703276";
// createPdfApi instance
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// input PDF document
String sourcePDF = "PdfWithAnnotations.pdf";
// start page of PDF to place Header Image
int startPage = 2;
// end page of PDF to place Header Image
int endPage = 3;
// create an instance of ImageHeader object
ImageHeader headerObject = new ImageHeader()
.width(250.0)
.height(80.0)
.leftMargin(1.)
.rightMargin(500.)
.topMargin(10.)
.fileName("asposetoolsnew.png");
// bring Header Image to foreground/visible
headerObject.background(false)
// set image header alignment as Left
.horizontalAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.LEFT)
.opacity(0.8)
.rotate(com.aspose.asposecloudpdf.model.Rotation.NONE)
.rotateAngle(0.)
.xindent(2.0)
.yindent(5.0)
.zoom(1.0);
// call API method to add Header Image in PDF
AsposeResponse response = pdfApi.postDocumentImageHeader(sourcePDF, headerObject,startPage, endPage, null, null);
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/
String clientId = "29ac1517-753f-4303-b755-7185e35cf939";
String clientSecret = "c537caf71eafc8a75a5ee7813b703276";
// createPdfApi instance
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// input PDF document
String sourcePDF = "PdfWithAnnotations.pdf";
// start page of PDF to place Header Image
int startPage = 2;
// end page of PDF to place Header Image
int endPage = 3;
// specify foreground color as SlateBlue in Hex code
Color foregroundColor = new Color();
foregroundColor.setA(0x00);
foregroundColor.setR(0x6A);
foregroundColor.setG(0x5A);
foregroundColor.setB(0xCD);
// specify Background color as AntiqueWhite in Hex code
Color backgroundColor = new Color();
backgroundColor.setA(0x00);
backgroundColor.setR(0xFA);
backgroundColor.setG(0xEB);
backgroundColor.setB(0xD7);
// Create TextState object to define font size, face and color details
TextState textState = new TextState()
.fontSize(16.)
.foregroundColor(foregroundColor)
.backgroundColor(backgroundColor)
.font("Arial");
// create an instance of TextHeader object
TextHeader headerObject = new TextHeader()
.leftMargin(1.)
.rightMargin(200.)
.topMargin(10.)
// the content of Text header to be rendered
.value("Aspose.PDF Cloud SDK For Java")
// set alignment as center
.textAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.CENTER)
.textState(textState);
// set Header Text behind page content
headerObject.background(true)
// set Text header alignment as Center
.horizontalAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.CENTER)
// set opacity of Text Header as 0.8
.opacity(0.8)
.rotate(com.aspose.asposecloudpdf.model.Rotation.NONE)
.rotateAngle(0.)
.xindent(2.0)
.yindent(5.0)
.zoom(1.0);
// call API method to add Text Header to PDF
AsposeResponse response = pdfApi.postDocumentTextHeader(sourcePDF, headerObject,startPage, endPage, null, null);
assertEquals(200, (int)response.getCode());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment