HTML'si Delphi'de PDFium VCL ile Sıfırdan PDF Belgeleri Oluşturma | losLab Software Development Blog

Teknik makale

Delphi'de PDFium VCL ile Sıfırdan PDF Belgeleri Oluşturma

· PDF Programlama

PDFium öncelikle PDF görüntüleme ve işlemeyle tanınırken, PDFium VCL aynı zamanda güçlü PDF oluşturma yetenekleri de sağlar. PDF oluştur demo, metin, şekil, tablo ve filigran içeren PDF belgelerinin programlı olarak nasıl oluşturulacağını gösterir.

Genel Bakış

Bu demo, stillendirilmiş metin, geometrik şekiller, basit bir tablo ve çapraz filigran gibi çeşitli öğeler içeren yeni bir PDF belgesi oluşturur. PDFium VCL'de bulunan temel PDF oluşturma API'lerini gösterir.

Temel Özellikler

  • Yeni Belgeler Oluştur – Sıfırdan PDF'ler oluşturun
  • Metin Ekle – Özel yazı tipleri, boyutlar ve renklerle stilize edilmiş metin ekleyin
  • Şekiller Çiz – Dikdörtgenler, yollar ve diğer geometrik şekiller oluşturun
  • Tablolar Oluştur – Basit metin tabanlı tablolar oluşturun
  • Filigran Ekle – Döndürülmüş, yarı şeffaf metin ekle

PDFium DLL Gereksinimleri

Herhangi bir PDFium VCL uygulamasını çalıştırmadan önce PDFium DLL dosyalarının kurulu olduğundan emin olun:

  • pdfium32.dll / pdfium64.dll – Standart sürümler (~5-6 MB)
  • pdfium32v8.dll / pdfium64v8.dll – V8 JavaScript motoruyla (~23-27 MB)

Kurulum: Çalıştır DLL'leri otomatik olarak Windows sistem dizinlerine kopyalamak için Yönetici olarak PDFiumVCL\DLLs\CopyDlls.bat .

Yeni Bir PDF Belgesi Oluşturma

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
procedure CreateNewPdf;
var
  Pdf: TPdf;
begin
  Pdf := TPdf.Create(nil);
  try
    // Create empty document
    Pdf.CreateDocument;
    Pdf.Active := True;
    
    // Add first page (A4 size: 595 x 842 points)
    Pdf.AddPage(1, 595, 842);
    Pdf.PageNumber := 1;
    
    // Add content here...
    
    // Save the document
    Pdf.SaveAs('output.pdf');
    
  finally
    Pdf.Active := False;
    Pdf.Free;
  end;
end;
[Format Süresi: 0,0007 saniye]

Metin Ekleme

 AddText yöntemi geçerli sayfaya stillendirilmiş metin ekler:

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
procedure TPdf.AddText(
  const Text, Font: WString;   // Text content and font name
  FontSize: Single;            // Font size in points
  X, Y: Double;                // Position (from bottom-left)
  Color: TColor = clBlack;     // Text color
  Alpha: Byte = $FF;           // Transparency (0-255)
  Angle: Double = 0.0          // Rotation angle in degrees
);
 
// Examples
Pdf.AddText('Hello World', 'Arial', 24, 50, 750, clBlack, $FF, 0.0);
Pdf.AddText('Subtitle', 'Times New Roman', 14, 50, 720, clGray, $FF, 0.0);
Pdf.AddText('Rotated Text', 'Arial', 18, 300, 400, clRed, $80, 45.0);
[Format Süresi: 0,0003 saniye]

Komple PDF Oluşturma Örneği

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
procedure TFormMain.CreateSimplePDF;
begin
  Pdf.CreateDocument;
  Pdf.Active := True;
  try
    // Add A4 page
    Pdf.AddPage(0, 595, 842);
    
    // Title
    Pdf.AddText(EditTitle.Text, 'Arial', 18, 50, 750, clBlack, $FF, 0.0);
    
    // Author line
    Pdf.AddText('By: ' + EditAuthor.Text, 'Arial', 12, 50, 720, clGray, $FF, 0.0);
    
    // Body text
    Pdf.AddText('This is a simple PDF document created with PDFium VCL.',
      'Arial', 12, 50, 680, clBlack, $FF, 0.0);
    Pdf.AddText('You can add various elements to your PDF:',
      'Arial', 12, 50, 650, clBlack, $FF, 0.0);
      
    // Optional features
    if CheckShapes.Checked then
      AddShapes;
      
    if CheckTable.Checked then
      AddTable;
      
    if CheckWatermark.Checked then
      AddWatermark;
      
    // Save
    Pdf.SaveAs('Sample.pdf');
    
  finally
    Pdf.Active := False;
  end;
end;
[Format Süresi: 0,0004 saniye]

CreatePath ile Şekiller Çizme

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Create a path starting at (X, Y)
procedure TPdf.CreatePath(
  X, Y: Single;                           // Starting point
  FillMode: TPdfFillMode = fmNone;        // None, Alternate, or Winding
  FillColor: TColor = clBlack;            // Fill color
  FillAlpha: Byte = $FF;                  // Fill transparency
  Stroke: Boolean = True;                 // Draw outline
  StrokeColor: TColor = clBlack;          // Outline color
  StrokeAlpha: Byte = $FF;                // Outline transparency
  StrokeWidth: Single = 1.0;              // Line width
  LineCap: TPdfLineCap = lcDefault;       // Line end style
  LineJoin: TPdfLineJoin = ljDefault;     // Line join style
  BlendMode: TPdfBlendMode = bmDefault    // Color blending
);
 
// Create a rectangle path
procedure TPdf.CreatePath(
  X, Y, Width, Height: Single;            // Rectangle bounds
  // Same optional parameters as above...
);
[Format Süresi: 0,0003 saniye]

Şekil Çizim Örnekleri

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
procedure TFormMain.AddShapes;
begin
  // Filled rectangle with blue color at 50% opacity
  Pdf.CreatePath(100, 550, 150, 80, fmWinding, $FFCCAA, $80);
  Pdf.AddPath;
  Pdf.AddText('Rectangle', 'Arial', 10, 120, 580, clBlack, $FF, 0.0);
  
  // Another filled shape (green)
  Pdf.CreatePath(350, 590, 80, 80, fmWinding, $CCFFCC, $80);
  Pdf.AddPath;
  Pdf.AddText('Shape', 'Arial', 10, 370, 580, clBlack, $FF, 0.0);
end;
[Format Süresi: 0,0003 saniye]

Karmaşık Yollar Oluşturma

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
procedure DrawTriangle(Pdf: TPdf);
begin
  // Start path at first vertex
  Pdf.CreatePath(200, 300, fmWinding, clBlue, $80, True, clNavy, $FF, 2.0);
  
  // Draw lines to other vertices
  Pdf.LineTo(300, 300);
  Pdf.LineTo(250, 400);
  
  // Close the path
  Pdf.ClosePath;
  
  // Add to page
  Pdf.AddPath;
end;
 
procedure DrawCurve(Pdf: TPdf);
begin
  Pdf.CreatePath(100, 200, fmNone, clBlack, $FF, True, clRed, $FF, 2.0);
  Pdf.MoveTo(100, 200);
  
  // Bezier curve with control points
  Pdf.BezierTo(
    150, 250,   // Control point 1
    200, 250,   // Control point 2
    250, 200    // End point
  );
  
  Pdf.AddPath;
end;
[Format Süresi: 0,0004 saniye]

Basit Tablolar Oluşturmak

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
procedure TFormMain.AddTable;
var
  X, Y: Double;
  I: Integer;
begin
  X := 100;
  Y := 450;
  
  // Table header
  Pdf.AddText('Item', 'Arial', 10, X, Y, clBlack, $FF, 0.0);
  Pdf.AddText('Quantity', 'Arial', 10, X + 100, Y, clBlack, $FF, 0.0);
  Pdf.AddText('Price', 'Arial', 10, X + 200, Y, clBlack, $FF, 0.0);
  
  // Draw header underline
  Pdf.CreatePath(X, Y - 5, 250, 0.5, fmNone, clBlack, $FF,
    True, clBlack, $FF, 1.0);
  Pdf.AddPath;
  
  // Table rows
  for I := 1 to 3 do
  begin
    Y := Y - 20;
    Pdf.AddText('Item ' + IntToStr(I), 'Arial', 9, X, Y, clBlack, $FF, 0.0);
    Pdf.AddText(IntToStr(I), 'Arial', 9, X + 100, Y, clBlack, $FF, 0.0);
    Pdf.AddText('$' + IntToStr(I * 10) + '.00', 'Arial', 9,
      X + 200, Y, clBlack, $FF, 0.0);
  end;
end;
[Format Süresi: 0,0007 saniye]

Filigran Ekleme

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
procedure TFormMain.AddWatermark;
begin
  // Large, semi-transparent, rotated text
  Pdf.AddText('SAMPLE', 'Arial', 72, 200, 200, clGray, $30, 45.0);
end;
 
// More sophisticated watermark
procedure AddDiagonalWatermark(Pdf: TPdf; const Text: string);
begin
  // Center of A4 page
  Pdf.AddText(Text, 'Arial', 60,
    595 / 2 - 100,  // X centered
    842 / 2,        // Y centered
    $CCCCCC,        // Light gray
    $40,            // 25% opacity
    45.0            // 45 degree rotation
  );
end;
[Format Süresi: 0,0002 saniye]

Görüntü Ekleme

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
procedure AddImageToPdf(Pdf: TPdf; const ImageFile: string);
var
  Picture: TPicture;
begin
  Picture := TPicture.Create;
  try
    Picture.LoadFromFile(ImageFile);
    
    // Add at position with automatic sizing
    Pdf.AddPicture(Picture, 50, 400);
    
    // Or with specific dimensions
    Pdf.AddPicture(Picture, 50, 400, 200, 150);
    
  finally
    Picture.Free;
  end;
end;
[Format Süresi: 0,0003 saniye]

Çok Sayfalı Belgeler

Urvanov Sözdizimi Vurgulayıcı v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
procedure CreateMultiPagePdf;
var
  Pdf: TPdf;
  I: Integer;
begin
  Pdf := TPdf.Create(nil);
  try
    Pdf.CreateDocument;
    Pdf.Active := True;
    
    for I := 1 to 5 do
    begin
      // Add page
      Pdf.AddPage(I, 595, 842);
      Pdf.PageNumber := I;
      
      // Add content
      Pdf.AddText('Page ' + IntToStr(I), 'Arial', 24,
        250, 400, clBlack, $FF, 0.0);
    end;
    
    Pdf.SaveAs('multipage.pdf');
    
  finally
    Pdf.Active := False;
    Pdf.Free;
  end;
end;
[Format Süresi: 0,0004 saniye]

Sayfa Boyutları

Format Width (pts) Height (pts)
A4 Portrait 595 842
A4 Landscape 842 595
Letter Portrait 612 792
Letter Landscape 792 612
Legal Portrait 612 1008

Kullanım Durumları

  • Rapor Oluşturma – Verilerden biçimlendirilmiş raporlar oluşturun
  • Fatura Oluşturma – Fatura ve makbuz oluştur
  • Sertifika Oluşturma – Şablonlarla sertifikalar oluşturun
  • Belge Şablonları – Şablonları dinamik içerikle doldurun

Sonuç

Create PDF demosu, PDFium VCL'nin yalnızca görüntülemek için olmadığını, eksiksiz bir PDF araç seti olduğunu gösterir. Metin, şekil, resim ve çok sayfalı belgelere yönelik destek sayesinde tamamen koddan profesyonel PDF'ler oluşturabilirsiniz.

Bileşenin görüntüleme ve işleme yetenekleriyle birleştiğinde PDFium VCL, Delphi uygulamalarında kapsamlı PDF işleme için ihtiyacınız olan her şeyi sağlar.

PDF oluşturmayı keşfedin ile PDFium VCL Bileşeni ve dinamik belge oluşturmayı uygulamalarınıza getirin.