Extracting a Specific Region from a PDF Page without Using a Crop Box in Aspose.PDF

How can I use Aspose.PDF to extract a particular area from a specific page in a PDF file and create a new PDF without utilizing the crop box method? This process involves manipulating the page content at a granular level, rather than simply applying a rectangular crop box. I need to access and handle individual elements like text, images, and shapes within the PDF document using the Aspose.PDF API. The goal is to identify the desired region, extract its content, and then generate a new PDF containing only that extracted content. Could you provide a code example or a detailed explanation of the steps involved in achieving this task? Keep in mind that I’m looking for a solution that doesn’t rely on the traditional crop box approach.

To achieve your goal of extracting a particular area from a specific page in a PDF file and creating a new PDF without utilizing the crop box method, you can use the Aspose.PDF API to manipulate the page content at a granular level. This involves accessing and handling individual elements like text, images, and shapes within the PDF document. Here’s a step-by-step guide on how to accomplish this task:

Load the PDF Document: Load the PDF document using Aspose.PDF.

Access the Page: Access the specific page from which you want to extract the content.

Identify the Desired Region: Define the coordinates or boundaries of the desired region on the page.

Extract Content: Iterate through the page contents and identify the elements (text, images, shapes, etc.) that fall within the defined region.

Create a New PDF Document: Create a new PDF document using Aspose.PDF.

Add Extracted Content: Add the extracted content (text, images, shapes, etc.) to the new PDF document.

Save the New PDF Document: Save the new PDF document with the extracted content.

Below is a sample code demonstrating how to implement the above steps using Aspose.PDF API in C#:

CODE :

using Aspose.Pdf;
using Aspose.Pdf.Text;
using Aspose.Pdf.Text.TextOptions;

class Program
{
    static void Main(string[] args)
    {
        // Load the PDF document
        Document pdfDocument = new Document("input.pdf");

        // Access the specific page (e.g., page 1)
        Page pdfPage = pdfDocument.Pages[1];

        // Define the coordinates of the desired region (e.g., x, y, width, height)
        double x = 100; // Example: x-coordinate of the top-left corner of the region
        double y = 100; // Example: y-coordinate of the top-left corner of the region
        double width = 200; // Example: width of the region
        double height = 150; // Example: height of the region

        // Iterate through the page contents and extract content within the defined region
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
        textFragmentAbsorber.TextSearchOptions.Rectangle = new Aspose.Pdf.Rectangle(x, y, x + width, y + height);
        pdfPage.Accept(textFragmentAbsorber);
        TextFragmentCollection textFragments = textFragmentAbsorber.TextFragments;

        // Create a new PDF document
        Document newPdfDocument = new Document();

        // Add extracted text to the new PDF document
        foreach (TextFragment textFragment in textFragments)
        {
            newPdfDocument.Pages.Add().Paragraphs.Add(new TextFragment(textFragment.Text));
        }

        // Save the new PDF document
        newPdfDocument.Save("output.pdf");
    }
}

In this code:

  • We load the input PDF document.

  • Access a specific page (e.g., page 1).

  • Define the coordinates of the desired region.

  • Use TextFragmentAbsorber to extract text within the defined region.

  • Create a new PDF document and add the extracted text to it.

  • Save the new PDF document with the extracted content.

***You can further extend this code to handle other customer satisfaction and feedback portal
types of elements like images and shapes if ***
needed, by using appropriate classes and methods provided by Aspose.PDF API.

Hello, this other example is just text. If I need shapes or images, what API should I use

@78783718

Extracting the content from a specific region of a PDF can be challenging if you want to include images, graphics and shapes. It is not determined at existing PDF level how these elements were added in the first place. Therefore, we may need to perform some investigation. Can you please share you sample PDF along with expected output PDF so that we can test the scenario in our environment and address it accordingly.

cc01.pdf (8.1 MB)

Extract a new PDF file from multiple rectangular boxes on top of the PDF file, instead of cropping the boxes。 This is a very practical feature

@78783718

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56887

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.