Aspose:在设置页眉时未看到&符号后的文本

问题描述 投票:0回答:2

我在设置包含&符号的页面标题文本时遇到了问题,例如'a&b'。 “&”之后的文本在pdf中消失可能是因为它是Aspose中的保留键。我的代码看起来像这样:

 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()
    }
}

在这种情况下,在&符号消失之后,我在pdf标题中只得到'a'所有其他文本。请建议我解决此问题。我正在使用aspose 18.2

grails groovy header aspose aspose.pdf
2个回答
0
投票

我们在PDF页面上添加了标题,下面是代码段,但是当标题文字中包含&符号时我们没有注意到任何问题。

// open document
Document document = new Document(dataDir + "input.pdf");
// create text stamp
TextStamp textStamp = new TextStamp("a&bcdefg");
// set properties of the stamp
textStamp.setTopMargin(10);
textStamp.setHorizontalAlignment(HorizontalAlignment.Center);
textStamp.setVerticalAlignment(VerticalAlignment.Top);

// set text properties
textStamp.getTextState().setFont(new FontRepository().findFont("Arial"));
textStamp.getTextState().setFontSize(14.0F);
textStamp.getTextState().setFontStyle(FontStyles.Bold);
textStamp.getTextState().setFontStyle(FontStyles.Italic);
textStamp.getTextState().setForegroundColor(Color.getGreen());

// iterate through all pages of PDF file
for (int Page_counter = 1; Page_counter <= document.getPages().size(); Page_counter++) {
        // add stamp to all pages of PDF file
        document.getPages().get_Item(Page_counter).addStamp(textStamp);
}
// save output document
document.save(dataDir + "TextStamp_18.8.pdf");

请确保在您的环境中使用Aspose.PDF for Java 18.8。有关添加页眉的更多信息,请访问Add Text Stamp in the Header or Footer section

如果您在添加标题时遇到任何问题,请通过Google云端硬盘,Dropbox等与我们分享您的代码段和生成的PDF文档,以便我们进行调查以帮助您解决问题。

PS:我和Aspose一起担任开发者传播者。


0
投票

嗯,是的,当通过Aspose.Cells API在MS Excel电子表格中插入页眉/页脚时,“&”是一个保留字。为了解决您的问题,您必须放置另一个&符号以在标题字符串中粘贴“&(&符号)”。请参阅示例代码以供参考:例如示例代码:

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");

希望这个对你有帮助。

我是Aspose的支持开发人员/传播者。

© www.soinside.com 2019 - 2024. All rights reserved.