- Ensure all margins are zero in points.
- Explicitly set CenterHorizontally and CenterVertically to false.
- Ensure Zoom is off and FitToPages is enabled.
MATLAB prints Excel to pdf in wrong size
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello. I print an excel sheet to pdf (PDFcreator as default printer), but the size is not in A4 (~33 x 23 in)! Plus, although the left margin is set to 0, it considers large margin. Thanks for any help.
clc;clear;
XLS= 'MyFile.xlsx';
PDF= 'myPDF.pdf';
excel= actxserver('Excel.Application');
workbook= excel.Workbooks.Open(XLS) ;% pick the correct xlsx file
worksheet= workbook.Worksheets.Item('SHEET'); % Choose desired sheet
worksheet.PageSetup.PrintArea= 'F7:AI37';
worksheet.PageSetup.LeftMargin = 0 ;%customize the margin
worksheet.PageSetup.RightMargin= 0 ;
worksheet.PageSetup.TopMargin= 100 ;
worksheet.PageSetup.BottomMargin= 0 ;
worksheet.PageSetup.PaperSize= 'xlPaperA4small' ;% xlPaperA4(9) should be A4, tried but useless
worksheet.PageSetup.Orientation= 2 ;%set the page to landscape(2)
worksheet.PageSetup.Zoom= false ;
worksheet.PageSetup.FitToPagesWide= 1;
worksheet.ExportAsFixedFormat('xlTypePDF',PDF,[],[],'false'); % with this it came out not in A4 but bigger
system('taskkill /F /IM EXCEL.EXE');
winopen(PDF)
0 comentarios
Respuestas (1)
Ayush
el 26 de Jun. de 2025
Hi Ali,
I understand that you are facing issues with the margin and size of the document in the generated pdf file. You can try the following suggestions to resolve the issue:
1. For the size of the document you can use the the following instead of xlPaperA4small enumeration.
worksheet.PageSetup.PaperSize = 9; % xlPaperA4
2. For the issues with the margin: Basically, you can setting excel margins to 0 but excel often adds system printer default margins, especially if the default printer is misconfigured or the excel is not in the "normal" page layout. You can do the following things to ensure margin remains 0 in the generated pdf file:
Here is a pseudo code with all the above changes and suggestions which generates an A4 pdf file with zero margin:
clc; clear;
XLS = 'MyFile.xlsx';
PDF = 'myPDF.pdf';
excel = actxserver('Excel.Application');
excel.Visible = false;
workbook = excel.Workbooks.Open(XLS);
worksheet = workbook.Worksheets.Item('SHEET');
% setting up the page
worksheet.PageSetup.PrintArea = 'F7:AI37';
worksheet.PageSetup.Orientation = 2; % Landscape
worksheet.PageSetup.PaperSize = 9; % xlPaperA4
% setting margin to 0
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
worksheet.PageSetup.HeaderMargin = 0;
worksheet.PageSetup.FooterMargin = 0;
% Fit to page
worksheet.PageSetup.Zoom = false;
worksheet.PageSetup.FitToPagesWide = 1;
worksheet.PageSetup.FitToPagesTall = 1;
% setting up the alignment
worksheet.PageSetup.CenterHorizontally = false;
worksheet.PageSetup.CenterVertically = false;
worksheet.ExportAsFixedFormat('xlTypePDF', PDF);
workbook.Close(false);
excel.Quit;
delete(excel);
winopen(PDF);
You can read more about working with excel in MATLAB here in the official documentation: https://www.mathworks.com/discovery/matlab-excel.html
Hope it helps!
Ver también
Categorías
Más información sobre Spreadsheets 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!