unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
fpvectorialpkg,
fpvectorial,
fpvutils // For RGBToFPColor()
;
type
//type
{ TvTableHelper }
TvTableHelper = class helper for TvTable
public
function AddColWidth(AValue: Double): Integer;
end;
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TvTableHelper }
function TvTableHelper.AddColWidth(AValue: Double): Integer;
begin
SetLength(ColWidths, Length(ColWidths) + 1);
ColWidths[High(ColWidths)] := AValue;
Result := 0;
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
Document: TvVectorialDocument;
Page: TvTextPageSequence;
Paragraph: TvParagraph;
BoldTextStyle: TvStyle;
CenterStyle: TvStyle;
Table: TvTable;
Row: TvTableRow;
// Col:TvTablecol;
Cell: TvTableCell;
iRow: Integer;
iCol: Integer;
const
// Most dimensions in FPVectorial are in mm. If you want to specify
// anything in other units, be ready to do the conversion...
ONE_POINT_IN_MM = 0.35278;
begin
Document := TvVectorialDocument.Create;
try
// Adds the defaut Paragraph Styles
// StyleTextBody, StyleHeading1,
// StyleHeading2 & StyleHeading3
Document.AddStandardTextDocumentStyles(vfUnknown);
// Add our own Style for bolded text elements
BoldTextStyle := Document.AddStyle();
BoldTextStyle.Kind := vskTextSpan; // This style will only be applied to selected Text Spans
BoldTextStyle.Name := 'Bold';
BoldTextStyle.Font.Bold := True;
BoldTextStyle.SetElements := BoldTextStyle.SetElements + [spbfFontBold];
// Add our own style for centered paragraphs
CenterStyle := Document.AddStyle();
CenterStyle.Kind := vskTextBody; // This style will be applied to the whole Paragraph
CenterStyle.Name := 'Table Body Centered';
CenterStyle.Font.Name := 'Verdana';
CenterStyle.Font.Size := 8;
CenterStyle.Alignment := vsaCenter;
CenterStyle.MarginTop := 2 * ONE_POINT_IN_MM;
CenterStyle.MarginBottom := 2 * ONE_POINT_IN_MM;
CenterStyle.SetElements :=
[spbfFontSize, spbfFontName, spbfAlignment, sseMarginTop, sseMarginBottom];
// Create the Document, and add a simple Heading
Page := Document.AddTextPageSequence;
Paragraph := Page.AddParagraph;
Paragraph.Style := Document.StyleHeading1;
Paragraph.AddText('Simple Table');
// Add our Table, which will have 5 columns
Table := Page.AddTable;
Table.PreferredWidth := Dimension(100, dimPercent);
// As we will be merging cells, we have to define all the column widths
// so that ODT knows how many columns there will be.
// Getting the width exactly correct is not essential as LibreOffice Writer
// and Microsoft Word each treat this as a PreferredWidth value.
Table.ColWidthsUnits:=dimMillimeter;
Table.AddColWidth(20);
Table.AddColWidth(50);
Table.AddColWidth(50);
Table.AddColWidth(50);
Table.AddColWidth(100);
// Add a single row at the start which will contain merged cells
Row := Table.AddRow;
Row.BackgroundColor := RGBToFPColor(192, 192, 192); // Grey Shading
Row.Header := True; // Tell the table this is a Header Row
// Header Rows repeat at the top of each page
Cell := Row.AddCell;
Cell.SpannedCols:=1;
Paragraph := Cell.AddParagraph;
Paragraph.Style := CenterStyle;
Paragraph.AddText('Category 1').Style := BoldTextStyle;
Cell := Row.AddCell;
Cell.SpannedCols:=2; // Make this cell cover two columns
Paragraph := Cell.AddParagraph;
Paragraph.Style := CenterStyle;
Paragraph.AddText('Category 2').Style := BoldTextStyle;
Cell := Row.AddCell;
//Cell.Row.;
Cell.SpannedCols:=2; // Make this cell cover two columns
Paragraph := Cell.AddParagraph;
Paragraph.Style := CenterStyle;
Paragraph.AddText('Category 3').Style := BoldTextStyle;
// Add 21 rows to the Table, with the first being the header row
for iRow := 0 to 20 do
begin
Row := Table.AddRow;
// Header Row
if iRow = 0 then
begin
Row.BackgroundColor := RGBToFPColor(192, 192, 192); // Grey Shading
Row.Header := True; // Tell the table this is a Header Row
// Header Rows repeat at the top of each page
end;
// Add 5 cells to each Row
for iCol := 0 to 4 do
begin
Cell := Row.AddCell;
// Each Cell is a TvRichText, we cad add anything we can add to the main
// body of a Document (for now Paragraphs, Tables or Lists)
Paragraph := Cell.AddParagraph;
Paragraph.Style := CenterStyle;
if iRow = 0 then
Paragraph.AddText(Format('Header Col %d', [iCol])).Style := BoldTextStyle
else
Paragraph.AddText(Format('Cell %d x %d', [iRow, iCol]));
end;
end;
Document.WriteToFile('123.docx', vfDOCX);
Document.WriteToFile('123.odt', vfODT);
finally
Document.Free;
end;
end;
end.