Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example demonstrating filling out the odt documents files using the native FPC paszlib library
unit zip_odt;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils;
procedure FillODTDoc(const aSrcFile, aDestFile: String; aKeyValuePairs: TStringList);
implementation
uses
Zipper, FileUtil
;
procedure UnzipODT(const aUnzipDir, aZipFile: String);
var
aUnZipper: TUnZipper;
begin
aUnZipper:=TUnZipper.Create;
try
ForceDirectories(aUnzipDir);
aUnZipper.OutputPath:=aUnzipDir;
aUnZipper.UnZipAllFiles(aZipFile);
finally
aUnZipper.Free;
end;
end;
procedure ZipODT(const aSrcDir, aZipFile: String);
var
aZipper: TZipper;
aList: tStringList;
s: String;
begin
aZipper := TZipper.create;
aList := TStringList.create;
try
FindAllFiles(aList, aSrcDir);
aZipper.FileName := aZipFile;
for s in aList do
aZipper.Entries.AddFileEntry(s, ExtractRelativePath(aSrcDir, s));
aZipper.ZipAllFiles;
finally
aZipper.Free;
aList.free;
end;
end;
procedure FillContent(const aPath: String; const aKey, aValue: String);
var
S, aFile: String;
aContent: TStringList;
begin
aFile:=IncludeTrailingPathDelimiter(aPath)+'content.xml';
S:=ReadFileToString(aFile);
S:=StringReplace(S, aKey, aValue, [rfReplaceAll]);
aContent:=TStringList.Create;
try
aContent.Text:=S;
aContent.SaveToFile(aFile);
finally
aContent.Free;
end;
end;
procedure FillContent(const aPath: String; aKeyValuePairs: TStringList);
var
S, aFile: String;
aContent: TStringList;
i: Integer;
begin
aFile:=IncludeTrailingPathDelimiter(aPath)+'content.xml';
S:=ReadFileToString(aFile);
for i:=0 to aKeyValuePairs.Count-1 do
S:=StringReplace(S, aKeyValuePairs.Names[i], aKeyValuePairs.ValueFromIndex[i], [rfReplaceAll]);
aContent:=TStringList.Create;
try
aContent.Text:=S;
aContent.SaveToFile(aFile);
finally
aContent.Free;
end;
end;
procedure FillODTDoc(const aSrcFile, aDestFile: String; aKeyValuePairs: TStringList);
var
aTempPath: String;
begin
aTempPath:=IncludeTrailingPathDelimiter(GetTempDir(False))+'zipodt'+PathDelim;
ForceDirectories(aTempPath);
UnzipODT(aTempPath, aSrcFile);
FillContent(aTempPath, aKeyValuePairs);
ZipODT(aTempPath, aDestFile);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment