迭代页脚中未显示的总页数

时间:2017-07-06 09:25:30

标签: android itext pdf-generation

我正在使用itext在我的应用程序中生成PDF。在页脚部分中,包含Y的页面X的短语存在。创建PDF后,页面总数未显示。请参考以下代码:

MainActivity中的

public partial class FilterControl : UserControl
{
    public delegate void FilterChangedHandler(object sender);
    public event FilterChangedHandler OnFilterChanged;

    public ObservableCollection<FilterValue> AvailableFilters { get; set; }
    public List<string> AppliedFilters
    {
        get
        {
            return new List<string>(AvailableFilters.Where(filter => filter.IsChecked).Select(filter => filter.Text));
        }
    }

    public FilterControl()
    {
        InitializeComponent();

        AvailableFilters = new ObservableCollection<FilterValue>();
    }

    public bool AddFilterValue(string filterValue)
    {
        bool found = false;

        foreach (FilterValue f in AvailableFilters)
        {
            if (f.Text == filterValue)
            {
                found = true;
                break;
            }
        }

        if (!found)
            AvailableFilters.Add(new FilterValue() { IsChecked = false, Text = filterValue });

        return found;
    }

    private void FilterChanged(object sender, RoutedEventArgs e)
    {
        //Here if I check AvailableFilters, the value is not changed yet.
        //PropertyChanged allways fires after this, what makes me unable to
        //get all currently applied filters (checked items)...
    }
}

在HeaderFooterPageEvent类中:

public class FilterValue : INotifyPropertyChanged
{
    private bool _IsChecked;
    private string _Text;

    public bool IsChecked
    {
        get { return _IsChecked; }
        set
        {
            _IsChecked = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsChecked"));
        }
    }
    public string Text
    {
        get { return _Text; }
        set
        {
            _Text = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Text"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

它只显示“Page x of”而不是“Page X of Y”。有什么我想念的吗?请帮忙。

1 个答案:

答案 0 :(得分:1)

您已经实施了onOpenDocument方法来创建尺寸为30,16的PdfTemplate(不是很小吗?)并且您要添加此占位符在onEndPage方法的每个页面上。

但是,我没有看到您在任何地方向PdfTemplate添加内容。如果您没有添加总页数,那么您将无法查看文档中任何位置的总页数。

由于您只能知道关闭文档时的总页数,因此需要实现onCloseDocument()

public void onCloseDocument(PdfWriter writer, Document document) {
    ColumnText.showTextAligned(total, Element.ALIGN_LEFT,
            new Phrase(String.valueOf(writer.getPageNumber() - 1)),
            2, 2, 0);
}

有关完整示例,请参阅MovieCountries1。这个例子是在本书第二版&#34; iText in Action中编写的。&#34;