如何使用iText库5.5.2绘制了整个PDF页面边框 [英] How to draw border for whole pdf pages using iText library 5.5.2

查看:215
本文介绍了如何使用iText库5.5.2绘制了整个PDF页面边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所提到的,如何绘制边框红颜色,宽度冲程5使用iText库所有生成的PDF页面。我已经尝试了一些codeS,但没有得到结果。

As in title mentioned, how to draw border with "RED" color , width-stroke 5 for all generated pdf pages using iText library. I've tried some codes but got no result.

(1)

                    PdfPTable table = new PdfPTable(1);
                    table.setWidthPercentage(99);
                    table.setLockedWidth(true);

                    PdfPCell cell = new PdfPCell();
                    cell.setFixedHeight(PageSize.A4.getHeight());


                    document.add(table);

(2)

                    PdfContentByte content = PdfWriter.getInstance(document, fout).getDirectContent();
                    Rectangle pageRect = document.getPageSize();

                    pageRect.setLeft(pageRect.getLeft() + 10);
                    pageRect.setRight(pageRect.getRight() - 10);
                    pageRect.setTop(pageRect.getTop() - 10);
                    pageRect.setBottom(pageRect.getBottom() +10);

                    content.setColorStroke( BaseColor.BLUE);
                    content.rectangle(pageRect.getLeft(), pageRect.getBottom(), pageRect.getWidth(), pageRect.getHeight());
                    content.setLineWidth(10);
                    content.stroke();
                    content.fillStroke();

这些方法让我没有结果,谢谢!

Those methods give me no result, Thanks!

我已经改变了我的方法也感谢布鲁诺Lowagie他的回应。该示例工作就像一个魅力,但我不能将它列入我的code。

I've changed my methods also thanks to Bruno Lowagie for his respond. the example worked like a charm but i couldn't fit it into my code.

下面是我的code:通过pressing一个按钮,PDF文件将生成在specif地址。稍后我将添加更多的内容,但现在,让我们坚持到生成的PDF文件(S)。

Here's my code: By pressing a button, PDF file will generate at specif address. I'll add more content later but now let's stick to generating pdf file(s).

   SaveToSD = (Button)findViewById(R.id.SaveToMemoryCard_xml);
    SaveToSD.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View sssdd)
        {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ConcreteProject";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            Log.d("PDFCreator", "PDF Path: " + path);

            // Incremental Process of Creating File(s).
            String pdfName = "SDG_Created_pdf.pdf";
            int num = 0;
            File file = new File(dir, pdfName);
            while (file.exists()) {
                num++;
                pdfName = "SDG_Created_pdf" + num + ".pdf";
                file = new File(dir, pdfName);
            }


            try {

                new ConAccept_Result().createPdf(pdfName);


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }//End Of onClick(View sssdd).
    });

和这里是方法/类定义:

And Here is method/class definitions:

public class RedBorder extends PdfPageEventHelper {
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte canvas = writer.getDirectContent();
        Rectangle rect = document.getPageSize();
        rect.setBorder(Rectangle.BOX); // left, right, top, bottom border
        rect.setBorderWidth(5); // a width of 5 user units
        rect.setBorderColor(BaseColor.RED); // a red border
        rect.setUseVariableBorders(true); // the full width will be visible
        canvas.rectangle(rect);
    }
}

public void createPdf(String stringfile) throws IOException, DocumentException {
    // step 1
    com.itextpdf.text.Document document = new com.itextpdf.text.Document();

    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(stringfile));
    RedBorder event = new RedBorder();
    writer.setPageEvent(event);
    // step 3
    document.open();
    // step 4

    Chunk chunk = new Chunk("Lovin' iText - Lovin' iText");
    chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.3f, BaseColor.CYAN);
    document.add(chunk);

    // step 5
    document.close();
}

运行大程序的文件夹,因为我命名创建的,但没有PDF文件!以后

After running da app a folder as i named created but there is no PDF file !

多谢了。

推荐答案

读你的问题,似乎很明显,你需要一个页面事件。您尝试将添加一个边框只有一次,而你可能要一个边框添加到每个页面。

Reading your question, it seems obvious that you need a page event. Your attempts will add a border only once whereas you probably want to add a border to each page.

请看看在 PageBorder 的例子。在这个例子中,你会发现一个名为 PageEvents 接口的实现 RedBorder

Please take a look at the PageBorder example. In this example, you'll find an implementation of the PageEvents interface named RedBorder:

public class RedBorder extends PdfPageEventHelper {
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte canvas = writer.getDirectContent();
        Rectangle rect = document.getPageSize();
        rect.setBorder(Rectangle.BOX); // left, right, top, bottom border
        rect.setBorderWidth(5); // a width of 5 user units
        rect.setBorderColor(BaseColor.RED); // a red border
        rect.setUseVariableBorders(true); // the full width will be visible
        canvas.rectangle(rect);
    }
}

的OnEndPage()方法会自动每次触发,网页端(不要使用的OnStartPage( )方法添加内容)。

The onEndPage() method is triggered automatically, every time a page ends (do not use theonStartPage() method to add content).

在此方法的实现,我们要求文件对象的当前页面大小。需要注意的是传递给该事件的文件实例的类型是 PdfDocument 。它的不可以作为 createPdf使用的文档()方法。

In the implementation of this method, we ask the document object for its current page size. Note that the document instance passed to the event is of type PdfDocument. It is not the same document as used in the createPdf() method.

我们适应矩形我们的需求。我们设置边框 BOX 这意味着我们要添加边框的左侧,右侧,顶部和底部。我们定义的边界的宽度(在此情况下用户5单位)和我们定义的颜色。

We adapt the rectangle to our needs. We set the border to BOX meaning we want to add a border to the left, right, top and bottom. We define the width of the border (in this case 5 user units) and we define the color.

如果你停在那里,用5用户单位边框的矩形将被绘制,但你只能看到2.5用户单位线,因为5用户单位的另一半会的之外的可见区域的页面。

If you would stop there, a rectangle with a border of 5 user units would be drawn, but you would only see lines of 2.5 user units because the other half of the 5 user units would be outside the visible area of the page.

您可以避免这种通过10个用户单位的宽度,或通过设置的变量边界的标志

You can avoid this by using a width of 10 user units, or by setting the variable borders flag to true.

现在我们所要做的就是在矩形对象传递给矩形()方法。这个方法是从该方法与已使用在某种意义上说,它也笔画矩形相同的名称不同

Now all we have to do is to pass the rect object to the rectangle() method. This method is different from the method with the same name you have used in the sense that it also strokes the rectangle.

这篇关于如何使用iText库5.5.2绘制了整个PDF页面边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆