-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfrecur.c
164 lines (138 loc) · 3.49 KB
/
pdfrecur.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* pdfrecur.c
*
* locate or remove the recurring blocks of text in a PDF document
*
* aimed at removing page numbers, headers and footers
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <poppler.h>
#include <cairo.h>
#include <cairo-pdf.h>
#include "pdfrects.h"
/*
* main
*/
int main(int argc, char *argv[]) {
int opt;
gboolean usage = FALSE;
char *infile, *outfile;
gdouble recurheight = -1, distance = -1;
gboolean draw = FALSE, clip = TRUE, noout = FALSE, usemain = FALSE;
PopplerDocument *doc;
PopplerPage *page;
int npages, n;
gdouble width, height;
RectangleList *textarea, *flist;
PopplerRectangle *maintext;
cairo_surface_t *surface;
cairo_t *cr;
/* arguments */
while ((opt = getopt(argc, argv, "s:t:mcdnh")) != -1)
switch(opt) {
case 's':
height = atof(optarg);
break;
case 't':
distance = atof(optarg);
break;
case 'm':
usemain = TRUE;
break;
case 'c':
clip = FALSE;
break;
case 'd':
draw = TRUE;
break;
case 'n':
noout = TRUE;
break;
case 'h':
usage = TRUE;
break;
}
if (! usage && argc - 1 < optind) {
printf("input file name missing\n");
usage = TRUE;
}
if (usage) {
printf("usage:\n");
printf("\tpdfrecur ");
printf("[-s height] [-t distance] [-d] [-m] [-c] [-n] ");
printf("[-h] file.pdf\n");
printf("\t\t-s height\tmaximal height of recurring text\n");
printf("\t\t-t distance\ttext-to-text distance\n");
printf("\t\t-d\t\tdraw a box around removed rectangles\n");
printf("\t\t-m\t\tuse the main text block in the page\n");
printf("\t\t-c\t\tdo not remove recurring rectangles\n");
printf("\t\t-n\t\tonly print recurring rectangles\n");
printf("\t\t-h\t\tthis help\n");
exit(EXIT_FAILURE);
}
infile = filenametouri(argv[optind]);
if (! infile)
exit(EXIT_FAILURE);
outfile = pdfaddsuffix(argv[optind], "norecur");
debugfrequent = 0x02 | 0x04;
/* open file */
doc = poppler_document_new_from_file(infile, NULL, NULL);
if (doc == NULL) {
printf("error opening pdf file\n");
exit(EXIT_FAILURE);
}
/* pages */
npages = poppler_document_get_n_pages(doc);
if (npages < 1) {
printf("no page in document\n");
exit(EXIT_FAILURE);
}
/* find the recurring text blocks */
flist = rectanglevector_frequent(doc, recurheight, distance);
if (usemain) {
maintext = rectanglevector_main(doc, flist,
recurheight, distance);
printf("maintext:\n");
rectangle_printyaml(stdout, " - ", " ", maintext);
}
if (noout)
return 0;
/* copy to destination */
surface = cairo_pdf_surface_create(outfile, 1, 1);
printf("infile: %s\n", argv[optind]);
printf("outfile: %s\n", outfile);
printf("pages: \n");
for (n = 0; n < npages; n++) {
printf(" - page: %d\n", n);
page = poppler_document_get_page(doc, n);
poppler_page_get_size(page, &width, &height);
cairo_pdf_surface_set_size(surface, width, height);
cr = cairo_create(surface);
textarea = rectanglelist_textarea_distance(page, distance);
cairo_save(cr);
if (clip) {
if (usemain) {
rectangle_cairo(cr, maintext, 0);
cairo_clip(cr);
}
else rectanglelist_clip_containing(cr, page,
textarea, flist);
}
poppler_page_render_for_printing(page, cr);
cairo_restore(cr);
if (draw) {
rectanglelist_draw(cr, flist,
FALSE, FALSE, FALSE, FALSE);
if (usemain)
rectangle_draw(cr, maintext,
FALSE, TRUE, FALSE);
}
cairo_destroy(cr);
cairo_surface_show_page(surface);
g_object_unref(page);
}
cairo_surface_destroy(surface);
return EXIT_SUCCESS;
}