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:
- Ładowanie pliku PDF.
- Przechwytywanie i skalowanie każdej strony.
- Zapisywanie przeskalowanych stron do nowego pliku PDF.
Kroki do osiągnięcia celu
- Zainicjuj środowisko:
- Załaduj oryginalny plik PDF.
- Usuń wcześniej istniejący plik wyjściowy, aby uniknąć konfliktów.
- Skonfiguruj parametry skalowania:
- Zdefiniuj współczynnik skalowania (70%).
- Oblicz granice wymagane do wyśrodkowania skalowanej zawartości.
- 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.
- 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:
|
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"); |
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; |
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 |
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.