Split PDF

Document splitting like the extraction of pages may be useful for separating the documents into various portions. This feature of PDF4me is used for faster distribution of, possibly, the relevant information in parts of PDF documents

The Split functionality splits a PDF of multiple pages into a number of smaller PDF documents. In addition, it can split Pdf periodically, say after every ‘x’ page. Most commonly used document formats can be converted to Pdf using the convert function in Pdf4me and split into smaller Pdfs as required.

Code Samples

Try the API in the language you prefer

  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
//create split object
var split = new Split()
{
    // document
    Document = new Document()
    {
        DocData = File.ReadAllBytes("myPdf.pdf"),
        Name = "myPdf.pdf",
    },
    // action
    SplitAction = new SplitAction()
    {
        SplitAfterPage = 2
    },
};

// splitting the PDF
var res = await Pdf4meClient.Pdf4me.Instance.SplitClient.SplitAsync(split);

// extract the resulting documents and writing them to disk
byte[] pdf1 = res.Documents[0].DocData;
byte[] pdf2 = res.Documents[1].DocData;
File.WriteAllBytes("pdf1.pdf", pdf1);
File.WriteAllBytes("pdf2.pdf", pdf2);
// setup the splitClient
SplitClient splitClient = new SplitClient(pdf4meClient);

//create split object
Split split = new Split();
// document
Document document = new Document();
document.setDocData(Files.readAllBytes(Paths.get("myPdf.pdf")));
split.setDocument(document);

// action
SplitAction splitAction = new SplitAction();
splitAction.setSplitAfterPage(2);
split.setSplitAction(splitAction);

// splitting the PDF
SplitRes res = splitClient.split(split);

// extracting the generated PDFs and writing them to disk
List documents = res.getDocuments();
byte[] pdf1 = documents.get(0).getDocData();
FileUtils.writeByteArrayToFile(new File("pdf1.pdf"), pdf1);
byte[] pdf2 = documents.get(1).getDocData();
FileUtils.writeByteArrayToFile(new File("pdf2.pdf"), pdf2);
// setup the pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')

// create the Split object
const splitReq = {
  // document
  document: {
    docData: fs.readFileSync(path.join(__dirname, 'myPdf.pdf')).toString('base64'),
  },
  // action
  splitAction: {
    splitAfterPage: 2,
  },
}

// splitting the PDF
pdf4meClient.split(splitReq)
  .then(splitRes => {
    // extract the resulting documents and writing them to disk
    const pdf1 = Buffer.from(splitRes.documents[0].docData, 'base64')
    const pdf2 = Buffer.from(splitRes.documents[0].docData, 'base64')
    fs.writeFileSync(path.join(__dirname, 'pdf1.pdf'), pdf1)
    fs.writeFileSync(path.join(__dirname, 'pdf2.pdf'), pdf2)
  })
  .catch(error => {
    console.error(error)
  })
# setup the split_client
split_client = SplitClient(pdf4me_client)

# create the split object
split = Split(
    # document
    document=Document(
        doc_data=FileReader().get_file_data('myPdf.pdf')
    ),
    # action
    split_action=SplitAction(
        split_after_page=2
    )
)

# splitting the PDF
res = split_client.split(split=split)

# extracting the generated PDFs
documents = res['documents']
pdf_1 = base64.b64decode(documents[0]['doc_data'])
pdf_2 = base64.b64decode(documents[1]['doc_data'])
# writing them to disk
with open('pdf1.pdf', 'wb') as f:
    f.write(pdf_1)
with open('pdf2.pdf', 'wb') as f:
    f.write(pdf_2)
// create the Split object
$create_split = [
    // document
    "document" => [
        'name' => 'myPdf.pdf',
        'docData' => $client->getFileData('myPdf.pdf')
    ],
    // action
    "splitAction" => [
        "splitAfterPage" => 2
    ]
];

// splitting the PDF
$res = $client->pdf4me()->splitPdf($create_split);

// extracting the generated PDFs
$pdf1 = base64_decode($res->documents[0]->docData);
$pdf2 = base64_decode($res->documents[1]->docData);

// and writing them to file
file_put_contents('pdf1.pdf', $pdf1);
file_put_contents('pdf2.pdf', $pdf2);
file_path = './in/GraphicsTest.pdf'

action = Pdf4me::Split.new(
    # document
    document: Pdf4me::Document.new(
        doc_data: Base64.encode64(File.open(file_path, 'rb', &:read)),
    ),
    # action
    split_action: Pdf4me::SplitAction.new(
        split_after_page: 2
    )
)

# splitting the PDF
response = action.run

# save documents
response.documents.each_with_index do |document, index|
    File.open("./out/Split_#{index}.pdf", 'wb') do |f|
      f.write(Base64.decode64(document.doc_data))
    end
end

Important Links

Swagger: Split PDF