C#로 PDF 페이지 크기 변경

C#로 PDF 페이지 크기 변경

Aspose.PDF for .NET을 사용하면 .NET 애플리케이션에서 간단한 코드로 PDF 페이지 크기를 변경할 수 있습니다. 이 주제에서는 기존 PDF 파일의 페이지 치수(크기)를 업데이트/변경하는 방법을 설명합니다.

다음 코드 스니펫은 Aspose.PDF.Drawing 라이브러리와 함께 작동합니다.

Page 클래스에는 페이지 크기를 설정할 수 있는 SetPageSize(…) 메서드가 포함되어 있습니다. 아래의 코드 스니펫은 몇 가지 간단한 단계로 페이지 치수를 업데이트합니다:

  1. 소스 PDF 파일을 로드합니다.
  2. PageCollection 객체에 페이지를 가져옵니다.
  3. 주어진 페이지를 가져옵니다.
  4. SetPageSize(..) 메서드를 호출하여 치수를 업데이트합니다.
  5. Document 클래스의 Save(..) 메서드를 호출하여 업데이트된 페이지 치수로 PDF 파일을 생성합니다.

다음 코드 스니펫은 PDF 페이지 치수를 A4 크기로 변경하는 방법을 보여줍니다.

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "UpdateDimensions.pdf"))
    {
        // Get page collection
        var pageCollection = document.Pages;
        // Get particular page
        var 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);
        // Save PDF document
        document.Save(dataDir + "UpdateDimensions_out.pdf"); 
    }
}

PDF 페이지 크기 가져오기

Aspose.PDF for .NET을 사용하여 기존 PDF 파일의 PDF 페이지 크기를 읽을 수 있습니다. 다음 코드 샘플은 C#을 사용하여 PDF 페이지 치수를 읽는 방법을 보여줍니다.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetPdfPageSize()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
    
    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "UpdateDimensions.pdf"))
    {
        // Adds a blank page to pdf document
        Page page = document.Pages.Count > 0 ? document.Pages[1] : document.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);
    }
}