Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove the default top margin on a pdf document with itextsharp?

I´m trying to get my pdf document to start at (0,0) however it seems that the document object has a default top margin which I cannot set to 0. Is there a way to do this?

My code looks like the following

        using (MemoryStream memoria = new MemoryStream())
        {
            Document pdf = new Document(new Rectangle(288, 144));

            try
            {
                PdfWriter writer = PdfWriter.GetInstance(pdf, memoria);

                pdf.Open();
                pdf.SetMargins(0, 0, 0, 0);

                PdfPTable tPrincipal = new PdfPTable(2);            
                tPrincipal .WidthPercentage = 100;           
                tPrincipal .DefaultCell.Border = 0;
                tPrincipal .TotalWidth = 288f;
                tPrincipal .LockedWidth = true;

....

I just can´t get to set the top margin to 0. It just doesnt care about my setting to (0,0,0,0) and leaves a top margin (around 50f).

like image 291
Lilian Avatar asked Oct 11 '10 05:10

Lilian


2 Answers

You'll need to set your margins in your Document constructor, like this:

Document pdf = new Document(new Rectangle(288f, 144f), 0, 0, 0, 0); 

You won't need to use the Document.SetMargins() method. I believe you'd use SetMargins()after you create a new page by calling Document.NewPage().

like image 200
Jay Riggs Avatar answered Oct 29 '22 21:10

Jay Riggs


Option 1:

Document doc = new Document();
doc.setMargins(0 , 0 , 0 , 0);

Option 2:

Document pdf = new Document(new Rectangle(595 , 842 ), 0, 0, 0, 0); 

Where, 595x842 is A4 size paper.

like image 1
jazzbpn Avatar answered Oct 29 '22 19:10

jazzbpn