I need to encode a pdf document to base64 in Delphi6. Can anyone help me?
Asked
Active
Viewed 1.3k times
3 Answers
9
You can use the EncdDecd
unit that is supplied with Delphi. The function you need is EncodeStream
. You simply need to create two streams, one for input and one for output. If you are working with files then you should create TFileStream
instances.
Once you have your two file streams created, all you need is:
EncodeStream(InputStream, OutputStream);

David Heffernan
- 601,492
- 42
- 1,072
- 1,490
-
Thank you very much. You've been so helpful . Thank you one again – user1984193 Feb 19 '13 at 17:53
-
David Heffernam, can you tell me how can i see the OutputStream? I need to insert this encoded file on a Xml file – user1984193 Feb 26 '13 at 10:52
-
1If you are adding to the XML file by means of a string, then make `OutputStream` be `TStringStream`. Read the `DataString` property of `TStringStream`. – David Heffernan Feb 26 '13 at 11:00
-
I tried to create OutputStream as a .txt file "OutputFile:=TFileStream.Create(WOutFicheiro,fmCreate) – user1984193 Feb 26 '13 at 15:24
-
That will work just fine. – David Heffernan Feb 26 '13 at 15:26
-
I tried to create OutputStream as a .txt file "OutputFile:=TFileStream.Create(WOutFicheiro,fmCreate)" and the content of the outputfile is not the same as if I create as a TStringStream (just gives 2 characters ) "Result:= OutputFile.DataString;" - Result = String . I don´t know if I'm doing this correctly. – user1984193 Feb 26 '13 at 15:37
-
Well, I cannot see your code. I think I answered the question that you asked. – David Heffernan Feb 26 '13 at 15:39
-
But how can I insert the content of my .txt file into Xml file? – user1984193 Feb 26 '13 at 15:55
-
That's not the question that you asked. I answered the question that you asked. As for how to put your file into your XML I've no idea. I don't know how you are working with XML. I don't know what XML lib you have. If you have another question do ask. But you should also accept an answer here because this question is answered. – David Heffernan Feb 26 '13 at 16:05
5
unit base64;
interface
uses Classes;
function base64encode(f:TStream):string;
implementation
const
Base64Codes:array[0..63] of char=
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function base64encode(f:TStream):string;
const
dSize=57*100;//must be multiple of 3
var
d:array[0..dSize-1] of byte;
i,l:integer;
begin
Result:='';
l:=dSize;
while l=dSize do
begin
l:=f.Read(d[0],dSize);
i:=0;
while i<l do
begin
if i+1=l then
Result:=Result+
Base64Codes[ d[i ] shr 2]+
Base64Codes[((d[i ] and $3) shl 4)]+
'=='
else if i+2=l then
Result:=Result+
Base64Codes[ d[i ] shr 2]+
Base64Codes[((d[i ] and $3) shl 4) or (d[i+1] shr 4)]+
Base64Codes[((d[i+1] and $F) shl 2)]+
'='
else
Result:=Result+
Base64Codes[ d[i ] shr 2]+
Base64Codes[((d[i ] and $3) shl 4) or (d[i+1] shr 4)]+
Base64Codes[((d[i+1] and $F) shl 2) or (d[i+2] shr 6)]+
Base64Codes[ d[i+2] and $3F];
inc(i,3);
if ((i mod 57)=0) then Result:=Result+#13#10;
end;
end;
end;
end.

Stijn Sanders
- 35,982
- 11
- 45
- 67
-
1+1 for stand-alone solution. Indy and JVCL are great, but not if you aren't already using them. – Chris Thornton Feb 18 '13 at 19:02
-
4@ChrisThornton There's no need to roll your own even in D6. You can use `EncdDecd` which ships with even ancient D6. – David Heffernan Feb 18 '13 at 19:07
-
@Stijn It would be useful to add bool parameter `linebrake` to the function for receiving one-line base64 string. `if ((i mod 57)=0) and linebrake then Result:=Result+#13#10;` – Maxim Ponomarev Mar 18 '13 at 05:53
1
You can use INDY's TIdEncoderMIME class to encode any file.
AFAIK, INDY 9 comes pre-installed in Delphi 6, but is advisable to update your INDY version as INDY 10 is current with lot's of improvements and bug fixes over the old INDY 9.
Your code may look like this:
uses IdCoder, IdCoder3to4, IdCoderMIME, IdBaseComponent;
procedure TForm1.Button1Click(Sender: TObject);
var
SourceStr: TFileStream;
Encoder: TIdEncoderMIME;
begin
if OpenDialog1.Execute then
begin
SourceStr := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
try
Encoder := TIdEncoderMIME.Create(nil);
try
Memo1.Lines.Text := Encoder.Encode(SourceStr);
finally
Encoder.Free;
end;
finally
SourceStr.Free;
end;
end;
end;

jachguate
- 16,976
- 3
- 57
- 98