使用IText将文本写入PDF

时间:2018-03-16 07:01:07

标签: itext pdf-generation text-parsing itext7

我将文本以.txt格式写入PDF,

请在http://textuploader.com/dgtss

找到.txt文件

我正在尝试遍历 .txt 文件的内容,并以下面指定的格式在PDF中编写相同内容 enter image description here

以下是使用 IText v5.0.6

将数据写入PDF的代码
RowIterator ri = new RowIterator(myData);

            while (ri.hasNext()) {

                line = ri.next();
                prefix = line.substring(0, 10).trim();
                content = line.substring(10);


                if (prevPrefix == "" && newPage) {
                    while (ri.hasNext() && !matchHR) {
                        docHeader.add(content);
                        line = ri.next();
                        prefix = line.substring(0, 10).trim();
                        content = line.substring(10);
                        if (content.startsWith("_")) {
                            matchHR = true;
                        }
                    }
                } else if (newPage && pageHeader == "") {
                    pageHeader += content + "\n";
                    prefix = line.substring(0, 10).trim();
                    content = line.substring(10);
                    if (!content.startsWith("_")) {
                        pageHeader += content;
                        // Extract out details
                        procPeriod = content.substring(0, 30).trim();
                        distribName = content.substring(70).trim();
                        newPage = false;
                        continue;
                    }
                } else if (newPage) {
                    // Skip first line
                    if (ri.hasNext()) {
                        line = ri.next();
                        prefix = line.substring(0, 10).trim();
                        content = line.substring(10);
                    }
                }
                // Start of new page
                if (prevPrefix != "" && !prevPrefix.startsWith("HEAD") && prefix.startsWith("HEAD")) {
                    pages.add(buffer);
                    buffer = content;
                    newPage = true;
                } else {
                    buffer += "\n" + content;
                    newPage = false;
                }

                prevPrefix = prefix;
            }


            pages.add(buffer);


            Document document = new Document(PageSize.A4, 20f, 10f, 100f, 0f);


            writer = PdfWriter.getInstance(document, baosPDF);

            Rectangle headerBox = new Rectangle(36, 54, 559, 788);

            HeaderFooter event = new HeaderFooter();
            writer.setBoxSize("headerBox", headerBox);
            writer.setPageEvent(event);

            ParagraphBorder border = new ParagraphBorder();
            writer.setPageEvent(border);

            document.open();
            Font titleFont = new Font(Font.FontFamily.COURIER, 4.6f, Font.NORMAL);
            Font pageDetailsFont = new Font(Font.FontFamily.COURIER, 7, Font.BOLD);

            PdfContentByte cb = writer.getDirectContent();


            for(String page : pages) {
                document.newPage();

                border.setActive(false);
                document.add(new Paragraph((10.0f),"Page : "+(i+1),pageDetailsFont));
                document.add( Chunk.NEWLINE );

                border.setActive(false);
                document.add(new Paragraph((10.0f),"Country : "+country.toUpperCase(),pageDetailsFont));

                document.add( Chunk.NEWLINE );
                document.add( Chunk.NEWLINE );
                document.add( Chunk.NEWLINE );
                document.add( Chunk.NEWLINE );
                document.add( Chunk.NEWLINE );

                border.setActive(true);
                Paragraph indent=new Paragraph((10.0f),page,titleFont);
                indent.setIndentationLeft(8);
                document.add(indent);
                i++;
            }

            document.close();

我能够将文本这样写入pdf而不是表格格式,我该如何实现呢?

我已经参考过此示例,但数据源是 .CSV 而不是 .txt 一个

https://developers.itextpdf.com/content/itext-7-jump-start-tutorial/chapter-3-using-renderers-and-event-handlers

0 个答案:

没有答案