Изменение размера страницы PDF с помощью C#

Изменение размера страницы PDF

Aspose.PDF для .NET позволяет изменять размер страницы PDF с помощью нескольких строк кода в ваших .NET приложениях. Эта тема объясняет, как обновить/изменить размеры страницы (размер) существующего PDF файла.

Следующий фрагмент кода также работает с библиотекой Aspose.PDF.Drawing.

Класс Page содержит метод SetPageSize(…), который позволяет установить размер страницы. Приведенный ниже фрагмент кода обновляет размеры страницы в несколько простых шагов:

  1. Загрузите исходный PDF-файл.
  2. Получите страницы в объект PageCollection.
  3. Получите данную страницу.
  4. Вызовите метод SetPageSize(..), чтобы обновить его размеры.
  5. Вызовите метод Save(..) класса Document, чтобы сгенерировать PDF-файл с обновленными размерами страниц.

Следующий фрагмент кода показывает, как изменить размеры страницы PDF на размер A4.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open document
Document pdfDocument = new Document(dataDir + "UpdateDimensions.pdf");
// Get page collection
PageCollection pageCollection = pdfDocument.Pages;
// Get particular page
Page pdfPage = pageCollection[1];
// Set the page size as A4 (11.7 x 8.3 in) and in Aspose.Pdf, 1 inch = 72 points
// So A4 dimensions in points will be (842.4, 597.6)
pdfPage.SetPageSize(597.6, 842.4);
dataDir = dataDir + "UpdateDimensions_out.pdf";
// Save the updated document
pdfDocument.Save(dataDir);

Получить размер страницы PDF

Вы можете прочитать размер страницы PDF существующего файла PDF, используя Aspose.PDF для .NET. Следующий пример кода показывает, как считать размеры страницы PDF с помощью C#.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open document
Document pdfDocument = new Document(dataDir + "UpdateDimensions.pdf");
// Adds a blank page to pdf document
Page page = pdfDocument.Pages.Count > 0 ? pdfDocument.Pages[1] : pdfDocument.Pages.Add();
// Get page height and width information
Console.WriteLine(page.GetPageRect(true).Width.ToString() + ":" + page.GetPageRect(true).Height);
// Rotate page at 90 degree angle
page.Rotate = Rotation.on90;
// Get page height and width information
Console.WriteLine(page.GetPageRect(true).Width.ToString() + ":" + page.GetPageRect(true).Height);