PDF 문서에 페이지 추가

Aspose.PDF for .NET API는 C# 또는 기타 .NET 언어를 사용하여 PDF 문서의 페이지를 작업할 수 있는 완전한 유연성을 제공합니다. PDF 문서의 모든 페이지는 PageCollection에서 유지되며, 이를 사용하여 PDF 페이지를 작업할 수 있습니다. Aspose.PDF for .NET은 PDF 문서의 파일 내의 원하는 위치에 페이지를 삽입할 수 있을 뿐만 아니라 PDF 파일의 끝에 페이지를 추가할 수 있습니다. 이 섹션에서는 C#을 사용하여 PDF에 페이지를 추가하는 방법을 보여줍니다.

PDF 파일에 페이지 추가 또는 삽입

Aspose.PDF for .NET은 PDF 문서의 파일 내의 원하는 위치에 페이지를 삽입할 수 있을 뿐만 아니라 PDF 파일의 끝에 페이지를 추가할 수 있습니다.

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

원하는 위치에 PDF 파일에 빈 페이지 삽입

PDF 파일에 빈 페이지를 삽입하려면:

  1. 입력 PDF 파일로 Document 클래스 객체를 생성합니다.
  2. 지정된 인덱스와 함께 PageCollection 컬렉션의 Insert 메서드를 호출합니다.
  3. Save 메서드를 사용하여 출력 PDF를 저장합니다.

다음 코드 스니펫은 PDF 파일에 페이지를 삽입하는 방법을 보여줍니다.

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "InsertEmptyPage.pdf"))
    {
       // Insert an empty page in a PDF
       document.Pages.Insert(2);
        // Save PDF document
       document.Save(dataDir + "InsertEmptyPage_out.pdf");
    }
}

위의 예에서는 기본 매개변수로 빈 페이지를 추가했습니다. 문서의 다른 페이지와 동일한 페이지 크기를 원한다면 몇 줄의 코드를 추가해야 합니다:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void InsertAnEmptyPageWithParameters()
{
    var page = document.Pages.Insert(2);
    //copy page parameters from page 1
    page.ArtBox = document.Pages[1].ArtBox;
    page.BleedBox = document.Pages[1].BleedBox;
    page.CropBox = document.Pages[1].CropBox;
    page.MediaBox = document.Pages[1].MediaBox;
    page.TrimBox = document.Pages[1].TrimBox;
}

PDF 파일 끝에 빈 페이지 추가

때때로 문서가 빈 페이지로 끝나도록 보장하고 싶습니다. 이 주제에서는 PDF 문서의 끝에 빈 페이지를 삽입하는 방법을 설명합니다.

PDF 파일 끝에 빈 페이지를 삽입하려면:

  1. 입력 PDF 파일로 Document 클래스 객체를 생성합니다.
  2. 매개변수 없이 PageCollection 컬렉션의 Add 메서드를 호출합니다.
  3. Save 메서드를 사용하여 출력 PDF를 저장합니다.

다음 코드 스니펫은 PDF 파일 끝에 빈 페이지를 삽입하는 방법을 보여줍니다.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void InsertAnEmptyPageAtTheEnd()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
    
    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "InsertEmptyPageAtEnd.pdf"))
    {
        // Insert an empty page at the end of a PDF file
        document.Pages.Add();
        // Save PDF document
        document.Save(dataDir + "InsertEmptyPageAtEnd_out.pdf");
    }
}