EplanPrinter.RTFWrite.java Source code

Java tutorial

Introduction

Here is the source code for EplanPrinter.RTFWrite.java

Source

//     * This file is part of the EplanPrinter project.
//     * Copyright (c) 2016 e-PlanSoft
//     * Authors: e-PlanSoft Team
//     * Website: http://www.eplansoft.com/
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Affero General Public License as
//    published by the Free Software Foundation, either version 3 of the
//    License, or (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU Affero General Public License for more details.
//
//    You should have received a copy of the GNU Affero General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//    * For more information, please contact us at info@eplansoft.com

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package EplanPrinter;

import com.lowagie.text.DocumentException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

public class RTFWrite {
    Document document;
    String cg = "";
    String cc = "";
    int count = 1;

    public String openDoc(String name, String title) throws FileNotFoundException, DocumentException {
        document = new Document();
        RtfWriter2.getInstance(document, new FileOutputStream(name + ".rtf"));
        document.open();
        Paragraph p = new Paragraph("Permit " + title);
        p.setAlignment(Element.ALIGN_CENTER);
        document.add(p);
        return "";
    }

    public String addItem(String comment, String category, String group) throws DocumentException, IOException {
        if (cg.compareTo(group) != 0) {
            count = 1;
            cg = group;
            cc = category;

            Font groupContent = new Font();
            groupContent.setStyle("bold");
            groupContent.setStyle("underline");
            groupContent.setSize(12);
            Paragraph g = new Paragraph(group, groupContent);

            Font catContent = new Font();
            catContent.setSize(12);
            Paragraph c = new Paragraph(category, catContent);
            c.setIndentationLeft(30);
            document.add(g);
            document.add(c);
        } else if (cc.compareTo(category) != 0) {
            count = 1;
            cc = category;
            Paragraph c = new Paragraph(category);
            c.setIndentationLeft(30);
            document.add(c);
        }
        Paragraph p = new Paragraph();
        //document.add(p);
        String test = count + ". " + comment;
        test = test.replaceAll("<p>", " ");
        test = test.replaceAll("</p>", " ");

        if (test.indexOf("<ol>") != -1) {
            test = test.replaceAll("<ol>", "");
            test = test.replaceAll("</ol>", "");
            test = test.replaceAll("</li>", "<br />");
            int subCount = 1;
            while (test.indexOf("<li>") != -1) {
                test = test.replaceFirst("<li>", subCount + ". ");
                subCount++;
            }
            int marker = test.lastIndexOf("<br />");
            String sub1 = test.substring(0, marker);
            String sub2 = test.substring(marker);
            sub2 = sub2.replaceAll("<br />", "");
            test = sub1 + sub2;
        }
        if (test.indexOf("<ul>") != -1) {
            test = test.replaceAll("<ul>", "");
            test = test.replaceAll("</ul>", "");
            test = test.replaceAll("</li>", "<br />");
            int c = 149;
            char ch = (char) 149;
            test = test.replaceAll("<li>", "&bull;");
            int marker = test.lastIndexOf("<br />");
            String sub1 = test.substring(0, marker);
            String sub2 = test.substring(marker);
            sub2 = sub2.replaceAll("<br />", "");
            test = sub1 + sub2;
        }

        StringReader str = new StringReader(test);
        List<Element> e = HTMLWorker.parseToList(str, null);
        for (int k = 0; k < e.size(); ++k) {
            p.add((com.lowagie.text.Element) e.get(k));
        }

        p.setIndentationLeft(40);
        p.setFirstLineIndent(30);
        document.add(p);

        count = count + 1;

        return "";
    }

    public String closeDoc() {
        document.close();

        return "";
    }

    public static void main(String[] args) throws Exception {

        RTFWrite r = new RTFWrite();

        r.openDoc("test", "453");
        r.addItem("<p>Test</p>", "InTop", "Top");
        r.addItem("<p>  <strong>test tags</strong></p> <p>  &nbsp;</p> <p>  <strong>test tags</strong></p>",
                "InTop", "Top");
        r.addItem("<ul>  <li>   test1</li>  <li>   test3</li>  <li>   test4</li> </ul>", "InBottom", "Bottom");
        r.addItem("<ol>  <li>   test1</li>  <li>   test3</li>  <li>   test4</li> </ol>", "InBottom", "Bottom");
        r.closeDoc();

    }
}