Fachartikel

Skalieren Sie PDF-Seiten um 70 % mit der losLab PDF-Bibliothek in Delphi, C# und VB.Net

· PDF-Programmierung
Skalieren Sie PDF-Seiten um 70% mithilfe von losLab PDF-Bibliothek.

Bei der Arbeit mit PDFs gibt es oft Anforderungen, den Inhalt für verschiedene Zwecke zu skalieren. In diesem Szenario möchten wir die Größe aller Seiten innerhalb einer PDF-Datei um 70% reduzieren. Diese Anleitung führt Sie durch die notwendigen Schritte, beantwortet die relevanten Fragen und bietet Lösungen.

Problemstellung

Wir müssen jede Seite eines PDF-Dokuments um 70% verkleinern, wobei die ursprüngliche Seitenreihenfolge beibehalten werden muss. Dies erfordert:

  1. Laden der PDF-Datei.
  2. Erfassen und Skalieren jeder Seite.
  3. Speichern der skalierten Seiten in einer neuen PDF-Datei.

Schritte zur Erreichung des Ziels.

  1. Initialisierung der Umgebung:
    • Laden der ursprünglichen PDF-Datei.
    • Löschen Sie alle zuvor vorhandenen Ausgabedateien, um Konflikte zu vermeiden.
  2. Konfigurieren Sie die Skalierungsparameter:
    • Definieren Sie den Skalierungsfaktor (70%).
    • Berechnen Sie die Ränder, die erforderlich sind, um den skalierten Inhalt zu zentrieren.
  3. Verarbeiten Sie jede Seite in einer Schleife:
    • Wählen Sie die erste Seite aus.
    • Erfassen Sie den Seiteninhalt.
    • Erstellen Sie eine neue Seite mit den ursprünglichen Abmessungen.
    • Zeichnen Sie den erfassten, skalierten Inhalt auf die neue Seite.
    • Wiederholen Sie diesen Vorgang für alle Seiten.
  4. Speichern Sie die neue PDF-Datei und öffnen Sie sie:
    • Speichern Sie die geänderten Seiten in einer neuen PDF-Datei.
    • Öffnen Sie automatisch die neue PDF-Datei, um die Ergebnisse zu überprüfen.

Code-Implementierung

Hier ist der C#-Code, der die oben genannten Schritte ausführt:

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");

Erklärung und Begründung
  • Datei löschen: Stellt sicher, dass alle vorherigen Ausgaben gelöscht werden, um Fehler oder veraltete Inhalte zu vermeiden.
  • Skalierungsfaktor: Wenn dieser Wert auf 0,70 eingestellt ist, wird die Größe des Inhalts auf 70 % der ursprünglichen Größe reduziert.
  • Berechnung des Randes: Zentriert den skalierten Inhalt innerhalb der ursprünglichen Seitenabmessungen.
  • Schleife zur Verarbeitung von Seiten: Durchläuft alle Seiten, erfasst, skaliert und zeichnet jede Seite nacheinander.
  • Speichern und Öffnen von Dateien: Schließt das neue Dokument und öffnet es für den Benutzer, um die Änderungen zu überprüfen.

Durch diesen strukturierten Ansatz stellen wir sicher, dass jede Seite im PDF-Dokument konsistent skaliert wird und ihre ursprüngliche Reihenfolge beibehält, was zu einem professionell verarbeiteten Dokument führt.

Delphi-Version:

Verwenden Sie Delphi, um PDF-Seiten um 70% zu skalieren:

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;

VB.Net-Version

Hier ist der VB.Net-Code, um diese Aufgabe auszuführen:

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

Sowohl die VB.Net- als auch die Delphi-Versionen des Codes erzielen das gleiche Ergebnis wie der ursprüngliche C#-Code, wodurch sichergestellt wird, dass jede Seite im PDF-Dokument um 70% skaliert wird, während die ursprüngliche Reihenfolge beibehalten wird.