j.jerrod.taylor
j.jerrod.taylor

Reputation: 1140

How do I write exactly one page of text with java?

In the project that I'm working on I'm working with a lot of text. I write code that takes text from two different languages and then it sentence aligns all of the text and then I write everything to an HTML file.

Here is example output:

<html>
    <head>
        <meta charset="utf-8"/>
    </head>
    <body>
    <table>
        <tr>
            <td>
                <p>
                    ALICE'S ADVENTURES IN WONDERLAND
                </p>
            </td>
            <td>
                <p>
                    Alice no País das Maravilhas
                </p>
             </td>
         </tr>
         <tr>
             <td>
                 <p>
                     Lewis Carroll
                 </p>
             </td>
             <td>
                 <p>
                     Lewis Carroll
                 </p>
             </td>
         </tr>

Now I want to get this converted to an ebook, but I have to separate the text into its respective pages but I want to make sure that the languages still line up. Is there a way for me to know when I've written one page of text so that I can switch and start writing text in the other language. I've been trying to do this programmatically and I haven't been able to figure it out.

Below is the code that I use to make the html file:

public String makePage(ArrayList<ArrayList<String>> content)
{
    this.openHtml()
            .openHead()
            .closeHead()
            .openBody()
            .openTable();

    for(int i=0;i<content.get(0).size(); i++)
    {
        this.openTR()
                .openTD()
                .openP()
                .addContent(content.get(0).get(i))
                .closeP()
                .closeTD()
                .openTD()
                .openP()
                .addContent(content.get(1).get(i))
                .closeP()
                .closeTD()
                .closeTR();
    }
    this.closeTable()
            .closeBody()
            .closeHtml();

    return this.page;
}

Below is the code that I use to write the file:

public void writeFile(String stringToWrite, String nameOfFile)
{
    this.bw = IOFactory.buildIOBufferedWriter(nameOfFile);
    try
    {
        this.bw.getBufferedWriter().write(stringToWrite);
        this.bw.getBufferedWriter().newLine();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            this.bw.getBufferedWriter().flush();
            this.bw.getBufferedWriter().close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 89

Answers (2)

Starscream1984
Starscream1984

Reputation: 3062

You are probably better off splitting the text into paragraphs which can layout nicely as two columns in a table. Then you can paste it into word and no matter what the page size, or formatting, the paragraphs and by extension the whole story will line up with the translation throughout the book.

Upvotes: 0

Manuel Selva
Manuel Selva

Reputation: 19050

The notion of page doesn't exist in a text file encoded in either ASCII or UTF8.

If you are working with an output format where the pages notion exists such as word, you'll have to know the internal of this output format to know when a page has been written. As @Starscream1984 said in a comment, in words for example this depends on formatting like margins and font size.

Upvotes: 1

Related Questions