Tyler Schroeder
Tyler Schroeder

Reputation: 730

Set page margins with iTextSharp

I have a template PDF file that has a a PDF form field embedded in. I am using PdfStamper to fill out these fields. In addition, I would like to be able to change the margins for generated PDF. is there any way I can modify the page margins on the stamped PDF?

Upvotes: 21

Views: 54051

Answers (3)

Lukas
Lukas

Reputation: 2923

Only way I know of is like this.

iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(pageWidth, pageHeight); 
Document doc = new Document(rec); 
doc.SetMargins(0f, 0f, 0f, 0f);

However, this will limit margins too

Upvotes: 16

user934335
user934335

Reputation: 47

setMaring Impelemented as




    public override bool SetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
            {
                if ((this.writer != null) && this.writer.IsPaused())
                {
                    return false;
                }
                this.nextMarginLeft = marginLeft;
                this.nextMarginRight = marginRight;
                this.nextMarginTop = marginTop;
                this.nextMarginBottom = marginBottom;
                return true;
            }

therefor margin applied for next page. for solve this problem after open pdfDocument call newPage() this solution work for empty pdfDocument .



    using (FileStream msReport = new FileStream(pdfPath, FileMode.Create))
            {
                using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f))
                {
                    try
                    {
                        //open the stream 
                        pdfDoc.Open();
                        pdfDoc.setMargin(20f, 20f, 20f, 20f);
                        pdfDoc.NewPage();

                        pdfDoc.Close();

                    }
                    catch (Exception ex)
                    {
                        //handle exception
                    }

                    finally
                    {


                    }

                }

            }

Upvotes: 0

Baxter
Baxter

Reputation: 5835

You can do it all in one line.

Document doc = new Document(PageSize.LETTER, 0f, 0f, 0f, 0f );

Upvotes: 24

Related Questions