Set page margins with iTextSharp

本秂侑毒 提交于 2019-12-04 22:47:11

You can do it all in one line.

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
                    {


                    }

                }

            }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!