We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Different Header Image for FirstPage


i would like to add an image to the header of my document, but on the first page there should be a different one.

byte[] binaryData1;
var headerFile= new System.IO.FileStream(@“C:\Pictures\header.PNG”, System.IO.FileMode.Open, System.IO.FileAccess.Read);
binaryData1 = new Byte[headerFile.Length];
long bytesRead = headerFile.Read(binaryData1, 0, (int)headerFile.Length);
pageSetup.SetHeaderPicture(1, binaryData1);
pageSetup.SetHeader(1, “&G”);
pageSetup.IsHFDiffFirst = true;
pageSetup.IsHFAlignMargins = true;
pageSetup.IsHFScaleWithDoc = true;

byte[] binaryData2;
var firstPageHeaderFile = new System.IO.FileStream(@“C:\Pictures\firstpageheader.PNG”, System.IO.FileMode.Open, System.IO.FileAccess.Read);
binaryData2 = new Byte[firstPageHeaderFile.Length];
bytesRead = firstPageHeaderFile.Read(binaryData2, 0, (int)firstPageHeaderFile.Length);
pageSetup.SetHeaderPicture(1,binaryData2 );

Unfortunately there is no image on the first page in the header section? I do not know how to achieve this requirement, so maybe someone could help?!

Hi Tobias,

Thank you for using Aspose products, and welcome to Aspose.Cells support forum.

You can insert the image in the header of only first page using the code snippet as below,


//Creating a Workbook object
Workbook workbook = new Workbook(myDir + “book1.xls”);

//Creating a string variable to store the url of the logo/picture
string logo_url = myDir + “logo2.png”;

//Declaring a FileStream object
FileStream inFile;

//Declaring a byte array
byte[] binaryData;

//Creating the instance of the FileStream object to open the logo/picture in the stream
inFile = new System.IO.FileStream(logo_url, System.IO.FileMode.Open, System.IO.FileAccess.Read);

//Instantiating the byte array of FileStream object’s size
binaryData = new Byte[inFile.Length];

//Reads a block of bytes from the stream and writes data in a given buffer of byte array.
long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);

//Creating a PageSetup object to get the page settings of the first worksheet of the workbook
PageSetup pageSetup = workbook.Worksheets[0].PageSetup;

//Setting the script for the logo/picture
pageSetup.SetFirstPageHeader(0, “&G”);

//Setting IsHFDiffFirst to true;
//this property should be true if you want to have different headers for the first page
pageSetup.IsHFDiffFirst = true;

//Setting the logo/picture in the first section of the page header
pageSetup.SetPicture(true, false, true, 0, binaryData);

//Saving the workbook
workbook.Save(myDir + “headerpic.xls”);

Please let us know if we can be of further assistance to you.

Thank you.

I just had to download the latest version of Aspose.Cells as I have been using version and therefore the SetPicture-Method did not exist!

Hi Tobias,

We didn’t know you are using an older version of Aspose.Cells for .NET API in which PageSetup.SetPicture method was not available. I believe you should be now getting the desired results with the latest build of Aspose.Cells for .NET 8.0.0. Please feel free to write back in case you need our further assistance.