Aroueterra
Aroueterra

Reputation: 377

Using Gembox.Spreadsheets to convert .Xlsx to PDF

I was looking for free alternatives to Spire.Xls that allow me to convert from .Xlsx format to .PDF, so far, Gembox is doing great work. However, I cant seem to get the scaling right, and as far as I've looked noone has had the same problem. I'm trying to set the scaling to 93% of the original size with 0 margins all around. However, I cant seem to find the documentation of code that mentions this. It isn't in their sample files either.

Does anyone with experience with this DLL know where I should be looking?

Upvotes: 0

Views: 3351

Answers (1)

Mario Z
Mario Z

Reputation: 4381

Use the following:

var workbook = ExcelFile.Load("Sample.xlsx");

foreach (var worksheet in workbook.Worksheets)
{
    var printOptions = worksheet.PrintOptions;
    printOptions.LeftMargin =
    printOptions.RightMargin =
    printOptions.TopMargin =
    printOptions.BottomMargin = 0;

    printOptions.AutomaticPageBreakScalingFactor = 93;
}

var saveOptions = new PdfSaveOptions();
saveOptions.SelectionType = SelectionType.EntireFile;

workbook.Save("Sample Output.pdf", saveOptions);

Also, I'm not sure why exactly you want to use 93% scaling, but in case you want to achieve fitting of all worksheet's columns on a single page's width then you should use the following instead:

//printOptions.AutomaticPageBreakScalingFactor = 93;
printOptions.FitWorksheetWidthToPages = 1;

Upvotes: 4

Related Questions