Print a table image in png to pdf A4 portrait but the table is to small and I want to zoom and reduce the margins
1 次查看(过去 30 天)
显示 更早的评论
I have generated a table using uitable and saved as png. Now I am trying to open the png and save as pdf A4 portrait. It works but the picture is too small and I want to zoom/reduce margins, see figures attached.
I have already the following code. Coud you please provide some guidance to with this please? Thanks in advance
% List of files in output
filesPNG = dir(fullfile("Best_Estimate/Report_Plots_BE/", '*.png'));
% Loop through each png file
for i = 1:numel(filesPNG);
% Get the current file name
pngFileName = fullfile(directory, filesPNG(i).name);
% Create a PDF file name based on the PNG file name
[~, baseName, ~] = fileparts(filesPNG(i).name);
pdfFileName = fullfile(outputDir, [baseName, '.pdf']);
% Open a new figure
figure('Visible', 'off');
% Read the PNG image
img = imread(pngFileName);
% Display the image in the figure
imshow(img);
% Set figure size to A4 dimensions (approximately)
fig.Position = [100, 100, 595, 842];
% Set figure to vertical orientation
fig.PaperOrientation = 'portrait';
% Set paper size to A4
fig.PaperSize = [595, 842];
% Set figure units to points
fig.PaperUnits = 'points';
% Set figure margins to zero
fig.PaperPositionMode = 'auto';
% Set additional margin at the bottom
fig.PaperPosition = [0, 0, 595, 842];
% Print the figure to a PDF file
print(pdfFileName, '-dpdf', '-r300');
fprintf('Converted: %s\n', pdfFileName);
end
0 个评论
回答(1 个)
Divyanshu
2024-1-11
Hi Paulino,
I have gone through the code you provided and I understand that you are trying to convert an image into a pdf file with the image being zoomed in the final pdf. So here is the sample code, you can refer it and modify it according to your use-case:
filesPNG = "myfig.png";
pngFileName = "myfig.png";
pdfFileName = "mypdf.pdf"
figure('Visible', 'off');
img = imread(pngFileName);
% imresize is used to zoom the image.
zoomed_image = imresize(img, 1.5);
imshow(zoomed_image);
fig.Position = [100, 100, 595, 842];
fig.PaperOrientation = 'portrait';
fig.PaperSize = [595, 842];
fig.PaperUnits = 'points';
fig.PaperPositionMode = 'auto';
fig.PaperPosition = [0, 0, 595, 842];
print(pdfFileName, '-dpdf', '-r300');
Please refer the following documentation for further details about 'imresize()' function:
Hope it helps!
2 个评论
Divyanshu
2024-1-12
编辑:Divyanshu
2024-1-12
Hi Paulino,
It is possible that above code may not give desired results for figures or images containing 'uitable'.
Please refer the following code snippet which convert a figure with 'uitable' into a pdf file:
h = figure('Name','Numbers','Visible','off');
u = uitable(h,'Data',[2,4,6,8;2,4,6,8;1,3,5,7;2,4,6,8;2,4,6,8]);
% Below code is a workaround to remove extra spaces from the figure and
% make the size of figure equal to uitable.
table_extent = get(u,'Extent');
set(u,'Position',[1 1 table_extent(3) table_extent(4)])
figure_size = get(h,'outerposition');
desired_fig_size = [figure_size(1) figure_size(2) table_extent(3)+15 table_extent(4)+65];
set(h,'outerposition', desired_fig_size);
set(h,'Units','Inches');
pos = get(h,'Position');
set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
savefig(h,'NewFig.fig')
fig1 = openfig("NewFig.fig");
print(fig1,'-dpdf',"MyFig.pdf");
close(fig1);
Above code is a sample code with dummy data, you can modify the data of uitable and other sections of code according to the use-case.
Hope it helps!
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!