Ian,
I think there is another problem. I looked at the source code for
addPageBreak() and it puts the page break before the reference paragraph.
Here is the work around I found to add a page break followed by a header as
the first row of the new page:
private TextDocument odtFile; // instantiated by the class
private void addNewPage(String text, int level) {
// Add an empty paragraph on the previous page to give the page break
// something to reference
Paragraph p0 = odtFile.addParagraph("");
try {
OdfContentDom dom = odtFile.getContentDom();
OdfOfficeAutomaticStyles styles = dom.getAutomaticStyles();
OdfStyle style = styles.newStyle(OdfStyleFamily.Paragraph);
// set break after paragraph not before
style.newStyleParagraphPropertiesElement().setFoBreakAfterAttribute("page");
p0.getOdfElement().setStyleName(style.getStyleNameAttribute());
// Now that we've added a page break we can add the header paragraph
Paragraph p1 = odtFile.addParagraph(text);
p1.setTextContent(text);
if (level > 0) {
p1.applyHeading(true, level);
}
} catch (Exception ex) {
Logger.getLogger(EMCReportGenerator.class.getName()).log(Level.SEVERE,
null, ex);
}
}
I think if addPageBreak() used setFoBreakAfterAttribute instead of
setFoBreakBeforeAttribute, then the following Simple API code would work:
odtFile.addPageBreak();
odtFile.addParagraph(text).applyHeading(true, level);
much nicer.
Thanks for the help,
Alan
On Sun, Aug 25, 2013 at 5:35 AM, Ian C <[email protected]> wrote:
> I have a work around for the header issue.
>
> It does seam that the Simple API is not working correctly?
>
> By default a header has a style name of "Heading_20_1". The last number
> reflects the level.
>
> So try
>
> header.applyHeading();
> TextParagraphElementBase element = header.getOdfElement();
> element.setStyleName("Heading_20_1");
>
> Then the style name will be set and it should look like a header.
>
> However, something is wrong you should not have to resort to using the
> underlying ODF Element.
> Calling header.setStyleName("Heading_20_1"); does not seem to work either.
>
> Copying the Dev group, we should probably raise a bug report on this,
> unless we are doing something wrong.
>
>
>
> On Sun, Aug 25, 2013 at 3:25 PM, Ian C <[email protected]> wrote:
>
> > Hi,
> >
> > I believe what is happening is...
> >
> > create new document - does so with an empty paragraph.
> > addParagraph - appends after the empty.
> >
> > So you can do as above.
> >
> > Or I have...
> >
> > TextDocument reportDoc = TextDocument.newTextDocument();
> > Paragraph firstEmpty = reportDoc.getParagraphByIndex(0, false);
> > firstEmpty.remove();
> > Paragraph header = Paragraph.newParagraph(reportDoc);
> >
> > And all seems well.
> >
> > Or better? Is simply to just use the firstEmpty, fill it and add others
> > following it.
> >
> > TextDocument reportDoc = TextDocument.newTextDocument();
> > Paragraph firstPara = reportDoc.getParagraphByIndex(0, false);
> > firstPara.appendTextContent(lead);
> > Paragraph nextPara = Paragraph.newParagraph(reportDoc);
> >
> > The heading issue I will check out later.
> >
> >
> >
> > On Fri, Aug 23, 2013 at 6:59 AM, Alan Knight <[email protected]> wrote:
> >
> >> The following code doesn't have a blank line after a page break.
> >>
> >> doc.addPageBreak();
> >> Paragraph p0 = doc.getParagraphByIndex(0, false);
> >> p0.setTextContent("First Paragraph");
> >> p0.applyHeading(true, 1);
> >>
> >> However, applyHeading isn't working.
> >>
> >> The following will apply heading level 1, but has the original problem
> of
> >> a
> >> blank line before the paragraph.
> >>
> >> doc.addPageBreak();
> >> doc.addParagraph("First Paragraph").applyHeading(true, 1);
> >>
> >>
> >> Alan
> >>
> >>
> >>
> >> On Thu, Aug 22, 2013 at 1:51 PM, Alan Knight <[email protected]> wrote:
> >>
> >> > It's not a style issue. The second paragraph doesn't have the blank
> >> > line. If I turn on the non-printable characters view in Open Office,
> I
> >> see
> >> > there are three paragraph marks. One is on the blank line before
> "First
> >> > paragraph" and the other two are at the end of "First paragraph" and
> >> > "Second paragraph".
> >> > However, I did just find a work around. If I replace the first
> >> > addParagraph() with
> >> >
> >> > Paragraph p0 = doc.getParagraphByIndex(0, false);
> >> > p0.setTextContent("First Paragraph");
> >> >
> >> > there is no more blank line. Now to see if this works after calling
> >> > addPageBreak();
> >> >
> >> > Thanks,
> >> > Alan
> >> >
> >> >
> >> >
> >> > On Thu, Aug 22, 2013 at 3:22 AM, Rory O'Farrell <[email protected]>
> >> wrote:
> >> >
> >> >> On Thu, 22 Aug 2013 03:04:37 -0400
> >> >> Alan Knight <[email protected]> wrote:
> >> >>
> >> >> > In the following code I get a blank line before the first
> paragraph.
> >> >> >
> >> >> > TextDocument doc = TextDocument.newTextDocument();
> >> >> > doc.addParagraph("First paragraph");
> >> >> > doc.addParagraph("Second paragraph");
> >> >> > doc.save("doc.odt");
> >> >> >
> >> >> > I also see this with a paragraph added after a
> >> >> > TextDocument::addPageBreak();
> >> >> >
> >> >> > Does anyone know how to avoid the blank line?
> >> >> >
> >> >> > I'm using the latest version of the simple API (0.6). However, I
> >> had to
> >> >> > download a lot of additional jar files to make that version run.
> >> Just
> >> >> > downloading the 0.6 JAR files and running the example above gives
> me
> >> a
> >> >> > java.lang.NoClassDefFoundError:
> >> org.apache.clerezza.utils.UriException.
> >> >> > After downloading about 15 additional JAR files, I was able to get
> >> the
> >> >> > example to run.
> >> >> >
> >> >> > Thanks,
> >> >> > Alan
> >> >>
> >> >> I suspect you need to modify the Default paragraph style on the
> target
> >> >> computer to remove either the Spacing : Above paragraph or the
> Spacing
> >> :
> >> >> below paragraph.
> >> >>
> >> >>
> >> >> --
> >> >> Rory O'Farrell <[email protected]>
> >> >>
> >> >
> >> >
> >>
> >
> >
> >
> > --
> > Cheers,
> >
> > Ian C
> >
>
>
>
> --
> Cheers,
>
> Ian C
>