delphi SynPDF 添加大纲(书签) - txgh - 博客园

uses SynPdf;

procedure TForm1.Button6Click(Sender: TObject);
var
  Pdf: TPdfDocumentGDI;
begin
  //创建PDF文档
  Pdf := TPdfDocumentGDI.Create(True);
  //使用书签,需要设置Create参数为True或者UseOutlines设置为True
  Pdf.UseOutlines := True;
  try
    Pdf.AddPage;
    //添加总书签(Level为级别,从0开始,TopPosition为位置,从页面底开始)
    Pdf.CreateOutline('文档开始', 0, Pdf.Canvas.Page.PageHeight);
    //添加第一页
    with Pdf.VCLCanvas do
    begin
      Font.Name := '宋体';
      Font.Size := 20;
      TextOut(100, 100, '第一页标题');
      TextOut(100, 200, '第一页内容');
      Pdf.CreateOutline('第一页标题', 1, Pdf.Canvas.Page.PageHeight - 100);
    end;
    //添加第二页
    Pdf.AddPage;
    with Pdf.VCLCanvas do
    begin
      Font.Name := '宋体';
      Font.Size := 20;
      TextOut(100, 100, '第二页标题');
      TextOut(100, 200, '第二页内容');
      Pdf.CreateOutline('第二页标题', 1, Pdf.Canvas.Page.PageHeight - 100);
    end;
    //添加第三页
    Pdf.AddPage;
    with Pdf.VCLCanvas do
    begin
      Font.Name := '宋体';
      Font.Size := 20;
      TextOut(100, 100, '第三页标题');
      TextOut(100, 200, '第三页内容');
      Pdf.CreateOutline('第三页标题', 1, Pdf.Canvas.Page.PageHeight - 100);
    end;
    //展开书签
    Pdf.OutlineRoot.First.Opened := True;
    Pdf.SaveToFile('C:\Users\Administrator\Desktop\ceshi.pdf');
  finally
    Pdf.Free;
  end;
end;