Forum > Packages and Libraries
ODF document from Gettysburg address
(1/1)
JD:
Hi there everyone,
First of all I would like to say a BIG, BIG thank you to all those behind the ODF package especially the person who made the most recent changes. It is now easy to create ODF (LibreOffice/OpenOffice Writer documents) which is a big relief for me because I had been looking at Apache POI to do the job.
I wanted to try creating some simple documents and I created the following unit
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, odf_types; type { TForm1 } TForm1 = class(TForm) btnSaveODF: TButton; memText: TMemo; procedure btnSaveODFClick(Sender: TObject); procedure FormShow(Sender: TObject); private public end; const GETTYSBURG_ADDRESS_BLISS_COPY = 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.' + LineEnding + LineEnding + 'Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. ' + 'We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.' + LineEnding + LineEnding + 'But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. ' + 'The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. ' + 'It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.' + LineEnding + LineEnding + LineEnding + 'Abraham Lincoln' + LineEnding + 'November 19, 1863'; cOutputFile = 'Gettysburg.odt'; {$IfDef WINDOWS} cOutput = cOutputFile; {$Else} cOutput = '/tmp/' + cOutputFile; {$EndIf} cStyleName = 'Standard'; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormShow(Sender: TObject);begin memText.Clear; memText.Append(GETTYSBURG_ADDRESS_BLISS_COPY); memText.ReadOnly := True;end; procedure TForm1.btnSaveODFClick(Sender: TObject);begin ShowMessage(memText.Text); with TOdfTextDocument.Create do begin AddParagraph(cStyleName).TextContent := memText.Text; SaveToZipFile(cOutput); Free; end;end; end.
However the final document truncated the original text. The project file that created the final document is added as an attachment.
I don't know why this happened. What am I missing? Is there a maximum text limit somewhere that I need to change?
In addition, I would like to know how I can create more complex documents containing varied text styles (bold, differing sizes, colours etc) and images.
Thanks a lot for your assistance.
JD
JD:
I've made a little progress. I switched to fpvectorial and this is the code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fpvectorialpkg, fpvectorial; type { TForm1 } TForm1 = class(TForm) btnSaveODF: TButton; memText: TMemo; procedure btnSaveODFClick(Sender: TObject); procedure FormShow(Sender: TObject); private public end; const GETTYSBURG_ADDRESS_BLISS_COPY = 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.' + LineEnding + LineEnding + 'Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. ' + 'We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.' + LineEnding + LineEnding + 'But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. ' + 'The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. ' + 'It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.' + LineEnding + LineEnding + LineEnding + 'Abraham Lincoln' + LineEnding + 'November 19, 1863'; var Form1: TForm1; Document: TvVectorialDocument; Page: TvTextPageSequence; TopParagraph, MainParagraph: TvParagraph; BoldTextStyle: TvStyle; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormShow(Sender: TObject);begin memText.Clear; memText.Append(GETTYSBURG_ADDRESS_BLISS_COPY); memText.ReadOnly := True;end; procedure TForm1.btnSaveODFClick(Sender: TObject);begin ShowMessage(memText.Text); // Create the document Document := TvVectorialDocument.Create; // try // Adds the defaut Paragraph Styles // StyleTextBody, StyleHeading1, // StyleHeading2 & StyleHeading3 Document.AddStandardTextDocumentStyles(vfUnknown); // Add our own Style BoldTextStyle := Document.AddStyle(); BoldTextStyle.Kind := vskTextSpan; BoldTextStyle.Name := 'Bold'; BoldTextStyle.Font.Bold := True; BoldTextStyle.SetElements := BoldTextStyle.SetElements + [spbfFontBold]; // Create the Document Page := Document.AddTextPageSequence; TopParagraph := Page.AddParagraph; TopParagraph.Style := Document.StyleHeading2; TopParagraph.AddText('GETTYSBURG ADDRESS - BLISS COPY'); MainParagraph := Page.AddParagraph; MainParagraph.Style := Document.StyleTextBody; MainParagraph.AddText(GETTYSBURG_ADDRESS_BLISS_COPY); // Save the document in 4 formats Document.WriteToFile('Gettysburg.docx', vfDOCX); Document.WriteToFile('Gettysburg.odt', vfODT); finally Document.Free; end;end; end.
The result is in the attachment. Now the text is complete but fpODF is said to have more features than fpVectorial.
Navigation
[0] Message Index