Crop white margins in a PDF file
155 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sim
el 5 de En. de 2022
Comentada: Richard Quist
el 2 de Feb. de 2022
Hi, I know this question has been asked already, but - so far - I did not find a suitable and easy solution.
I have a PDF file in a folder, which is actually a graph/figure.
I would like to programmatically read that PDF file and remove the margins/white space around my graph/figure.
Is there any simple but efficient way to do so (without changing figure ratio, size or quality)?
Something like this:
filename = open('mypdffile.pdf')
crop_margins(filename)
2 comentarios
DGM
el 6 de En. de 2022
Editada: DGM
el 6 de En. de 2022
It would help if you provide an example of the file.
Unless you know for certain that it's not, I'd suspect that the padding is actually part of the image. Accumulating extra padding when saving plots is a very common problem.
If the padding is part of the image, then removing it will change the size of the image. Should the cropped image be rescaled to retain the original geometry?
Respuesta aceptada
Richard Quist
el 6 de En. de 2022
One of the following should help.
1. Use external tool to modify previously generated PDF file
MATLAB does not provide a way to read in a PDF file and edit/crop it. You may be able to use an external tool such as ghostscript to do this.
2. Use exportgraphics
In R2020a and above you should be able to use exportgraphics to do this.
exportgraphics(fig, 'output.pdf', 'ContentType', 'vector');
If you are getting errors during installation or when running that command you should contact tech support.
3. Use print with a tiledlayout
If you are using R2020b or later you could place your plot inside a tiledlayout container with tight padding and then use the print command to generate the PDF. Something like the following:
fig = figure;
% set whatever figure properties are needed
tl = tiledlayout(fig, 1, 1, 'padding', 'tight');
ax = nexttile;
% additional code to create your plot inside ax
bar(ax, magic(4));
% make sure PDF paper size is sized properly
fig.PaperSize = fig.PaperPosition(3:4);
% print the figure
print(fig, '-dpdf', 'output.pdf');
3 comentarios
Tim Baur
el 2 de Feb. de 2022
With option 3, the printing process always cuts off the right box line of the axes.
Do you have a solution for that problem, too?
Of course, I could used "compact" instead of "tight" padding but then again, the white space remains.
Richard Quist
el 2 de Feb. de 2022
@Tim Baur: that looks like a clipping issue with the renderer.
You could try adjusting the size of the tiledlayout object, making it a tiny bit smaller to avoid the clipping. For example, set the OuterPosition property when creating it:
tl = tiledlayout(fig, 1, 1, 'padding', 'tight', 'OuterPosition',[0 0 .995 1]);
Or you could do this after the tiledlayout object has been created:
tl.OuterPosition(3) = .995;
You may need to tweak that value to get the results you want.
I hope that helps!
Más respuestas (0)
Ver también
Categorías
Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!