【问题标题】:Print image to pdf without margin using Matlab使用 Matlab 将图像打印为无边距的 pdf
【发布时间】:2013-11-23 19:01:55
【问题描述】:

我正在尝试使用我在这些问题中找到的答案:

将 matlab 绘图打印到 pdf 而不包含白边。

但是使用此代码:

function saveTightFigure( h, outfilename, orientation )

% SAVETIGHTFIGURE(H,OUTFILENAME) Saves figure H in file OUTFILENAME without
%   the white space around it. 
%
% by ``a grad student"
% http://tipstrickshowtos.blogspot.com/2010/08/how-to-get-rid-of-white-margin-in.html

% get the current axes
ax = get(h, 'CurrentAxes');

% make it tight
ti = get(ax,'TightInset');
set(ax,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);

% adjust the papersize
set(ax,'units','centimeters');
pos = get(ax,'Position');
ti = get(ax,'TightInset');
set(h, 'PaperUnits','centimeters');
set(h, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
set(h, 'PaperPositionMode', 'manual');
set(h, 'PaperPosition',[0 0  pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);

% save it
%saveas(h,outfilename);
if( orientation == 1)
    orient portrait
else
    orient landscape
end
print( '-dpdf', outfilename );

end

这个输出的结果:

如您所见,“PaperSize”似乎设置不正确。对可能的修复有任何想法吗?

注意

如果我在landscapeportrait 之间改变方向,结果是一样的,只是图像以不同的方式被切碎。

但是,如果我使用 saveas(h,outfilename); 指令保存图像,则会产生正确的输出。

这是为什么?两个保存指令有什么区别?

【问题讨论】:

  • 我终于放弃了直接打印成pdf的尝试,开始打印成eps,需要的时候可以很方便的转换成pdf。

标签: image matlab pdf matlab-figure


【解决方案1】:

您提到的所有答案都提供了很多方法,但其中大多数也对我不起作用。当您想要获得紧密的插图时,他们中的大多数都会搞砸您的纸张尺寸,唯一对我有用的是:

set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));

我终于写了一个函数,我在其中指定了输出图形在纸上的确切高度和宽度,以及我想要的边距(或者只是将其设置为零)。请注意,您还需要传递轴句柄。也许这个功能也适合你。

function saveFigure( fig_handle, axes_handle, name , height , width , margin)

set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
set(fig_handle, 'Units','centimeters','PaperUnits','centimeters')

% the last two parameters of 'Position' define the figure size
set(fig_handle,'Position',[-margin -margin width height],...
       'PaperPosition',[0 0 width+margin height+margin],...
       'PaperSize',[width+margin height+margin],...
       'PaperPositionMode','auto',...
       'InvertHardcopy', 'on',...
       'Renderer','painters'...     %recommended if there are no alphamaps
   );
saveas(fig_handle,name,'pdf')

end

编辑:如果您使用painters 作为渲染器saveasprint 应该会产生类似的结果。对于 jpeg,print 更可取,因为您可以指定分辨率。

【讨论】:

  • 对于axes_handle,我使用例如h=gca;,但我如何指定名称?
  • @AboAmmar 我不明白你的问题。
  • ,@thewaywewalk 是的,我将其命名为 file.pdf 并运行良好,但对于旧版本的 matlab(我测试了 2009b),它会从左下角开始将图形裁剪为给定的 height , width丢失信息。然而,在 Matlab 2015a 中,它运行良好,但右边距太紧,边缘上的标记被切割。
  • 也可以提高输出pdf图形的分辨率吗?我放大一点,发现圆形标记是一个不完美的多边形。
  • @AboAmmar 与 R2014b 一切都改变了。任何也许这个答案不再适用。
猜你喜欢
  • 2014-07-10
  • 1970-01-01
  • 2012-03-29
  • 2014-04-10
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
  • 2011-07-12
相关资源
最近更新 更多