عند العمل مع ملفات PDF، غالبًا ما توجد متطلبات لتغيير حجم المحتوى لأغراض مختلفة. في هذا السيناريو، نهدف إلى تقليل حجم جميع الصفحات داخل ملف PDF بنسبة 70٪. يوضح هذا الدليل الخطوات اللازمة، ويجيب على الأسئلة ذات الصلة، ويقدم الحلول.
بيان المشكلة.
نحن بحاجة إلى تقليل حجم كل صفحة في مستند PDF بنسبة 70٪ مع الحفاظ على ترتيب الصفحات الأصلي. يتطلب ذلك:
- تحميل ملف PDF.
- التقاط وتغيير حجم كل صفحة.
- حفظ الصفحات التي تم تغيير حجمها في ملف PDF جديد.
خطوات لتحقيق الهدف.
- تهيئة البيئة:
- قم بتحميل ملف PDF الأصلي.
- احذف أي ملف إخراج موجود مسبقًا لتجنب التعارض.
- قم بتعيين معلمات القياس:
- حدد عامل القياس (70٪).
- احسب الحدود المطلوبة لتوسيط المحتوى المقاس.
- قم بمعالجة كل صفحة في حلقة:
- حدد الصفحة الأولى.
- التقط محتوى الصفحة.
- أنشئ صفحة جديدة بالأبعاد الأصلية.
- ارسم المحتوى الذي تم التقاطه وتوسيعه على الصفحة الجديدة.
- كرر العملية لكل الصفحات.
- احفظ ملف PDF الجديد وافتحه.
- احفظ الصفحات المعدلة في ملف PDF جديد.
- افتح تلقائيًا ملف PDF الجديد لمراجعة النتائج.
تنفيذ الكود.
هذا هو كود C# الذي ينفذ الخطوات المذكورة أعلاه:
|
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"); |
شرح وتبرير:
- حذف الملفات. يضمن مسح أي مخرجات سابقة لمنع حدوث أخطاء أو محتوى قديم.
- عامل القياس: عند تعيينه على 0.70، يقلل هذا من حجم المحتوى إلى 70٪ من الحجم الأصلي.
- حساب الحدود: يقوم بوضع المحتوى المقاس في منتصف أبعاد الصفحة الأصلية.
- حلقة معالجة الصفحات: يتكرر خلال جميع الصفحات، ويقوم بالتقاط وتوسيع ورسم كل صفحة بالتسلسل.
- حفظ الملفات وفتحها: ينهي هذا الإجراء إنشاء المستند الجديد ويفتحه للمستخدم للتحقق من التغييرات.
من خلال اتباع هذا النهج المنظم، نضمن أن كل صفحة في ملف PDF يتم تغيير حجمها بشكل متسق وتحافظ على ترتيبها الأصلي، مما ينتج عنه مستند مُعالج بشكل احترافي.
إصدار Delphi:
استخدم Delphi لتغيير حجم صفحات PDF بنسبة 70٪:
|
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:
هذا هو كود VB.Net لتنفيذ المهمة:
|
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 |
كلا من إصدار VB.Net وإصدار Delphi من الكود يحققان نفس نتيجة الكود الأصلي المكتوب بلغة C#، مما يضمن أن كل صفحة في ملف PDF يتم تصغير حجمها بنسبة 70٪ مع الحفاظ على الترتيب الأصلي.