Text after Ampersand(&) not seen Issue while setting the page header

Hello Aspose family,
I encountered a problem with setting the page header text like ‘a&b’. The text after ‘&’ disappears in the pdf maybe because it is the reserved key in Aspose. My code looks like this:
String headerText='a&bcdefg’
pageSetup.setHeader(0, pageSetup.getHeader(0) + "\n&“Lucida Sans,Regular”&8&K02-
074&BPopulationss:&B "+headerText)

In this case, I get only ‘a’ in the header all other text after ampersand gets disappeared. Please suggest me with a solution for this. I am using aspose 18.2

@BenevolentHuman

Thank you for contacting support.

Would you please share a code snippet reproducing this issue, along with source and generated files so that we may try to reproduce and investigate it in our environment. Before sharing requested data, please ensure using Aspose.PDF for Java 18.8 in your environment.

@Farhan.Raza

PageSetup pageSetup = workbook.getWorksheets().get(worksheetName).getPageSetup();
 //calling the function
  setHeaderFooter(pageSetup, parameters, criteria)

 //function for setting header and footer
 def setHeaderFooter(PageSetup pageSetup, parameters, criteria = [:])
    {
       def selectedLoa=getSelectedLoa(parameters)
       if(selectedLoa.length()>110){
        String firstLine = selectedLoa.substring(0,110);
        String secondLine = selectedLoa.substring(110);
        if(secondLine.length()>120){
            secondLine = secondLine.substring(0,122)+"...."
        }
        selectedLoa = firstLine+"\n"+secondLine.trim();
    }
    def periodInfo=getPeriodInfo(parameters, criteria)
    def reportingInfo=periodInfo[0]
    def comparisonInfo=periodInfo[1]
    def benchmarkName=getBenchmark(parameters)
    def isNonComparison = criteria.isNonComparison?
    criteria.isNonComparison:false

    def footerInfo="&BReporting Period:&B " + reportingInfo+"\n"
    if (comparisonInfo && !isNonComparison){
        footerInfo=footerInfo+"&BComparison Period:&B " +comparisonInfo+"\n"
    }
    if (benchmarkName){
        footerInfo+="&BBenchmark:&B "+benchmarkName
    }
   //where I encounterd the issue,selectedLoa contains string with ampersand
    pageSetup.setHeader(0, pageSetup.getHeader(0) + "\n&\"Lucida Sans,Regular\"&8&K02-074&BPopulation:&B "+selectedLoa)

    //Insertion of footer
    pageSetup.setFooter(0,"&\"Lucida Sans,Regular\"&8&K02-074"+footerInfo)
    def downloadDate = new Date().format("MMMM dd, yyyy")
    pageSetup.setFooter(2,"&\"Lucida Sans,Regular\"&8&K02-074" + downloadDate)

    //Insertion of logo
    try{
        def bucketName = parameters.containsKey('printedRLBucketName')?parameters.get('printedRLBucketName'):null
        def filePath = parameters.containsKey('printedReportLogo')?parameters.get('printedReportLogo'): null

        // Declaring a byte array
        byte[] binaryData

        if(!filePath || filePath.contains("null") || filePath.endsWith("null")){
            filePath = root+"/images/defaultExportLogo.png"
            InputStream is = new FileInputStream(new File(filePath))
            binaryData = is.getBytes()
        }else {
            AmazonS3Client s3client = amazonClientService.getAmazonS3Client()
            S3Object object = s3client.getObject(bucketName, filePath)

            // Getting the bytes out of input stream of S3 object
            binaryData = object.getObjectContent().getBytes()
        }

        // Setting the logo/picture in the right section (2) of the page header
        pageSetup.setHeaderPicture(2, binaryData);

        // Setting the script for the logo/picture
        pageSetup.setHeader(2, "&G");

        // Scaling the picture to correct size
        Picture pic = pageSetup.getPicture(true, 2);
        pic.setLockAspectRatio(true)
        pic.setRelativeToOriginalPictureSize(true)
        pic.setHeight(35)
        pic.setWidth(Math.abs(pic.getWidth() * (pic.getHeightScale() / 100)).intValue());
    }catch (Exception e){
        e.printStackTrace()
    }
}

@BenevolentHuman,

Well, yes, “&” is a reserved char when inserting headers/footers in MS Excel spreadsheet via Aspose.Cells APIs. To cope with your issue, you got to place another ampersand to paste the “& (ampersand)” in the header string. See the sample code for your reference:
e.g
Sample code:

Workbook wb = new Workbook();

        Worksheet ws = wb.getWorksheets().get(0);
        ws.getCells().get("A1").putValue("testin..");

        String headerText="a&&bcdefg";
        PageSetup pageSetup = ws.getPageSetup();
        pageSetup.setHeader(0, headerText);

        wb.save("f:\\files\\out1.xlsx");
        wb.save("f:\\files\\out2.pdf");

Hope this helps a bit .

@Amjad_Sahi Thank you Amjad! it helped a bit. When the text is like ‘a&bcdefgh’, replacing the ‘&’ in text with ‘&&’ fixes the issue. But when the text is like ‘a&b&c&d&e’, i.e., when the text has multiple ampersands, it does not work. It only works for the first occurred ampersand. Can you also provide some light on this?
Sample code:
Workbook wb = new Workbook();

    Worksheet ws = wb.getWorksheets().get(0);
    ws.getCells().get("A1").putValue("testin..");

    String headerText="a&b&c&d&e&f&g"?.replace('&','&&');
    PageSetup pageSetup = ws.getPageSetup();
    pageSetup.setHeader(0, headerText);

    wb.save("f:\\files\\out1.xlsx");
    wb.save("f:\\files\\out2.pdf");

@BenevolentHuman,

Please try our latest version/fix (attached): Aspose.Cells for Java v18.8.5

I have tested your scenario/ case using the attached version and it works fine. The output MS Excel and PDF files has correct(complete) headers.
Aspose.Cells for Java v18.8.5.zip (6.3 MB)