【问题标题】:How to print footnote on the page that contains the text?如何在包含文本的页面上打印脚注?
【发布时间】:2019-03-18 09:57:07
【问题描述】:

我正在使用带有溢出且相对于我的 textField 拉伸的 textField 我想在 pageFooter 中添加一个脚注。

仅当页面包含我的 textField 或至少在页面包含页脚时显示页脚时,我才需要在 pageFooter 中打印文本。

例子:

我的 .jrxml 中有一个名为“Witnesseth”和“A 节”的部分,如下所示:

现在,这个见证可以很长,由于它的长度可以移动到下一页。

我有一个带有如下脚注的页脚:

目前它出现在每一页上,但我只希望它出现在已打印 Section A 部分的位置。

【问题讨论】:

  • 您是否尝试过在包含$F{myField}.contains("word") 信息的字段上进行表达式,您需要准确指定您需要什么,现在有点不清楚您实际要求的是什么。
  • 嗨@PetterFriberg,感谢您的回复。我刚刚更新了我的问题。关于您的回复,如果我这样做,恐怕它仍然会打印在每一页上。
  • 嗨,肯尼,好的,你喜欢一个脚注。其实 Stackoverflow 上没有很好的答案,类似于这个问题,stackoverflow.com/questions/27549193/…。您可能可以使用一些参数hack勾号,将映射添加到参数,设置一个值,如果值存在打印页脚,删除值。但是我不喜欢那个解决方案,我会研究它,如果有一个更动态的解决方案会很好。如果我想出一些好的东西,我会告诉你的。
  • 嗨@PetterFriberg,非常感谢您的回复。请让我知道这是否有解决方案。 =) 感谢您的回复。
  • @PetterFriberg 你能分享我那个参数黑客勾号以便我试试吗? =) 我什么时候可以知道价值的存在?我想在这个特定的页面上。我什么时候可以删除它的值?

标签: java jasper-reports footnotes


【解决方案1】:

如果您不愿意发布详细的结果(这也不简单),在 jasper-reports 中创建脚注有两个主要挑战:

  1. textField 溢出时将在哪个页面开始/结束?

    只有通过分组或使用不同的详细信息带,您才能轻松获得textField 将从哪个页面开始的信息。

  2. pageFooter 是固定大小的(不能动态缩放)

    您需要预先定义脚注的空间,在某些有限的情况下,您可以使用可以动态缩放的 groupFooter 带,但这些情况是有限的。

考虑到这一点,没有处理脚注的“终极”解决方案,我将展示一个简单案例场景中可以做什么的想法。

我们的限制是相对于 textField 的脚注将打开 textField 开始的页面,以及 pageFooter 最多只允许 3 个脚注 x 页(这是我预定义的空间)。请注意,它会 通过添加伪造来轻松在文本字段结束的页面上添加脚注 新细节带中的文本字段。

这个想法是使用一个简单的JRScriplet,它会在 textField 上的printWhenExpression 被调用时收集数据(它可能会被调用多次,所以我们需要处理这个问题)。在pageFooter,我们将请求收集到的数据来生成我们的脚注列表。

示例

JRScriplet,我们将调用addFootnote 添加注释并调用getFootnoteDatasource 打印我们当前的脚注。

public class FootnoteScriplet extends JRDefaultScriptlet {

    //Distinct ordered values (since jasper may call more then once)
    private TreeSet<String> footNotes;

    public FootnoteScriplet(){
        super();
        this.footNotes = new TreeSet<>();
    }   

    /**
     * Add a footnote 
     * @param footNote, string of footNote 
     * @return always <code>true</true> since we use in printWhenExpression
     */
    public boolean addFootnote(String footNote){
        this.footNotes.add(footNote);
        return true;
    }

    /**
     * Get the datasource for footNotes and clear for future use.
     * @return
     */
    public JRDataSource getFootnoteDatasource(){
        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(new ArrayList<>(footNotes));
        this.footNotes.clear();
        return ds;
    }
}

jrxml,用OnEmptyRecord数据源运行测试,结构是使用多个detail band,在textField的printWhenExpression(总是返回true)中调用我们的scriplet添加脚注然后使用一个jr:list 组件,用于在pageFooter 中显示收集的脚注。

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Footnotes" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" scriptletClass="my.package.FootnoteScriplet" uuid="b5b15f62-e36a-4c91-a871-ea43faa7d0af">
    <subDataset name="footNoteDS" uuid="884dba42-5c44-4049-a50a-b7e13cc47607">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="_THIS" class="java.lang.String"/>
    </subDataset>
    <parameter name="TEXT" class="java.lang.String">
        <defaultValueExpression><![CDATA["Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <group name="TEXT2" footerPosition="CollateAtBottom">
        <groupExpression><![CDATA["2"]]></groupExpression>
    </group>
    <detail>
        <band height="50">
            <textField isStretchWithOverflow="true">
                <reportElement x="0" y="0" width="550" height="16" isRemoveLineWhenBlank="true" uuid="8def1808-b5c0-45b6-943f-8a88ec04f02b">
                    <printWhenExpression><![CDATA[$P{REPORT_SCRIPTLET}.addFootnote("Footnote 1")]]></printWhenExpression>
                </reportElement>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA["(1)" + $P{TEXT} + "\n" + $P{TEXT}]]></textFieldExpression>
            </textField>
        </band>
        <band height="50">
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="0" y="0" width="550" height="16" uuid="b6faedf8-11d8-45c8-ac46-e3fb95106140">
                    <printWhenExpression><![CDATA[$P{REPORT_SCRIPTLET}.addFootnote("Footnote 2")]]></printWhenExpression>
                </reportElement>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA["(2)" + $P{TEXT} + "\n" + $P{TEXT}]]></textFieldExpression>
            </textField>
        </band>
        <band height="50">
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="0" y="0" width="550" height="16" uuid="2db72e84-3e07-4e38-b2bb-c172bbd30956">
                    <printWhenExpression><![CDATA[$P{REPORT_SCRIPTLET}.addFootnote("Footnote 3")]]></printWhenExpression>
                </reportElement>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA["(3)" + $P{TEXT}  + "\n" + $P{TEXT}]]></textFieldExpression>
            </textField>
        </band>
        <band height="50">
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="0" y="0" width="550" height="16" uuid="4e4b4f2f-4279-4c21-8f0d-62ba92760edd">
                    <printWhenExpression><![CDATA[$P{REPORT_SCRIPTLET}.addFootnote("Footnote 4")]]></printWhenExpression>
                </reportElement>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA["(4)" + $P{TEXT}  + "\n" + $P{TEXT}]]></textFieldExpression>
            </textField>
        </band>
        <band height="50">
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="0" y="0" width="550" height="16" uuid="5552de59-29f3-49e7-87aa-ee75b811739d">
                    <property name="footNote" value="&quot;FootNote 5&quot;"/>
                    <printWhenExpression><![CDATA[$P{REPORT_SCRIPTLET}.addFootnote("Footnote 5")]]></printWhenExpression>
                </reportElement>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA["(5)"  + $P{TEXT}  + "\n" + $P{TEXT}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <pageFooter>
        <band height="64">
            <line>
                <reportElement x="13" y="5" width="534" height="1" uuid="c5b242d5-cafa-43ed-9536-391a4728edf6"/>
                <graphicElement>
                    <pen lineWidth="0.25" lineStyle="Solid"/>
                </graphicElement>
            </line>
            <componentElement>
                <reportElement positionType="Float" x="11" y="10" width="470" height="16" uuid="08bbac11-4f61-4858-8d82-639875dfe1c7"/>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="footNoteDS" uuid="c10d3aaf-8109-4f5b-8099-40450ea9ad7e">
                        <dataSourceExpression><![CDATA[$P{REPORT_SCRIPTLET}.getFootnoteDatasource()]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="16" width="470">
                        <textField>
                            <reportElement x="0" y="0" width="470" height="16" uuid="d6b5f278-02eb-43d0-934e-5282d37950f5"/>
                            <textElement verticalAlignment="Middle"/>
                            <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
            <textField>
                <reportElement positionType="FixRelativeToBottom" x="500" y="44" width="50" height="20" uuid="7e4a61be-7f67-4003-bd6e-1417cd77378b"/>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["P." + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
</jasperReport>

输出

这远非终极解决方案,而是希望展示与脚注相关的问题并给出如何处理它们的初步想法。

【讨论】:

  • 嗨@PetterFriberg,我已经尝试了您的解决方案,并且确实出现了一个脚注。这次真是万分感谢。但是,例如,当我强制溢出直到我的详细信息 2 创建一个新页面时,它仍然出现在它“应该在”的位置,但它实际上应该出现在下一页上。请帮助.. =)
  • @Kenny 在一个空的 textField 后面放了一个假的详细信息带,并在其中添加脚注(空的 textField 将在新页面上)
  • 我应该把假细节带放在哪里?在细节 2 之后还是在我希望它显示的细节之后?
  • @Kenny 使用新的详细信息带确保首先评估先前详细信息带上的文本,因此在文本之后如果溢出到新页面,您需要在新页面中添加脚注。
  • 好的。我会检查一下。非常感谢@Petter。但截至目前,我仍然无法让它工作。我会在 JasperSoft 周围玩一些..
【解决方案2】:

我通过关注this answer on SO. 修复了我的这个问题的版本

这可能是 Petter 所说的“参数技巧”,但它确实遵循脚注应该所在的页面,即使前一页溢出等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2012-01-13
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多