E
E
Emil Revencu2022-01-13 21:02:49
Python
Emil Revencu, 2022-01-13 21:02:49

How to change positions of elements (figure, line, image) in PDF file?

There is a PDF file. It has a bunch of non-text elements. It is necessary under certain conditions to move these elements.
Some change color. If you just work with text then you could use the pypdf2
list element I deduced through

from pdfminer.high_level import extract_pages
from pdfminer.layout import LTTextContainer, LTChar, LTCurve, LTFigure
for page_layout in extract_pages('File-XD.pdf'):
    for element in page_layout:
        print(element, element.bbox)

The resulting list looks like this:
<LTTextBoxHorizontal(34) 78.142,83.061,94.482,92.561 'BMI\n'> (78.14209999999997, 83.0611, 94.48209999999997, 92.5611)
<LTTextBoxHorizontal(35) 78.142,102.061,238.008,111.561 'Effect of Fitness on Blood Pressure\n'> (78.14209999999997, 102.0611, 238.00809999999998, 111.5611)
<LTTextBoxHorizontal(36) 307.140,102.061,382.864,111.561 'Physical Activity\n'> (307.1396, 102.0611, 382.8641, 111.5611)
<LTFigure(Fm0) -19.000,294.000,596.000,1010.000 matrix=[1.00,0.00,0.00,1.00, (0.00,0.00)]> (-19.0, 294.0, 596.0, 1010.0)
<LTCurve 38.965,415.467,188.311,469.171> (38.9648, 415.46729999999997, 188.3108, 469.1713)
<LTCurve 406.965,414.467,556.311,468.171> (406.9648, 414.46729999999997, 556.3108, 468.1713)
<LTFigure(Fm1) 38.000,200.000,595.000,246.000 matrix=[1.00,0.00,0.00,1.00, (0.00,0.00)]> (38.0, 200.0, 595.0, 246.0)
<LTCurve 53.577,61.695,541.699,184.695> (53.57690000000002, 61.6949, 541.6989000000001, 184.6949)
<LTFigure(Fm2) 62.988,174.314,595.275,246.390 matrix=[1.00,0.00,0.00,1.00, (0.00,0.00)]> (62.9881, 174.314, 595.2751000000001, 246.3902)
<LTLine 75.638,100.824,288.638,100.824> (75.6378, 100.8237, 288.63779999999997, 100.8237)
<LTLine 304.638,100.824,517.638,100.824> (304.6378, 100.8237, 517.6378, 100.8237)
<LTCurve 486.638,106.354,505.638,112.854> (486.6379, 106.3542, 505.6379, 112.8542)
<LTCurve 257.638,106.354,276.638,112.854> (257.6379, 106.3542, 276.6379, 112.8542)
<LTCurve 257.638,87.513,276.638,94.013> (257.638, 87.5128, 276.638, 94.0128)
<LTCurve 76.136,131.823,201.136,155.445> (76.1357, 131.8228, 201.13569999999999, 155.4448)
<LTCurve 87.619,139.590,91.320,140.501> (87.6189, 139.5904, 91.31989999999999, 140.5014)
<LTCurve 84.439,140.798,84.885,144.109> (84.43889999999999, 140.7984, 84.88489999999999, 144.1094)
<LTCurve 82.657,137.153,94.378,150.730> (82.6569, 137.15339999999998, 94.3779, 150.73039999999997)
<LTFigure(Fm3) 411.000,162.000,943.000,294.000 matrix=[1.00,0.00,0.00,1.00, (0.00,0.00)]> (411.0, 162.0, 943.0, 294.0)
<LTCurve 417.009,138.775,525.267,155.445> (417.00910000000005, 138.7749, 525.2671, 155.4449)
<LTRect 453.804,134.758,487.530,168.878> (453.804, 134.758, 487.53, 168.87800000000001)
<LTRect 420.204,134.758,453.930,168.878> (420.204, 134.758, 453.93, 168.87800000000001)
<LTRect 487.562,134.758,522.204,168.878> (487.56199999999995, 134.758, 522.204, 168.87800000000001)
<LTFigure(Fm4) 491.000,154.000,1012.000,281.000 matrix=[1.00,0.00,0.00,1.00, (0.00,0.00)]> (491.0, 154.0, 1012.0, 281.0)
<LTCurve 500.197,136.754,511.814,144.350> (500.19669999999996, 136.7542, 511.8137, 144.3502)
<LTFigure(Fm5) 38.000,437.000,595.000,625.000 matrix=[1.00,0.00,0.00,1.00, (0.00,0.00)]> (38.0, 437.0, 595.0, 625.0)
<LTCurve 53.577,203.195,541.699,421.195> (53.57690000000002, 203.1949, 541.6989000000001, 421.19489999999996)

So you need to save it to a new PDF with shifted (right, left) elements like LTRect, LTFigure

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question