We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Right margin is too wide

I am generating PDF file using Aspose.cells. And I have noticed that right margin of the page is wider then left margin even if sheet.getPageSetup().getLeftMargin(); and sheet1.getPageSetup().getRightMargin(); return same values.

This is full java code which will recreate issue. I also attached java code in zip file

import com.aspose.cells.*;

public class Test {
private static boolean doneLicense;

public static void main(String[] args) throws Exception {
setLicense();
Workbook wb = new Workbook();

String[][] data = { { “88”, “44”, “12” }, { “23”, “77”, “77” }, { “55”, “78”, “80” }, };
String[] categoryData = { “one”, “two”, “three” };
String[] series = { “Some text here”, “Somer long text here 38838” , “Some another very long name 0032841399”};

Worksheet sheet2 = wb.getWorksheets().add(“sheet2”);
int index2 = sheet2.getCharts().add(ChartType.COLUMN, 0, 0, 20, 20);
Chart chart2 = sheet2.getCharts().get(index2);
buildGraph(chart2, data, categoryData, series, “Some Title 2”);
setChartWidth(chart2);

wb.save(“chart.pdf”, FileFormatType.PDF);
System.out.println(“Done!”);
}

public static void buildGraph(Chart chart, String[][] data, String[] categoryData, String[] series, String title) throws Exception {

// format graph
SeriesCollection nSeries = chart.getNSeries();
Legend legend = chart.getLegend();
legend.setPosition(LegendPositionType.BOTTOM);
legend.setAutomaticSize(true);
legend.setAutoScaleFont(true);
chart.getTitle().setText(title);
chart.getTitle().setShadow(true);
chart.getCategoryAxis().setTickLabelPosition(TickLabelPositionType.LOW);
chart.getCategoryAxis().getTickLabels().setRotationAngle(90);

// add data and collect categories information
for (int r = 0; r < data.length; r++) {
String[] rowData = data[r];
int index = nSeries.add(getSeriesValuesStringVersion(rowData), true);
nSeries.get(index).setName(series[r]);
}
nSeries.setCategoryData(getSeriesValuesStringVersion(categoryData));
chart.calculate();
}

public static String getSeriesValuesStringVersion(String[] data) {
StringBuilder builder = new StringBuilder("{");
boolean firstElement = true;
for (String d : data) {
if (firstElement) {
builder.append(d);
firstElement = false;
} else {
builder.append(", “);
builder.append(d);
}
}
builder.append(”}");

return builder.toString();
}

private static void setChartWidth(Chart chart) {
ChartShape shape = chart.getChartObject();

shape.setWidth(3000);
shape.setHeight(951);
}

/
* Loads the Aspose license.
*
* @throws Exception If we can’t load the license.
/
private static void setLicense() throws Exception {

if (doneLicense) {
return; // Only need to do it once.
}
// Get the Aspose license file, and load it into Aspose.
License license = new License();
license.setLicense(“Aspose.Java.lic”);

doneLicense = true;

}
}import com.aspose.cells.;

public class Test {
private static boolean doneLicense;

public static void main(String[] args) throws Exception {
setLicense();
Workbook wb = new Workbook();

String[][] data = { { “88”, “44”, “12” }, { “23”, “77”, “77” }, { “55”, “78”, “80” }, };
String[] categoryData = { “one”, “two”, “three” };
String[] series = { “Some text here”, “Somer long text here 38838” , “Some another very long name 0032841399”};

Worksheet sheet2 = wb.getWorksheets().add(“sheet2”);
int index2 = sheet2.getCharts().add(ChartType.COLUMN, 0, 0, 20, 20);
Chart chart2 = sheet2.getCharts().get(index2);
buildGraph(chart2, data, categoryData, series, “Some Title 2”);
setChartWidth(chart2);

wb.save(“chart.pdf”, FileFormatType.PDF);
System.out.println(“Done!”);
}

public static void buildGraph(Chart chart, String[][] data, String[] categoryData, String[] series, String title) throws Exception {

// format graph
SeriesCollection nSeries = chart.getNSeries();
Legend legend = chart.getLegend();
legend.setPosition(LegendPositionType.BOTTOM);
legend.setAutomaticSize(true);
legend.setAutoScaleFont(true);
chart.getTitle().setText(title);
chart.getTitle().setShadow(true);
chart.getCategoryAxis().setTickLabelPosition(TickLabelPositionType.LOW);
chart.getCategoryAxis().getTickLabels().setRotationAngle(90);

// add data and collect categories information
for (int r = 0; r < data.length; r++) {
String[] rowData = data[r];
int index = nSeries.add(getSeriesValuesStringVersion(rowData), true);
nSeries.get(index).setName(series[r]);
}
nSeries.setCategoryData(getSeriesValuesStringVersion(categoryData));
chart.calculate();
}

public static String getSeriesValuesStringVersion(String[] data) {
StringBuilder builder = new StringBuilder("{");
boolean firstElement = true;
for (String d : data) {
if (firstElement) {
builder.append(d);
firstElement = false;
} else {
builder.append(", “);
builder.append(d);
}
}
builder.append(”}");

return builder.toString();
}

private static void setChartWidth(Chart chart) {
ChartShape shape = chart.getChartObject();

shape.setWidth(3000);
shape.setHeight(951);
}

/
* Loads the Aspose license.
*
* @throws Exception If we can’t load the license.
*/
private static void setLicense() throws Exception {

if (doneLicense) {
return; // Only need to do it once.
}
// Get the Aspose license file, and load it into Aspose.
License license = new License();
license.setLicense(“Aspose.Java.lic”);

doneLicense = true;

}
}<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /><o:p></o:p>

Hi,


Thanks for the sample code.

I have evaluated your code by adding a line of code to your code to save the XLSX file as well. When I compare both XLSX (open the print preview of the sheet pages) file and PDF, both are ok. I could not spot the issue except the legend entries are overlapped which is already logged as “CELLSJAVA-40514” into our database for your other thread (<a href="https://forum.aspose.com/t/93534).

If you still have any difference (other than legend items), kindly elaborate it and provide some more screen shots to highlight the problem areas, we will check it soon.

Thank you.

Hi,

I currently use Aspose.Cells.7.4.3 version and when I generate pdf file I always get right margin around 50 % wider then a left margin. I attached PDF file which is generated by code I provided in my previous post. Thank you

Hi,


I simply added a line (save to XLSX file format) to your code and created the Excel file too. When I compare both XLSX (open the print preview of the sheet pages) file and PDF, both are identical regarding right margins, see the screen shot attached here for your reference.

For your information, Aspose.Cells renders PDF file based on the what is shown in the print preview of the sheets(in MS Excel).

Thank you.

Hi,

I do understand that print preview of XLSX and generated PDF is
identical. Problem is that I want that right and left margins to be the same
width. But I can’t accomplish that. Every time I generate PDF file regardless
what it contains, tables or graphs anyway I can’t get right and left margin
with identical width. Every time right margin is around 50% wider even if those
margins must be the same. Bellow I provided slightly modified code so you will
be able to see issue better. I also attached screenshot of PDF generated by
code with some explanations painted on top. Please let me know if you need any
more information.

Thank you.

Alikas Jesatovas

import com.aspose.cells.*;

public class Test {
private static boolean doneLicense;

public static void main(String[] args) throws Exception {
setLicense();
Workbook wb = new Workbook();

String[][] data = { { “88”, “44”, “12” }, { “23”, “77”, “77” }, { “55”, “78”, “80” }, };
String[] categoryData = { “one”, “two”, “three” };
String[] series = { “Some text here”, “Somer long text here 38838” , “Some another very long name 00328413”};

Worksheet sheet = wb.getWorksheets().get(0);
int index = sheet.getCharts().add(ChartType.COLUMN, 0, 0, 20, 20);
Chart chart = sheet.getCharts().get(index);
buildGraph(chart, data, categoryData, series, “Some Title”);
setChartWidth(chart);

wb.save(“chart.pdf”, FileFormatType.PDF);
System.out.println(“Done!”);
}

public static void buildGraph(Chart chart, String[][] data, String[] categoryData, String[] series, String title) throws Exception {

// format graph
SeriesCollection nSeries = chart.getNSeries();
Legend legend = chart.getLegend();
legend.setPosition(LegendPositionType.BOTTOM);
legend.setAutomaticSize(true);
legend.setAutoScaleFont(true);
chart.getTitle().setText(title);
chart.getTitle().setShadow(true);
chart.getCategoryAxis().setTickLabelPosition(TickLabelPositionType.LOW);
chart.getCategoryAxis().getTickLabels().setRotationAngle(90);

// add data and collect categories information
for (int r = 0; r < data.length; r++) {
String[] rowData = data[r];
int index = nSeries.add(getSeriesValuesStringVersion(rowData), true);
nSeries.get(index).setName(series[r]);
}
nSeries.setCategoryData(getSeriesValuesStringVersion(categoryData));
chart.calculate();
}

public static String getSeriesValuesStringVersion(String[] data) {
StringBuilder builder = new StringBuilder("{");
boolean firstElement = true;
for (String d : data) {
if (firstElement) {
builder.append(d);
firstElement = false;
} else {
builder.append(", “);
builder.append(d);
}
}
builder.append(”}");

return builder.toString();
}

private static void setChartWidth(Chart chart) {
ChartShape shape = chart.getChartObject();

shape.setWidth(575 + 10);
shape.setHeight(400);
}

/**
* Loads the Aspose license.
*
* @throws Exception If we can’t load the license.
*/
private static void setLicense() throws Exception {

if (doneLicense) {
return; // Only need to do it once.
}
// Get the Aspose license file, and load it into Aspose.
License license = new License();
license.setLicense(“Aspose.Java.lic”);

doneLicense = true;

}
}

Hi,


As I told you, we follow MS Excel’s standards and we have to follow it. You may confirm this after manually creating the chart in MS Excel based on the what is written in your codes, you will see that you got two pages with some right most borders part/portion renders on the second page. You may also confirm this behavior when using MS Excel’s renderer to export to PDF within MS Excel manually. I think for your requirement, you may try any one of the following options.

1) Set PageSetup worksheet’s margins accordingly, see the lines of code that I added to your code segment, now the PDF rendering would be a lot better and the chart would be rendered in a single page.

Worksheet sheet = wb.getWorksheets().get(0);
int index = sheet.getCharts().add(ChartType.COLUMN, 0, 0, 20, 20);
Chart chart = sheet.getCharts().get(index);
buildGraph(chart, data, categoryData, series, “Some Title”);
setChartWidth(chart);
//set the margins of pagesetup accordingly
PageSetup pageSetup = sheet.getPageSetup();
pageSetup.setLeftMargin(pageSetup.getLeftMargin() + 0.1);
pageSetup.setRightMargin(pageSetup.getLeftMargin()/2);
wb.save(“chart.pdf”, SaveFormat.PDF);
wb.save(“chart.xlsx”, SaveFormat.XLSX);
System.out.println(“Done!”);



2) Use setOnePagePerSheet option so the chart would be rendered in one page automatically, see the topic for your reference:
http://www.aspose.com/docs/display/cellsjava/Render+One+PDF+Page+Per+Excel+Worksheet±+Excel+to+PDF+Conversion

(Note: you got to use the option before rendering to PDF using PdfSaveOption)

Hope, this helps a bit.

Thank you.

Finally I fixed my margins error. Tried various ways but only one worked:

PageSetup pageSetup = sheet.getPageSetup();

pageSetup.setRightMargin(pageSetup.getRightMargin() * 0.75);

pageSetup.setFitToPagesWide(1);

I am not sure why margins wasn't same width in first place.

Thanks

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

We are pleased to know that you were able to sort out your issue. Let us know if you face any other issue, we will be happy to help you.