简体   繁体   中英

Set page margins with iTextSharp

I have a template PDF file that has aa 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?

你可以在一行中完成所有这些工作。

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

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

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
                    {


                    }

                }

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM