html Skalowanie stron PDF o 70% przy użyciu biblioteki losLab PDF, w Delphi, C# i VB.Net | losLab Software Development Blog

Artykuł techniczny

Skalowanie stron PDF o 70% przy użyciu biblioteki losLab PDF, w Delphi, C# i VB.Net

· Programowanie PDF
Skaluj strony PDF o 70%. losLab PDF biblioteka

Podczas pracy z plikami PDF często wymagane jest skalowanie zawartości do różnych celów. W tym scenariuszu naszym celem jest zmniejszenie rozmiaru wszystkich stron w PDF o 70%. W tym przewodniku opisano niezbędne kroki, odpowiadając na odpowiednie pytania i podając rozwiązania.

Oświadczenie o problemie

Musimy zmniejszyć każdą stronę dokumentu PDF o 70%, zachowując oryginalną kolejność stron. Wymaga to:

  1. Ładowanie pliku PDF.
  2. Przechwytywanie i skalowanie każdej strony.
  3. Zapisywanie przeskalowanych stron do nowego pliku PDF.

Kroki do osiągnięcia celu

  1. Zainicjuj środowisko:
    • Załaduj oryginalny plik PDF.
    • Usuń wcześniej istniejący plik wyjściowy, aby uniknąć konfliktów.
  2. Skonfiguruj parametry skalowania:
    • Zdefiniuj współczynnik skalowania (70%).
    • Oblicz granice wymagane do wyśrodkowania skalowanej zawartości.
  3. Przetwarzaj każdą stronę w pętli:
    • Wybierz pierwszą stronę.
    • Przechwyć zawartość strony.
    • Utwórz nową stronę z oryginalnymi wymiarami.
    • Narysuj przechwyconą, przeskalowaną treść na nowej stronie.
    • Powtórz dla wszystkich stron.
  4. Zapisz nowy PDF i otwórz go:
    • Zapisz zmodyfikowane strony w nowym pliku PDF.
    • Automatycznie otwórz nowy PDF, aby przejrzeć wyniki.

Implementacja kodu

Oto kod C#, który wykonuje powyższe kroki:

Zakreślacz składni Urvanov 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");
[Czas formatu: 0,0010 sekundy]
Wyjaśnienie i uzasadnienie
  • Usuwanie pliku: Zapewnia wyczyszczenie wszelkich poprzednich danych wyjściowych, aby zapobiec błędom lub nieaktualnej treści.
  • Współczynnik skalowania: Ustawienie na 0,70 powoduje zmniejszenie rozmiaru zawartości do 70% oryginału.
  • Obliczanie granicy: Wyśrodkowuje skalowaną treść w obrębie oryginalnych wymiarów strony.
  • Pętla przetwarzania strony: Przechodzi przez wszystkie strony, przechwytując, skalując i rysując każdą z nich po kolei.
  • Zapisywanie i otwieranie plików: Kończy nowy dokument i otwiera go dla użytkownika w celu sprawdzenia zmian.

Stosując to uporządkowane podejście, zapewniamy, że każda strona w PDF jest skalowana spójnie i zachowuje swój pierwotny porządek, co skutkuje profesjonalnie przetworzonym dokumentem.

Delphi wydanie:

Użyj Delphi, aby przeskalować strony PDF o 70%:

Zakreślacz składni Urvanov 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;
[Czas formatowania: 0,0011 sekundy]

VB.Net wydanie

Oto kod VB.Net do wykonania zadania:

Zakreślacz składni Urvanov 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
[Czas formatowania: 0,0008 sekundy]

Obie wersje kodu VB.Net i Delphi osiągają ten sam wynik, co oryginalny kod C#, zapewniając, że każda strona w PDF zostanie zmniejszona o 70% przy zachowaniu oryginalnej kolejności.