HTML'si Delphi, C# ve VB.Net'te losLab PDF Kitaplığı'nı kullanarak PDF Sayfalarını %70 oranında ölçeklendirme | losLab Software Development Blog

Teknik makale

Delphi, C# ve VB.Net'te losLab PDF Kitaplığı'nı kullanarak PDF Sayfalarını %70 oranında ölçeklendirme

· PDF Programlama
PDF sayfalarını %70 oranında ölçeklendirin losLab PDF kitaplığı

PDF'lerle çalışırken, genellikle içeriği çeşitli amaçlarla ölçeklendirme gereksinimleri vardır. Bu senaryoda PDF'deki tüm sayfaların boyutunu %70 oranında azaltmayı hedefliyoruz. Bu kılavuzda gerekli adımlar açıklanmakta, ilgili sorular ele alınmakta ve çözümler sunulmaktadır.

Sorun Bildirimi

Orijinal sayfa sırasını korurken bir PDF belgesinin her sayfasını %70 oranında küçültmemiz gerekiyor. Bu şunları gerektirir:

  1. PDF dosyası yükleniyor.
  2. Her sayfayı yakalıyor ve ölçeklendiriyor.
  3. Ölçeklenen sayfalar yeni bir PDF dosyasına kaydediliyor.

Hedefe Ulaşmak İçin Adımlar

  1. Ortamı Başlat:
    • Orijinal PDF dosyasını yükleyin.
    • Çakışmaları önlemek için önceden var olan çıktı dosyalarını silin.
  2. Ölçeklendirme Parametrelerini Ayarlayın:
    • Ölçeklendirme faktörünü (%70) tanımlayın.
    • Ölçeklenen içeriği ortalamak için gereken sınırları hesaplayın.
  3. Her Sayfayı Bir Döngüde İşle:
    • İlk sayfayı seçin.
    • Sayfa içeriğini yakalayın.
    • Orijinal boyutlarla yeni bir sayfa oluşturun.
    • Yakalanan, ölçeklenen içeriği yeni sayfaya çizin.
    • Tüm sayfalar için tekrarlayın.
  4. Yeni PDF'yi kaydedin ve açın:
    • Değiştirilen sayfaları yeni bir PDF dosyasına kaydedin.
    • Sonuçları incelemek için yeni PDF'yi otomatik olarak açın.

Kod Uygulaması

Yukarıdaki adımları gerçekleştiren C# kodu:

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
38
39
40
41
42
43
44
45
46
47
48
private void button_Click(object sender, EventArgs e)
{
    // Delete the old file if it exists to avoid any conflicts.
    File.Delete("newpages.pdf");
 
    // Define variables for page dimensions and scaling factors.
    double pageWidth, pageHeight, horizBorder, vertBorder;
    double scaleFactor = 0.70; // 70% scaling reduction.
    int capturedPageId;
    int ret;
 
    // Load the original PDF document.
    PDFL.LoadFromFile("Pages.pdf");
    PDFL.SetOrigin(1);
 
    // Get the total number of pages in the document.
    int numPages = PDFL.PageCount();
 
    // Loop through all pages to process each one.
    for (int i = 1; i <= numPages; i++)
    {
        // Always select the first page as the pages get deleted after capture.
        PDFL.SelectPage(1);
 
        // Retrieve the dimensions of the current page.
        pageWidth = PDFL.PageWidth();
        pageHeight = PDFL.PageHeight();
 
        // Calculate the borders to center the scaled page content.
        horizBorder = pageWidth * (1.0 - scaleFactor) / 2;
        vertBorder = pageHeight * (1.0 - scaleFactor) / 2;
 
        // Capture the content of the first page. This action deletes the page from the document.
        capturedPageId = PDFL.CapturePage(1);
 
        // Create a new page with the original dimensions.
        int pageId = PDFL.NewPage();
        PDFL.SetPageDimensions(pageWidth, pageHeight);
 
        // Draw the captured page content onto the new page with the specified scaling.
        ret = PDFL.DrawCapturedPage(capturedPageId, horizBorder, vertBorder, pageWidth - 2 * horizBorder, pageHeight - 2 * vertBorder);
    }
 
    // Save the modified document as a new PDF file.
    PDFL.SaveToFile("newpages.pdf");
 
    // Open the newly created PDF file for review.
    System.Diagnostics.Process.Start(@"newpages.pdf");
[Format Süresi: 0,0010 saniye]
Açıklama ve Gerekçe
  • Dosya Silme: Hataları veya güncel olmayan içeriği önlemek için önceki çıktıların temizlenmesini sağlar.
  • Ölçekleme Faktörü: 0,70'e ayarlandığında içeriğin boyutu orijinalin %70'ine düşer.
  • Sınır Hesaplaması: Ölçeklenen içeriği orijinal sayfa boyutları içinde ortalar.
  • Sayfa İşleme Döngüsü: Tüm sayfaları yineler, her birini sırayla yakalar, ölçeklendirir ve çizer.
  • Dosya Kaydetme ve Açma: Yeni belgeyi sonlandırır ve kullanıcının değişiklikleri doğrulaması için onu açar.

Bu yapılandırılmış yaklaşımı izleyerek, PDF'deki her sayfanın tutarlı bir şekilde ölçeklenmesini ve orijinal sırasını korumasını sağlıyoruz, böylece profesyonelce işlenmiş bir belge elde ediyoruz.

Delphi baskısı:

PDF sayfalarını %70 oranında Ölçeklendirmek için Delphi'yi kullanın:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
procedure TForm1.ButtonClick(Sender: TObject);
var
  pageWidth, pageHeight, horizBorder, vertBorder: Double;
  scaleFactor: Double;
  capturedPageId, ret: Integer;
  numPages, pageId, i: Integer;
begin
  // Delete the old file if it exists to avoid any conflicts.
  if FileExists('newpages.pdf') then
    DeleteFile('newpages.pdf');
 
  // Define the scaling factor (70%).
  scaleFactor := 0.70; // 70% scaling reduction.
 
  // Load the original PDF document.
  PDFL.LoadFromFile('Pages.pdf');
  PDFL.SetOrigin(1);
 
  // Get the total number of pages in the document.
  numPages := PDFL.PageCount();
 
  // Loop through all pages to process each one.
  for i := 1 to numPages do
  begin
    // Always select the first page as the pages get deleted after capture.
    PDFL.SelectPage(1);
 
    // Retrieve the dimensions of the current page.
    pageWidth := PDFL.PageWidth();
    pageHeight := PDFL.PageHeight();
 
    // Calculate the borders to center the scaled page content.
    horizBorder := pageWidth * (1.0 - scaleFactor) / 2;
    vertBorder := pageHeight * (1.0 - scaleFactor) / 2;
 
    // Capture the content of the first page. This action deletes the page from the document.
    capturedPageId := PDFL.CapturePage(1);
 
    // Create a new page with the original dimensions.
    pageId := PDFL.NewPage();
    PDFL.SetPageDimensions(pageWidth, pageHeight);
 
    // Draw the captured page content onto the new page with the specified scaling.
    ret := PDFL.DrawCapturedPage(capturedPageId, horizBorder, vertBorder, pageWidth - 2 * horizBorder, pageHeight - 2 * vertBorder);
  end;
 
  // Save the modified document as a new PDF file.
  PDFL.SaveToFile('newpages.pdf');
 
  // Open the newly created PDF file for review.
  ShellExecute(0, 'open', 'newpages.pdf', nil, nil, SW_SHOWNORMAL);
end;
[Format Süresi: 0,0011 saniye]

VB.Net sürümü

İşte görevi gerçekleştirmek için VB.Net kodu:

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
38
39
40
41
42
43
44
45
46
47
48
Private Sub button_Click(sender As Object, e As EventArgs) Handles button.Click
    ' Delete the old file if it exists to avoid any conflicts.
    If File.Exists("newpages.pdf") Then
        File.Delete("newpages.pdf")
    End If
 
    ' Define variables for page dimensions and scaling factors.
    Dim pageWidth, pageHeight, horizBorder, vertBorder As Double
    Dim scaleFactor As Double = 0.70 ' 70% scaling reduction.
    Dim capturedPageId, ret As Integer
 
    ' Load the original PDF document.
    PDFL.LoadFromFile("Pages.pdf")
    PDFL.SetOrigin(1)
 
    ' Get the total number of pages in the document.
    Dim numPages As Integer = PDFL.PageCount()
 
    ' Loop through all pages to process each one.
    For i As Integer = 1 To numPages
        ' Always select the first page as the pages get deleted after capture.
        PDFL.SelectPage(1)
 
        ' Retrieve the dimensions of the current page.
        pageWidth = PDFL.PageWidth()
        pageHeight = PDFL.PageHeight()
 
        ' Calculate the borders to center the scaled page content.
        horizBorder = pageWidth * (1.0 - scaleFactor) / 2
        vertBorder = pageHeight * (1.0 - scaleFactor) / 2
 
        ' Capture the content of the first page. This action deletes the page from the document.
        capturedPageId = PDFL.CapturePage(1)
 
        ' Create a new page with the original dimensions.
        Dim pageId As Integer = PDFL.NewPage()
        PDFL.SetPageDimensions(pageWidth, pageHeight)
 
        ' Draw the captured page content onto the new page with the specified scaling.
        ret = PDFL.DrawCapturedPage(capturedPageId, horizBorder, vertBorder, pageWidth - 2 * horizBorder, pageHeight - 2 * vertBorder)
    Next
 
    ' Save the modified document as a new PDF file.
    PDFL.SaveToFile("newpages.pdf")
 
    ' Open the newly created PDF file for review.
    Process.Start("newpages.pdf")
End Sub
[Format Süresi: 0,0008 saniye]

Kodun hem VB.Net hem de Delphi sürümleri, orijinal C# koduyla aynı sonucu elde ederek, orijinal sırayı korurken PDF'deki her sayfanın %70 oranında küçültülmesini sağlar.