Free Support Forum - aspose.com

Logo not appear in the first page of pdf but appearing in other pages?

I am trying to generate a PDF from excel template. When i open the excel template and simply save it and generate the PDF than the logo appears in first page of PDF. But without opening the same template when I try to create PDF then logo doesn’t appear in first page.

Steps to follow.

  1. Without opening the excel file attached here try to generate the PDF then, logo doesn’t appear in the first page of PDF.
  2. Open the template and simply save it and try to generate PDF then, logo appears in first page of PDF.

Code used:
FileInputStream io = new FileInputStream(new File(“niteshLogoCensusTest1234.xlsx”));
Workbook workbook = new Workbook(io);
String logo_url = “team-logo(80_80).png”;
FileInputStream inFile;
PageSetup pageSetup = workbook.getWorksheets().get(1).getPageSetup();
pageSetup.setHeader(2, “&G”);
inFile = new FileInputStream(logo_url);
byte[] binaryData;
binaryData = new byte[inFile.available()];
inFile.read(binaryData);
println “binaryData === $binaryData”
pageSetup.setHeaderPicture(2, binaryData);
workbook.save(“name.pdf”)

The resources used in above code are attached below in zip format.
resourceAspose.zip (48.3 KB)

@niteshregmi1234,

Thanks for the sample code and template file.

Your issue (logo (header picture) not appearing on the first page) is due to the fact that an option “Different first page” is set , see the screenshot for your reference:

https://i.imgur.com/l6Fh57R.png

if you need header picture should be printed on every page of the document, kindly do set this option to false. See the updated sample code (see the line in bold) for your reference:
e.g
Sample code:

FileInputStream io = new FileInputStream(new File(“niteshLogoCensusTest1234.xlsx”));
Workbook workbook = new Workbook(io);
String logo_url = “team-logo(80_80).png”;
FileInputStream inFile;
PageSetup pageSetup = workbook.getWorksheets().get(1).getPageSetup();
pageSetup.setHeader(2, “&G”);
inFile = new FileInputStream(logo_url);
byte[] binaryData;
binaryData = new byte[inFile.available()];
inFile.read(binaryData);
println “binaryData === $binaryData”
pageSetup.setHeaderPicture(2, binaryData);
pageSetup.setHFDiffFirst(false);
workbook.save(“name.pdf”);

Hope, this helps a bit

@Amjad_Sahi

Yes that worked.
Thanks…

@niteshregmi1234,

Good to know that your issue is sorted out by the suggested sample code. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

@Amjad_Sahi,

I am reopening this issue because while inserting pageSetup.setHFDiffFirst(false); in my code the logo appeared in my first page but my header description disappear from my first page. I want my header description in the first page only and my logo in all of the page. How can i do this? please suggest.

I have used bolow code to display my description in first page only :-

pageSetup.setHFDiffFirst(true)
pageSetup.setFirstPageHeader(0, pageSetup.getHeader(0) + “\n\n&“Lucida Sans,Regular”&8&K02-074” + “My Description for first page only”)

@niteshregmi1234,

Well, since your first page header is long so you have to set top margins for the page to show all the contents (header + body) accordingly. You have to write your code accordingly to cope with it. I have written/updated the following sample code to accomplish your task a bit. Please refer to it and you may update it further if you want:
e.g
Sample code:

FileInputStream io = new FileInputStream(new File("niteshLogoCensusTest1234.xlsx"));
		Workbook workbook = new Workbook(io);
		String logo_url = "team-logo(80_80).png";
		FileInputStream inFile;
		PageSetup pageSetup = workbook.getWorksheets().get(1).getPageSetup();
		pageSetup.setHeader(2, "&G");
		inFile = new FileInputStream(logo_url);
		byte[] binaryData;
		binaryData = new byte[inFile.available()];
		inFile.read(binaryData);
		System.out.println("println binaryData === $binaryData");
		pageSetup.setHeaderPicture(2, binaryData);
		pageSetup.setHFDiffFirst(true);
		pageSetup.setFirstPageHeader(0, pageSetup.getFirstPageHeader(0) + "\n\n&\"Lucida Sans,Regular\"&8&K02-074" + "My Description for first page only");
		pageSetup.setTopMargin(2.7);
		workbook.save("out1.pdf");

Hope, this helps a bit.

@Amjad_Sahi

It’s not working still the logo disappear from the first page. I have posted the code snippet for this:-

pageSetup.setHeaderPicture(2, binaryData);
pageSetup.setHeader(2, “&G”); //for showing logo in header in all pages
pageSetup.setHeader(0, “&“Tahoma”&12&K02-074&Kff0000&B” + “My report Name”) // for showing report name in header of all pages
pageSetup.setHFDiffFirst(true) //for showing description in first page only
pageSetup.setFirstPageHeader(0, pageSetup.getHeader(0) + “\n\n&“Lucida Sans,Regular”&8&K02-074” + “My Description for first page only”); //Description in first page only

Note: Report name, Description and Logo all should contain in the header.
The above code disappears the logo from the first page only. Although the logo is shown in other pages.I want my output to show logo in all pages and description in first page only in header. please suggest.

@niteshregmi1234,

Thanks for providing code segment and further details.

See the following updated sample code and try it for your reference. You can amend the code and add/update your desired code accordingly. It does set the header picture to first page in the output PDF file format:
e.g
Sample code:

FileInputStream io = new FileInputStream(new File("F:\\Files\\resourceAspose\\niteshLogoCensusTest1234.xlsx"));
        Workbook workbook = new Workbook(io);
        String logo_url = "F:\\Files\\resourceAspose\\team-logo(80_80).png";
        FileInputStream inFile;
        PageSetup pageSetup = workbook.getWorksheets().get(1).getPageSetup();
        pageSetup.setHeader(2, "&G");
        inFile = new FileInputStream(logo_url);
        byte[] binaryData;
        binaryData = new byte[inFile.available()];
        inFile.read(binaryData);
        System.out.println("println binaryData === $binaryData");
        pageSetup.setHeaderPicture(2, binaryData);
        pageSetup.setHFDiffFirst(true);
        pageSetup.setFirstPageHeader(0, pageSetup.getFirstPageHeader(0) + "\n\n&\"Lucida Sans,Regular\"&8&K02-074" + "My Description for first page only");
        pageSetup.setFirstPageHeader(2,"&G");
        pageSetup.setPicture(true, false, true, 2,binaryData);
        pageSetup.setTopMargin(2.7);
        workbook.save("F:\\Files\\resourceAspose\\out1.pdf");        

Since we found an issue with the output Excel file when adding header picture to first page in the output Excel/PDF file format. I found the output Excel file is corrupted. I used the template file provided by you with the above sample code with an additional line of code:

workbook.save(“F:\Files\resourceAspose\out2.xlsx”);

The culprit line is the following:
pageSetup.setFirstPageHeader(0, pageSetup.getFirstPageHeader(0) + "\n\n&\"Lucida Sans,Regular\"&8&K02-074" + "My Description for first page only");
I have logged a ticket with an id “CELLSJAVA-43057” for your issue. We will look into it soon.

Once we have an update on it, we will let you know.

Please note, since you are not saving to XLSX file format and only saving to PDF file format, so the above code works fine for your needs.

@niteshregmi1234,
We have investigated it more and found it not to be a bug. There is limitation that the max length of header/footer is 255.
We will remove additional data of header/footer in the next fix, but for your need, the header image in first page is not visible in MS Excel, so please reduce the size of header/footer.

@ahsaniqbalsidiqui @Amjad_Sahi

Yes i have modified amjad code in my project it worked. My logo is shown in my all pages and description in first page only.
@ahsaniqbalsidiqui thanks for the information.
@amjad Thanks for the working code.

I will notify you if any issue arises. But now my issue is fixed with above implementation. Thanks.

@niteshregmi1234,

Good to know that the suggested code segment works for your needs well. Feel free to contact us any time if you have further queries or comments, we will be happy to assist you soon.

@niteshregmi1234,
Please try our latest version/fix: Aspose.Cells for Java v19.11.7 and limit the size of header and footer.

aspose-cells-19.11.7.zip (6.7 MB)