如何在JTextPane页脚中打印页数?

时间:2021-08-31 12:05:38

I have searched high and low for this answer and have come up blank.

我到处寻找这个答案,却一无所获。

I have a requirement to print the contents of a JTextPane with a footer that says "Page <n> of <m> pages". It appears to impossible to do this simple function in Java.

我需要打印JTextPane的内容,它的页脚写着“Page of pages”。在Java中似乎不可能实现这个简单的功能。

I can set the footer to print the page numbers when I get the Printable, e.g.

当我得到可打印的页码时,我可以设置页脚来打印页码。


     String header = "whatever";
     String footer = " Page - {0}";
     printText(textPane.getPrintable(new MessageFormat(header), 
        new MessageFormat(footer)));

But there appears to be no way to find the total number of pages that will be printed until after the printer dialog box is dispatched. I assume that is because that dialog is used to format the pages before they are sent to the printer. The printer dialog box always says that there is only 1 (one) page.

但是似乎没有办法找到打印到打印机对话框发送之后的页面总数。我认为这是因为在将页面发送到打印机之前,使用该对话框对页面进行格式化。打印机对话框总是说只有一个页面。

So, I began to write a routine that will go through the JTextPane document and count the pages by getting the viewable area from the PageFormat in the print() method and then using the height of each line (fontsize) to count the lines in each page, and subsequently count the number of pages.e.g.

所以,我开始编写一个程序,将穿过JTextPane文档和计算页面的可视面积从PageFormat print()方法,然后使用每一行的高度(字形大小)在每个页面数行,随后数pages.e.g的数量。


      int maxh = (int) pf.getImageableHeight ();
      Element section = doc.getDefaultRootElement();
      for (int i=0; i<paraCount; i++) 
      {
        Element e = section.getElement(i);
        // Get the attributeSet for this paragraph (Element)
        // The attributeSet contains the Styles, which contain
        // the fonts etc.
        AttributeSet attrs = e.getAttributes();
        int fontsize = StyleConstants.getFontSize(attrs);

        // .... add up the lines and count filled pages ...
      }

However, the PageFormat is not available until the system calls back into the print() method, and by the time it gets to the print() method, it appears to be impossible to modify the footer since the header and footer are both defined as "final" in the method call:

但是,PageFormat在系统调用print()方法之前是不可用的,当它到达print()方法时,似乎不可能修改页脚,因为页脚和页脚在方法调用中都被定义为“final”:



       public Printable getPrintable(final MessageFormat headerFormat,
                                      final MessageFormat footerFormat)

Surely somebody has found a simple way to do this basic function.

肯定有人找到了一种简单的方法来实现这个基本功能。

Thanks in advance for any who can help.

提前感谢任何能帮忙的人。

2 个解决方案

#1


2  

I'm not sure if the links below to articles about pagination and printing of the JEditorPane/JTextPane will do the trick for you, but surely they did a lot for me:

我不确定下面有关JEditorPane/JTextPane的分页和打印文章的链接是否会对您有所帮助,但他们肯定为我做了很多:

  1. Pagination in the JEditorPane Component. Part I.
  2. 在JEditorPane组件中的分页。我一部分。
  3. Pagination in the JEditorPane Component. Part II (Printing).
  4. 在JEditorPane组件中的分页。第二部分(印刷)。
  5. Pagination in the JEditorPane Component. Part III (Headers and footers).
  6. 在JEditorPane组件中的分页。第三部分(页眉和页脚)。
  7. Pagination in the JEditorPane Component. Part IV (Page Breaks).
  8. 在JEditorPane组件中的分页。第四部分(分页符)。

#2


1  

It seems there is no "direct" way to accomplish this. The tutorial on JTextComponent says the following:

似乎没有“直接”的方法来实现这个目标。关于JTextComponent的教程说:

Since the total number of pages in the output is not known before printing time, there is no way to specify a numbering format like "Page 1 of 5".

由于在打印时间之前不知道输出中的页面总数,因此无法指定“5的第1页”之类的编号格式。

#1


2  

I'm not sure if the links below to articles about pagination and printing of the JEditorPane/JTextPane will do the trick for you, but surely they did a lot for me:

我不确定下面有关JEditorPane/JTextPane的分页和打印文章的链接是否会对您有所帮助,但他们肯定为我做了很多:

  1. Pagination in the JEditorPane Component. Part I.
  2. 在JEditorPane组件中的分页。我一部分。
  3. Pagination in the JEditorPane Component. Part II (Printing).
  4. 在JEditorPane组件中的分页。第二部分(印刷)。
  5. Pagination in the JEditorPane Component. Part III (Headers and footers).
  6. 在JEditorPane组件中的分页。第三部分(页眉和页脚)。
  7. Pagination in the JEditorPane Component. Part IV (Page Breaks).
  8. 在JEditorPane组件中的分页。第四部分(分页符)。

#2


1  

It seems there is no "direct" way to accomplish this. The tutorial on JTextComponent says the following:

似乎没有“直接”的方法来实现这个目标。关于JTextComponent的教程说:

Since the total number of pages in the output is not known before printing time, there is no way to specify a numbering format like "Page 1 of 5".

由于在打印时间之前不知道输出中的页面总数,因此无法指定“5的第1页”之类的编号格式。