Dynamic Print Area: PRINT_AREA named range equivalent

In my EXCEL document, in order to set a dynamic print area, I would define a name called “Print_Area” with the following reference:
<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

<![endif]–><span style=“font-size: 11pt; font-family: “Calibri”,“sans-serif”;”>=OFFSET(Sheet1!$B$3,0,0,<span style=“font-size:11.0pt;font-family:“Calibri”,“sans-serif”;
mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:
EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>Sheet1<span style=“font-size:11.0pt;font-family:“Calibri”,“sans-serif”;
mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:
EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>!$F$2,Pvt!$E$3)

Where
F2: =SUM(IF(F3:AP3<>"",1,0))+4
E3: =COUNTA(F3:F548)+1

How would I do the equivalent programitically with ASPOSE?

Hi,


If you are looking for Dynamically setting the Print Area in Page Setup options of an Excel file then below is an article for you.
Setting print options

Please feel free to write back again. Thanks

Hi,

Please see the code below to calculate the Print Area for any of your worksheet.

Please see the bold line where final Print Area has been calculated.

C#


string filePath = @“F:\Downloads\yourSource.xlsx”;


Workbook wb = new Workbook(filePath);

Worksheet ws = wb.Worksheets[“Sheet1”];


//Get the last cell

Cell lastCell = ws.Cells.LastCell;


//If last cell is merged then calculate last cell again

if (lastCell.IsMerged)

{

Range rng = lastCell.GetMergedRange();


int idxCol = lastCell.Column + rng.ColumnCount;

int idxRow = lastCell.Row + rng.RowCount;


string mycell = CellsHelper.CellIndexToName(idxRow, idxCol);


lastCell = ws.Cells[mycell];

}


//Set the width and height of the last row and column


//Set its width 0.5 before taking a print

double oldWidth = ws.Cells.Columns[lastCell.Column].Width;

ws.Cells.Columns[lastCell.Column].Width = 0.5;


//Set its width 0.5 before taking a print

double oldHeight = ws.Cells.Rows[lastCell.Row].Height;

ws.Cells.Rows[lastCell.Row].Height = 0.5;


//Assign the print area

PageSetup pgSetup = ws.PageSetup;

pgSetup.PrintArea = “A1:” + lastCell.Name; //Final Print Area


//Set the margins to remove white spacing

pgSetup.LeftMargin = 0;

pgSetup.RightMargin = 0;

pgSetup.TopMargin = 0;

pgSetup.BottomMargin = 0;


//Now you can take the print image of your worksheet

ImageOrPrintOptions options = new ImageOrPrintOptions() { PrintingPage = PrintingPageType.Default, IsImageFitToPage = true, OnePagePerSheet = true };

SheetRender sheetRender = new SheetRender(ws, options);


//Save the image in jpeg format to reduce size
Bitmap sheetImage = sheetRender.ToImage(0);

sheetImage.Save(filePath + “SheetImage.jpg”, ImageFormat.Jpeg);

Thank you for the code sample.
Attached is the spreadsheet for which I am attempting set the dynamic print area.
As you can see, when you select any new grouping (paging filter) that causes the cells from the pivot table to change the borders of the print area, those cells will not be printed.

Hi,

Please download Aspose.Cells for .NET (Latest Version) and share your feedback.

Hello.
I already have 5.3.3.2
What feature of this release will allow me to implement a dynamic print area such that when I change the input of the pivot table the print area will also change?

Hi,

We have forwarded your inquiry to development team. We will help you as soon as possible. Thanks for your patience.

This issue has been logged as CELLSNET-27955.

Hi,

We have fixed this issue. Please download Aspose.Cells for .NET (Latest Version) .

Please try the following codes with this fix :

C#


workbook.Worksheets[0].PageSetup.PrintArea = “=OFFSET(MeaningfulDifference!$B$3,0,0,MeaningfulDifference!$F$2,MeaningfulDifference!$E$3)”;


workbook.Save(“CellsNet27955.xlsx”);



But we do not support to dynamic print area in printing or exporting to pdf file now.

The issues you have found earlier (filed as CELLSNET-27955) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
HI

"But we do not support to dynamic print area in printing or exporting to pdf file now."


I am on Aspose.Cells 7.1.2, is the above statement still valid and if not, how could i "simulate" a Dynamic Print Area behaviour with PDF.


Thank you
Matthias

Hi Matthias,

If your requirement is to render only specific part of the worksheet onto the resultant PDF then you should use the PageSetup.PrintArea property to specify the desired cells range at run-time. Please check the following piece of code. In case I have misunderstood your requirement then please share more details, most preferably a sample spreadsheet along with desired range to be printed.

Please note, I have tested the code with latest version Aspose.Cells for .NET (Latest Version) , and I believe the same can be done with version 7.1.2 as well.

C#

var option = new PdfSaveOptions();
option.OnePagePerSheet = true;

var book = new Workbook(inFile);
var sheet = book.Worksheets[0];
var psetup = sheet.PageSetup;
psetup.TopMargin = 0;
psetup.LeftMargin = 0;
psetup.RightMargin = 0;
psetup.BottomMargin = 0;

//Render only one Cell A1 to PDF
psetup.PrintArea = “A1:A1”;
book.Save(“D:/OneCell.pdf”, option);
//Render display area
psetup.PrintArea = sheet.Cells.MaxDisplayRange.RefersTo;
book.Save(“D:/MaxDisplayRange.pdf”, option);
//Render range containing data
psetup.PrintArea = sheet.Cells.FirstCell.Name + “:” + CellsHelper.ColumnIndexToName(sheet.Cells.MaxDataColumn) + sheet.Cells.MaxDataRow + 1;
book.Save(“D:/DataRange.pdf”, option);