PDFページサイズをC#で変更する
PDFページサイズの変更
Aspose.PDF for .NETを使用すると、.NETアプリケーションでPDFページサイズを簡単なコードで変更できます。このトピックでは、既存のPDFファイルのページ寸法(サイズ)を更新/変更する方法について説明します。
以下のコードスニペットは、Aspose.PDF.Drawing ライブラリでも機能します。
Page クラスには、SetPageSize(…) メソッドが含まれており、ページサイズを設定できます。以下のコードスニペットは、数ステップでページ寸法を更新します:
- ソースPDFファイルを読み込む。
- PageCollection オブジェクトにページを取得する。
- 特定のページを取得する。
- SetPageSize(..) メソッドを呼び出してその寸法を更新する。
- Document クラスの Save(..) メソッドを呼び出して、ページ寸法が更新された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ページサイズを読み取ることができます。次のコードサンプルは、C#を使用してPDFページの寸法を読み取る方法を示しています。
// 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); |