2022-02-28 21:00:25 +01:00
|
|
|
|
//
|
|
|
|
|
// PDF to text program for PDFio.
|
|
|
|
|
//
|
2024-12-23 01:00:17 +01:00
|
|
|
|
// Copyright © 2022-2024 by Michael R Sweet.
|
2022-02-28 21:00:25 +01:00
|
|
|
|
//
|
|
|
|
|
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
|
|
|
|
// information.
|
|
|
|
|
//
|
|
|
|
|
// Usage:
|
|
|
|
|
//
|
2024-12-23 01:00:17 +01:00
|
|
|
|
// ./pdf2text FILENAME.pdf > FILENAME.txt
|
2022-02-28 21:00:25 +01:00
|
|
|
|
//
|
|
|
|
|
|
2024-12-23 01:00:17 +01:00
|
|
|
|
#include <pdfio.h>
|
2022-03-01 15:18:56 +01:00
|
|
|
|
#include <string.h>
|
2022-02-28 21:00:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// 'main()' - Main entry.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
int // O - Exit status
|
|
|
|
|
main(int argc, // I - Number of command-line arguments
|
|
|
|
|
char *argv[]) // I - Command-line arguments
|
|
|
|
|
{
|
|
|
|
|
pdfio_file_t *file; // PDF file
|
|
|
|
|
size_t i, j, // Looping vars
|
|
|
|
|
num_pages, // Number of pages
|
|
|
|
|
num_streams; // Number of streams for page
|
|
|
|
|
pdfio_obj_t *obj; // Current page object
|
|
|
|
|
pdfio_stream_t *st; // Current page content stream
|
|
|
|
|
char buffer[1024]; // String buffer
|
|
|
|
|
bool first; // First string token?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify command-line arguments...
|
|
|
|
|
if (argc != 2)
|
|
|
|
|
{
|
2024-12-23 01:00:17 +01:00
|
|
|
|
puts("Usage: pdf2text FILENAME.pdf > FILENAME.txt");
|
2022-02-28 21:00:25 +01:00
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open the PDF file...
|
2024-12-23 01:00:17 +01:00
|
|
|
|
if ((file = pdfioFileOpen(argv[1], /*password_cb*/NULL, /*password_data*/NULL, /*error_cb*/NULL, /*error_data*/NULL)) == NULL)
|
2022-02-28 21:00:25 +01:00
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
|
|
// Try grabbing content from all of the pages...
|
|
|
|
|
for (i = 0, num_pages = pdfioFileGetNumPages(file); i < num_pages; i ++)
|
|
|
|
|
{
|
|
|
|
|
if ((obj = pdfioFileGetPage(file, i)) == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
num_streams = pdfioPageGetNumStreams(obj);
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < num_streams; j ++)
|
|
|
|
|
{
|
|
|
|
|
if ((st = pdfioPageOpenStream(obj, j, true)) == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-12-23 03:29:32 +01:00
|
|
|
|
// Read PDF tokens from the page stream...
|
2022-02-28 21:00:25 +01:00
|
|
|
|
first = true;
|
|
|
|
|
while (pdfioStreamGetToken(st, buffer, sizeof(buffer)))
|
|
|
|
|
{
|
|
|
|
|
if (buffer[0] == '(')
|
|
|
|
|
{
|
2024-12-23 03:29:32 +01:00
|
|
|
|
// Text string using an 8-bit encoding
|
2022-02-28 21:00:25 +01:00
|
|
|
|
if (first)
|
|
|
|
|
first = false;
|
2024-12-23 01:00:17 +01:00
|
|
|
|
else if (buffer[1] != ' ')
|
2022-03-01 15:18:56 +01:00
|
|
|
|
putchar(' ');
|
2022-02-28 21:00:25 +01:00
|
|
|
|
|
|
|
|
|
fputs(buffer + 1, stdout);
|
|
|
|
|
}
|
2022-03-01 15:18:56 +01:00
|
|
|
|
else if (!strcmp(buffer, "Td") || !strcmp(buffer, "TD") || !strcmp(buffer, "T*") || !strcmp(buffer, "\'") || !strcmp(buffer, "\""))
|
|
|
|
|
{
|
2024-12-23 03:29:32 +01:00
|
|
|
|
// Text operators that advance to the next line in the block
|
2022-03-01 15:18:56 +01:00
|
|
|
|
putchar('\n');
|
|
|
|
|
first = true;
|
|
|
|
|
}
|
2022-02-28 21:00:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
|
|
pdfioStreamClose(st);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pdfioFileClose(file);
|
|
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
|
}
|