使用 xhtmlrenderer (iText) 的书签

发布于 2024-09-09 12:24:59 字数 100 浏览 3 评论 0原文

我使用 xhtmlrenderer(也称为 Flying Saucer)和 iText 将 HTML 转换为 PDF。我如何用它创建书签?有人有一个小例子吗?

提前致谢。

I'm using xhtmlrenderer (Also known as Flying Saucer) with iText to convert HTML to PDF. How would I create bookmarks with this? Does someone with have a small example?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ左 2024-09-16 12:25:06

上述方法有效是正确的。

为书签演示修改的 Flyingsaucer-R8-src 示例

public class PDFRenderToMultiplePages {
    public static void main(String[] args) throws Exception {
        OutputStream os = null;
        try {
            final String[] inputs = new String[] { newPageHtml(1, "red"),
                    newPageHtml(2, "blue"), newPageHtml(3, "green") };
            final File outputFile = File.createTempFile("FlyingSacuer", ".pdf");
            os = new FileOutputStream(outputFile);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(inputs[0]);
            renderer.layout();
            renderer.createPDF(os, false);
            BlockBox rootBox = renderer.getRootBox();
            for (int i = 1; i < inputs.length; i++) {
                renderer.setDocumentFromString(inputs[i]);
                renderer.layout();
                renderer.writeNextDocument();
            }
            renderer.finishPDF();
            System.out.println("Sample file with " + inputs.length
                    + " documents rendered as PDF to " + outputFile);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) { /* ignore */
                }
            }
        }
    }

    private static String newPageHtml(int pageNo, String color) {
        return "<html><head><bookmarks>"
                + "<bookmark name=\"bookmark"
                + pageNo
                + "\" href=\"#bookMark\"/></bookmarks></head>"
                + "<body><div style=\"color:"
                + color
                + ""
                + ";\"><a name=\"bookMark\">Book Mark Example</a></div></body></html>";
    }
}

Its correct that above approach will work.

Sample example from flyingsaucer-R8-src modified for BookMarks Demonstration

public class PDFRenderToMultiplePages {
    public static void main(String[] args) throws Exception {
        OutputStream os = null;
        try {
            final String[] inputs = new String[] { newPageHtml(1, "red"),
                    newPageHtml(2, "blue"), newPageHtml(3, "green") };
            final File outputFile = File.createTempFile("FlyingSacuer", ".pdf");
            os = new FileOutputStream(outputFile);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(inputs[0]);
            renderer.layout();
            renderer.createPDF(os, false);
            BlockBox rootBox = renderer.getRootBox();
            for (int i = 1; i < inputs.length; i++) {
                renderer.setDocumentFromString(inputs[i]);
                renderer.layout();
                renderer.writeNextDocument();
            }
            renderer.finishPDF();
            System.out.println("Sample file with " + inputs.length
                    + " documents rendered as PDF to " + outputFile);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) { /* ignore */
                }
            }
        }
    }

    private static String newPageHtml(int pageNo, String color) {
        return "<html><head><bookmarks>"
                + "<bookmark name=\"bookmark"
                + pageNo
                + "\" href=\"#bookMark\"/></bookmarks></head>"
                + "<body><div style=\"color:"
                + color
                + ""
                + ";\"><a name=\"bookMark\">Book Mark Example</a></div></body></html>";
    }
}
安静被遗忘 2024-09-16 12:25:04

R6中似乎添加了书签,但用户指南仅提供了一个临时参考。向作者提供 TODO 以便稍后添加示例。

搜索论坛,我发现这个例子

<html> 
<head>  
<bookmarks>  
  <bookmark name="A bookmark" href="#bm" />  
  <bookmark name="A bookmark 2" href="#bm2" />  
  <bookmark name="A bookmark 3" href="#bm3" />    
  <bookmark name="A bookmark 4" href="#bm4" />  
  <bookmark name="A bookmark invalid" href="#bm99" />  
</bookmarks> 
</head> 
<body>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
      <a name="bm">some text</a>  
   </div>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
      <a name="bm2">some text</a>  
   </div>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
     <a name="bm3">some text</a>  
   </div>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
      <p>some text</p>  <p>some text</p>  <p>some text</p>  <p>some text</p>  <p>some text</p>  <p>some text</p>  
      <p><a name="bm4">and some more text</a></p>  
   </div> 
</body> 
</html>` 

所以看来添加书签只不过是在中声明书签 并将它们作为 中的锚点引用。

这应该适用于您现有的 XHTML->PDF 转换,而不需要任何代码更改。

It seems bookmarks were added in R6, but the user guide only gives them a passing reference. with a TODO to the author to add an example later.

Searching the forums, I found this example:

<html> 
<head>  
<bookmarks>  
  <bookmark name="A bookmark" href="#bm" />  
  <bookmark name="A bookmark 2" href="#bm2" />  
  <bookmark name="A bookmark 3" href="#bm3" />    
  <bookmark name="A bookmark 4" href="#bm4" />  
  <bookmark name="A bookmark invalid" href="#bm99" />  
</bookmarks> 
</head> 
<body>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
      <a name="bm">some text</a>  
   </div>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
      <a name="bm2">some text</a>  
   </div>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
     <a name="bm3">some text</a>  
   </div>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
      <p>some text</p>  <p>some text</p>  <p>some text</p>  <p>some text</p>  <p>some text</p>  <p>some text</p>  
      <p><a name="bm4">and some more text</a></p>  
   </div> 
</body> 
</html>` 

So it seems that adding bookmarks is no more than declaring the bookmarks in the <head> and referencing them as anchors in the <body>.

This should work will with your existing XHTML->PDF conversion without requiring any code changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文