ページプロパティの操作

既存のPDFファイルからPDFページプロパティを取得する

PdfPageEditorを使用すると、PDFファイルの個々のページを操作できます。これにより、異なるページボックスサイズ、ページ回転、ページズームなどの個々のページのプロパティを取得できます。これらのプロパティを取得するには、PdfPageEditorオブジェクトを作成し、BindPdfメソッドを使用して入力PDFファイルをバインドする必要があります。その後、GetPageRotationGetPagesGetPageBoxSizeなどの異なるメソッドを使用してページプロパティを取得できます。

以下のコードスニペットは、既存のPDFファイルからPDFページプロパティを取得する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetPdfPageProperties()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Open PDF document
    using (var pageEditor = new Aspose.Pdf.Facades.PdfPageEditor())
    {
        // Bind PDF document
        pageEditor.BindPdf(dataDir + "input.pdf");

        // Get page properties and print them to the console
        Console.WriteLine($"Page 1 Rotation: {pageEditor.GetPageRotation(1)}");
        Console.WriteLine($"Total Pages: {pageEditor.GetPages()}");
        Console.WriteLine($"Trim Box Size of Page 1: {pageEditor.GetPageBoxSize(1, "trim")}");
        Console.WriteLine($"Art Box Size of Page 1: {pageEditor.GetPageBoxSize(1, "art")}");
        Console.WriteLine($"Bleed Box Size of Page 1: {pageEditor.GetPageBoxSize(1, "bleed")}");
        Console.WriteLine($"Crop Box Size of Page 1: {pageEditor.GetPageBoxSize(1, "crop")}");
        Console.WriteLine($"Media Box Size of Page 1: {pageEditor.GetPageBoxSize(1, "media")}");
    }
}

既存のPDFファイルにPDFページプロパティを設定する

ページ回転、ズーム、または原点などのページプロパティを設定するには、PdfPageEditorクラスを使用する必要があります。このクラスは、これらのページプロパティを設定するためのさまざまなメソッドとプロパティを提供します。まず、PdfPageEditorクラスのオブジェクトを作成し、BindPdfメソッドを使用して入力PDFファイルをバインドする必要があります。その後、これらのメソッドとプロパティを使用してページプロパティを設定できます。最後に、Saveメソッドを使用して更新されたPDFファイルを保存します。

以下のコードスニペットは、既存のPDFファイルにPDFページプロパティを設定する方法を示しています。

 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetPdfPageProperties()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Open PDF document
    using (var pageEditor = new Aspose.Pdf.Facades.PdfPageEditor())
    {
        // Bind PDF document
        pageEditor.BindPdf(dataDir + "input.pdf");

        // Set page properties
        // Move origin from (0,0)
        pageEditor.MovePosition(100, 100);

        // Set page rotations
        var pageRotations = new System.Collections.Hashtable
        {
            { 1, 90 },
            { 2, 180 },
            { 3, 270 }
        };

        // Set zoom where 1.0f = 100% zoom
        pageEditor.Zoom = 2.0f;

        // Save PDF document
        pageEditor.Save(dataDir + "SetPageProperties_out.pdf");
    }
}

PDFファイルの特定のページのページコンテンツをリサイズする

PdfPageEditorクラスのResizeContentsメソッドを使用すると、PDFファイル内のページコンテンツをリサイズできます。ContentsResizeParametersクラスは、ページをリサイズするために使用されるパラメータ(例えば、パーセンテージまたは単位でのマージンなど)を指定するために使用されます。すべてのページをリサイズするか、リサイズするページの配列を指定することができます。

以下のコードスニペットは、PDFファイルの特定のページのコンテンツをリサイズする方法を示しています。

  // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void ResizePdfPageContents()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     // Create PdfFileEditor Object
     var fileEditor = new Aspose.Pdf.Facades.PdfFileEditor();

     // Open PDF Document
     using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
     {
         // Specify Parameters to be used for resizing
         var parameters = new Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeParameters(
             // Left margin = 10% of page width
             PdfFileEditor.ContentsResizeValue.Percents(10),
             // New contents width calculated automatically as width - left margin - right margin (100% - 10% - 10% = 80%)
             null,
             // Right margin is 10% of page
             PdfFileEditor.ContentsResizeValue.Percents(10),
             // Top margin = 10% of height
             PdfFileEditor.ContentsResizeValue.Percents(10),
             // New contents height is calculated automatically (similar to width)
             null,
             // Bottom margin is 10%
             PdfFileEditor.ContentsResizeValue.Percents(10)
         );

         // Resize Page Contents
         fileEditor.ResizeContents(document, new[] { 1, 2 }, parameters);

         // Save PDF document
         document.Save(dataDir + "ResizePageContents_out.pdf");
     }
 }