How to crop page with right (x,y,w,h)?

Hi,Support:
How can I get the right result by cropping pdf page with the cropping parameters as ((x,y,w,h))? If the parameter is set to (0,0,585,485), the size of the result page is wrong, which is not a rectange page as (0,0,585,485) but as ( (0,0,585,10) ).

How to set the right crop parameter?

@ducaisoft

You can please try using the below sample code and if it still produces incorrect results, please let us know. We will further proceed to assist you accordingly.

// Load the PDF document
Document pdfDocument = new Document("input.pdf");

// Get the first page
Page page = pdfDocument.Pages[1];

// Set the cropping parameters
float x = 100;  // X-coordinate of the top-left corner
float y = 200;  // Y-coordinate of the top-left corner
float w = 300;  // Width
float h = 400;  // Height

// Crop the page
page.CropBox = new Rectangle(x, y, x + w, y + h);

// Save the cropped PDF
pdfDocument.Save("output.pdf");

Thanks!
It works perfectly!
However, a new issue has been found like that:

  1. The size of pdf-page in the input pdf file is 585x858, and the page count of the input pdf file is 2 pages;
  2. The input pdf file has been horizontally split into trhee parts, therefore, there are 6 pages in the output pdf file.
  3. the size of the output pdf file should be 585x(858/3) =585x286, however, the height of pagesize of the output pdf file is actually 858 not 286, which is wrong value.

What’s for this?

Input.pdf (62.2 KB)

Output.pdf (345.8 KB)

@ducaisoft

Can you please share what method are you using to calculate the page dimensions? Please also try using the below code snippet to see it correctly produces the output:

// Load the input PDF document
Document pdfDocument = new Document("input.pdf");

// Define the number of parts you want to split the page into (in your case, 3)
int numParts = 3;

// Calculate the height for each part
float partHeight = pdfDocument.Pages[1].Rect.Height / numParts;

// Create a new Document to store the output pages
Document outputDocument = new Document();

// Iterate through the input pages
foreach (Page inputPage in pdfDocument.Pages)
{
    // Split each input page into multiple output pages
    for (int i = 0; i < numParts; i++)
    {
        // Create a new output page with the desired size
        Page outputPage = outputDocument.Pages.Add();
        outputPage.PageInfo.Height = partHeight;
        outputPage.PageInfo.Width = inputPage.Rect.Width;

        // Calculate the crop box for the output page based on the part index
        float cropY = i * partHeight;
        outputPage.CropBox = new Rectangle(inputPage.Rect.Left, cropY, inputPage.Rect.Right, cropY + partHeight);

        // Copy the content from the input page to the output page
        foreach (Operator op in inputPage.Contents)
        {
            outputPage.Contents.Add(op);
        }
    }
}

// Save the output PDF document
outputDocument.Save("output.pdf");

Thanks!
By your reference, I work it out perfectly using the following function:

Private Function TestSplit(ByVal InputPdf As String, ByVal OutputPdf As String, ByVal FileIndex As Integer, _
                           Optional ByVal Row As Short = 3, Optional ByVal Col As Short = 4) As Boolean
    '// Load the input PDF document
    Dim pdfDocument As Aspose.Pdf.Document = New Aspose.Pdf.Document(InputPdf)
    '// Define the number of parts you want to split the page into (in your case, 3)
    Dim numParts As Integer = Row
    '// Calculate the height for each part
    Dim partHeight As Double = pdfDocument.Pages(1).Rect.Height / numParts
    '// Create a New Document to store the output pages
    Dim outputDocument As Aspose.Pdf.Document = New Aspose.Pdf.Document()
    '// Iterate through the input pages
    For Each inputPage As Page In pdfDocument.Pages
        '// Split each input page into multiple output pages
        For i As Short = numParts - 1 To 0 Step -1
            '// Create a New output page with the desired size
            On Error Resume Next
            Dim outputPage As Page = outputDocument.Pages.Add()
            If Err.Number Then Exit For
            outputPage.PageInfo.Height = partHeight
            outputPage.PageInfo.Width = inputPage.Rect.Width
            '// Calculate the crop box for the output page based on the part index
            Dim cropY As Double = i * partHeight
            outputPage.CropBox = New Rectangle(inputPage.Rect.LLX, cropY, inputPage.Rect.URX, cropY + partHeight)
            '// Copy the content from the input page to the output page
            For Each op As [Operator] In inputPage.Contents
                outputPage.Contents.Add(op)
            Next
            Application.DoEvents()
        Next
    Next
    '// Save the output PDF document
    outputDocument.Save(AppPath & "\Tmp.pdf")
    outputDocument.Dispose()
    outputDocument = Nothing
    pdfDocument.Dispose()
    pdfDocument = Nothing
    Dim PdfEditor As PdfFileEditor = New PdfFileEditor()
    '// Load the input PDF document
    pdfDocument = New Aspose.Pdf.Document(AppPath & "\Tmp.pdf")
    '// Define the number of parts you want to split the page into (in your case, 3)
    numParts = Col
    '// Calculate the height for each part
    Dim partWidth As Double = pdfDocument.Pages(1).Rect.Width / numParts
    '// Create a New Document to store the output pages
    outputDocument = New Aspose.Pdf.Document()
    '// Iterate through the input pages
    Dim Pages As Integer = pdfDocument.Pages.Count
    'For Each inputPage As Page In pdfDocument.Pages
    For ii As Integer = 1 To Pages
        Dim inputPage As Aspose.Pdf.Page = pdfDocument.Pages(ii)
        '// Split each input page into multiple output pages
        For i As Short = 0 To numParts - 1
            '// Create a New output page with the desired size
            On Error Resume Next
            Dim outputPage As Page = outputDocument.Pages.Add()
            If Err.Number Then Exit For
            outputPage.PageInfo.Height = partHeight
            outputPage.PageInfo.Width = inputPage.Rect.Width
            '// Calculate the crop box for the output page based on the part index
            Dim cropX As Double = i * partWidth
            outputPage.CropBox = New Rectangle(cropX, inputPage.Rect.LLY, cropX + partWidth, inputPage.Rect.URY)
            '// Copy the content from the input page to the output page
            For Each op As [Operator] In inputPage.Contents
                outputPage.Contents.Add(op)
            Next
            Application.DoEvents()
        Next
    Next
    '// Save the output PDF document
    Do
        On Error Resume Next
        outputDocument.Save(OutputPdf)
        If Err.Number = 61 Then
            Rc = MsgBox(OutputPdf.Substring(0, 1) & "Disk is full, Please release space for trying save again!", MsgBoxStyle.Exclamation + MsgBoxStyle.RetryCancel, "Message")
            If Rc = MsgBoxResult.Cancel Then Exit Do
        End If
    Loop While Err.Number = 61 And Not abort
End Function

Input.pdf (62.2 KB)

output.pdf (28.4 KB)

@ducaisoft

Its nice to know that you were able to achieve your requirements. Please keep using the API and feel free to create a new topic in case you need further assistance.