Problem with Page Margins

Hi,

I am trying to generate a pdf file using Aspose.pdf. I have a problem with the page margins. Even after setting proper page margins the table that I am displaying hits the right end of the page and leaves a lot of space on the left in the begining of the page. My code setting the page margins is pretty straight forward as shown below:

//The values of all these margins is 50.0

_pdfdocument.PageSetup.Margin.Right = _rightMargin;

_pdfdocument.PageSetup.Margin.Left = _leftMargin;

_pdfdocument.PageSetup.Margin.Top = _topMargin;

_pdfdocument.PageSetup.Margin.Bottom = _bottomMargin;

When I debug the code and look at the PageSetup object I see two MarginInfo objects as part of it. One MarginInfo object has all the margin variables with correct values i.e. 50.0 as set by me in the code whereas the other one shows all the margin values as 0.0. I am not sure why it contains two margin objects instead of just one.

Also, please note that I am generating this pdf in streaming mode i.e. I do pdf.Close() in the end. I am attaching the pdf file with this post.

Any help on this will be highly appreciated.

Regards,
Swaleh

Dear Swaleh,

Thank you for considering Aspose.

When a table is too large, it may displayed to right edge and the right margin is ignored. You need to enlarge the page size or use a small table. You can also try setting table's column width with percentage. If you still can get it work, please provide a runable example to us and let us test it.

Hi,

Thanks for the response.

I have one question though. If I am specifying the size of the table as 512 points and the left margin as 50 points and right margin as 50 points, the total comes out to 612 which is width of a Letter size page. Still it runs to the right end of the page. Does this mean that Aspose internally ignores the Table size specified in the program if the table is bigger then the size specified? Also, please note that apart from Columnwidths I have also specified the the FixedWidth property of the table to 512.

The problem is my test code works fine but when I use it in real production environment I get problems. Hence, its a bit difficult for me to send you wroking sample with this problem in it.

Thanks & Regards,
Swaleh

Dear Swaleh,

The Table.FixedWidth don't work and has been obsolete. Sorry for not making it clear in the document. You should set the column width using Table.ColumnWidths. Here is an example:





test



Hi,

Thanks for the reply. Please note that I am using ColumnWidths to set the widths of the columns. I later on tried using FixedWidth to check whether it works or not but it did not work either. I mean its still hitting the right of the page instead of providing proper margins whether you use Columnwidths or fixedwidth or both.

Thanks,

Swaleh

Hi Swaleh,

Have you tried my example? I have attached the result. If it still won't work, please provide a complete example and let us test it.

Please try the following codes which work as same as the xml above.


Pdf pdf = new Pdf();
Aspose.Pdf.Section sec1 = pdf.Sections.Add();
sec1.PageInfo.PageWidth = 612;
sec1.PageInfo.Margin.Left = 50;
sec1.PageInfo.Margin.Right = 50;

Aspose.Pdf.Table table1 = new Aspose.Pdf.Table();
sec1.Paragraphs.Add(table1);
table1.ColumnWidths = "512";
Aspose.Pdf.Row row1 = table1.Rows.Add();
table1.DefaultCellBorder = new BorderInfo((int)BorderSide.All,0.1F);
Aspose.Pdf.Cell cell = row1.Cells.Add("test");

pdf.Save("PageMargins.pdf");

Hi,

Actually I am doing the same thing in my code. But still the margins are running to the right hand side of the page. Also, I am using streaming mode. So I do Pdf.Close() instead of Pdf.Save(). That's the only difference.

My code looks like this:

//Instantiate a Pdf document instance

_pdfdocument = new Pdf(file);

//Create a section in the Pdf instance

_section = _pdfdocument.Sections.Add();

_pdfdocument.PageSetup.PageHeight = PageSize.LetterHeight;

_pdfdocument.PageSetup.PageWidth = PageSize.LetterWidth;

_section.PageInfo.Margin.Left = 50;
_section.PageInfo.Margin.Right = 50;

// create main body table

_table = new Table();

//Set column widths of the table

_table.ColumnWidths = "100 100 100 100 50 62"; (which is 512)

Row row = new Row(_table);

_table.Rows.Add(row);

_section.AddParagraph(_table);

_pdfdocument.Close()

I don't think the problem is so straight forward.

Regards,
Swaleh

Dear Swaleh,

Please use the following two methods to set the page size:

1) Set the page size for section:
Section _section = _pdfdocument.Sections.Add();

_section.PageInfo.PageHeight = PageSize.LetterHeight;
_section.PageInfo.PageWidth = PageSize.LetterWidth;

2) Set the page size for document. When create new section, inherit page info from document:
_pdfdocument.PageSetup.PageHeight = PageSize.LetterHeight;
_pdfdocument.PageSetup.PageWidth = PageSize.LetterWidth;

Section _section = new Section(_pdfdocument);
_pdfdocument.Sections.Add(_section);