文章目录
用纯代码打印PDF文字或自定义形状
提示:效果如下
核心源代码
procedure TForm1.Button1Click(Sender: TObject);
var
MyStream: TMemoryStream;
Writer: TBinaryWriter;
begin
MyStream := TMemoryStream.Create;
try
Writer := TBinaryWriter.Create(MyStream);
try
// Header
Writer.Write(TEncoding.UTF8.GetBytes(‘%PDF-1.4’ + sLineBreak));
// First object
Writer.Write(TEncoding.UTF8.GetBytes(‘1 0 obj’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘<< /Type /Catalog’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘/Outlines 2 0 R’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘/Pages 3 0 R’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘>>’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘endobj’ + sLineBreak));
// End
// Second object
Writer.Write(TEncoding.UTF8.GetBytes(‘2 0 obj’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘<< /Type /Outlines’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘/Count 0’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘>>’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘endobj’ + sLineBreak));
// End
// Third object
Writer.Write(TEncoding.UTF8.GetBytes(‘3 0 obj’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘<< /Type /Pages’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘/Kids [4 0 R]’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘/Count 1’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘>>’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘endobj’ + sLineBreak));
// End
// Fourth
Writer.Write(TEncoding.UTF8.GetBytes(‘4 0 obj’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetBytes(‘<< /Type /Page’ + sLineBreak));
Writer.Write(TEncoding.UTF8.GetByt