【问题标题】:iText Flying Saucer pdf headers and ignoring htmliText Flying Saucer pdf标题并忽略html
【发布时间】:2011-07-08 13:48:31
【问题描述】:

我们使用 xhtml 转换为 pdf 取得了很大的成功,但是提出了一个新要求,即在每一页上都有页眉和页数。我们正在使用 Flying Saucer 的新版本。

我在这里遵循了示例:http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features

...但这行不通。标题将在第一页的左上角。

如果我使用 r7 版本,页眉和页码可以完美运行,但不会呈现传入的 html,而在 r8 中会忽略页眉\页码,但会完美呈现 html。用于测试的 xHTML 是从上面的 url 复制而来的。

我知道我一定遗漏了一些非常简单的东西,如果有人有任何想法\ cmets,我将非常感激。

【问题讨论】:

    标签: java flying-saucer


    【解决方案1】:

    我认为他们在 r8 中更改了此功能......改用此方法:

    https://gist.github.com/626264

    【讨论】:

      【解决方案2】:

      我们使用相同的方法,一切正常,但是我决定不使用飞碟内置的页眉/页脚,并在生成 PDF 后使用PdfStamper 添加它们,效果很好,这里是一个例子。

          public void modifyPdf(PdfStamper stamper) {
              this.reader = stamper.getReader();
      
              PdfContentByte under = null;
      
              PdfPTable header = null;
              PdfPTable footer = null;
      
              final int total = this.reader.getNumberOfPages();
              for (int page = 1; page <= total; page++) {
                  under = stamper.getUnderContent(page);
      
                  final PdfDocument doc = under.getPdfDocument();
                  final Rectangle rect = this.reader.getPageSizeWithRotation(page);
      
                  header = ... //build your header
                  footer = ... // build your footer
      
                  final float x = 0;
      
                  //write header to PDF
                  if (header != null) {
                      float y = (rect.getTop() - 0);
                      header.writeSelectedRows(0, -1, x, y, under);
                  }
      
                  //write footer to PDF
                  if (footer != null) {
                      float y = (rect.getBottom() + 20);
                      footer.writeSelectedRows(0, -1, x, y, under);
                  }
              }
          }
      

      您可以像这样构建您的压模:

              final PdfReader reader = new PdfReader(/*your pdf file*/);
              final PdfStamper stamper = new PdfStamper(reader, /* output */);
      

      希望对您有所帮助。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 2015-06-29
      • 2023-03-03
      • 2011-11-17
      • 2023-03-14
      • 1970-01-01
      • 2011-10-23
      相关资源
      最近更新 更多