Extract

Generates a new PDF consisting of the pages extracted from a given pdf.

Code Samples

Try the API in the language you prefer

  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
// create extract object
Extract extract = new Extract()
{
    // document
    Document = new Document()
    {
        DocData = File.ReadAllBytes("myPdf.pdf"),
        Name = "myPdf.pdf",
    },
    // action
    ExtractAction = new ExtractAction()
    {
        // list of pages to be extracted
        ExtractPages = new System.Collections.Generic.HashSet() { 1, 4 },
    }
};

// extraction
ExtractRes res = await Pdf4meClient.Pdf4me.Instance.ExtractClient.ExtractAsync(extract);

// extracting the generated PDF and writing it to disk
byte[] extractedPdf = res.Document.DocData;
File.WriteAllBytes("extractedPdf.pdf", extractedPdf);
// setup the extractClient
ExtractClient extractClient = new ExtractClient(pdf4meClient);

// create extract object
Extract extract = new Extract();
// document
Document document = new Document();
document.setDocData(Files.readAllBytes(Paths.get("myPdf.pdf")));
extract.setDocument(document);
// action
ExtractAction extractAction = new ExtractAction();
extractAction.setExtractPages(Arrays.asList(1, 4));
extract.setExtractAction(extractAction);

// extraction
ExtractRes res = extractClient.extract(extract);

// extracting the generated PDF and writing it to disk
byte[] extractedPdf = res.getDocument().getDocData();
FileUtils.writeByteArrayToFile(new File("extractedPdf.pdf"), extractedPdf);
// setup the pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')

// create extract object
const extractReq = {
  // document
  document: {
    docData: fs.readFileSync(path.join(__dirname, 'myPdf.pdf')).toString('base64'),
  },
  // action
  extractAction: {
    extractPages: [1, 4],
  },
}

// extraction
pdf4meClient.extract(extractReq)
  .then(function(extractRes) {
    // extracting the generated PDF and writing it to disk
    const pdfDocument = Buffer.from(extractRes.document.docData, 'base64')
    fs.writeFileSync(path.join(__dirname, 'extractedPdf.pdf'), pdfDocument)
  })
  .catch(error => {
    console.log(error)
  })
# setup the extract_client
extract_client = ExtractClient(pdf4me_client)

# create the extract object
extract = Extract(
    # document
    document=Document(
        doc_data=FileReader().get_file_data('myPdf.pdf')
    ),
    # action
    extract_action=ExtractAction(
        extract_pages=[1,4]
    )
)

# extraction
res = extract_client.extract(extract=extract)

# extracting the generated PDF and writing it to disk 
extracted_pdf = base64.b64decode(res['document']['doc_data'])
with open('extractedPdf.pdf', 'wb') as f:
    f.write(extracted_pdf)
// create extract object
$create_exrtract = [
    //document
    "document" => [
        "docData" => $client->getFileData('myPdf.pdf')
    ],
    //action
    "extractAction" => [
        "extractPages" => [
            1,
            4
        ]
    ]
];

// extraction
$extractedPdf = $client->pdf4me()->extract($create_extract);

// extracting the generated PDF and writing it to disk
$extractedPdf = base64_decode($createExtract->document->docData);
file_put_contents('extractedPdf.pdf', $extractedPdf);
file_path = './myPdf.pdf'

 action = Pdf4me::Extract.new(
        # document
        document: Pdf4me::Document.new(
          doc_data: Base64.encode64(File.open(file_path, 'rb', &:read))
        ),
        # action
        extract_action: Pdf4me::ExtractAction.new(
          extract_pages: [1, 4]
        ),
       
    )
response = action.run

    # saving extracted pages
    File.open('/extractedPdf.pdf', 'wb') do |f|
      f.write(Base64.decode64(response.document.doc_data))
    end

Important Links

Swagger - Extract