C# 使用iTextSharp设置页边距

C# 使用iTextSharp设置页边距,c#,.net,itextsharp,C#,.net,Itextsharp,我有一个模板PDF文件,其中嵌入了一个PDF表单字段。我正在使用PdfStamper填写这些字段。此外,我希望能够更改生成的PDF的页边距。有什么方法可以修改盖章的PDF上的页边距吗?我知道的唯一方法是这样的 iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(pageWidth, pageHeight); Document doc = new Document(rec); doc.SetMargins(0f, 0f,

我有一个模板PDF文件,其中嵌入了一个PDF表单字段。我正在使用PdfStamper填写这些字段。此外,我希望能够更改生成的PDF的页边距。有什么方法可以修改盖章的PDF上的页边距吗?

我知道的唯一方法是这样的

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

但是,这也会限制页边距

您可以在一行中完成所有操作

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

设置推进为




    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;
            }

下一页的页边空白。 要在打开pdfDocument后解决此问题,请调用newPage() 此解决方案适用于空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
                    {


                    }

                }

            }


您是否需要保持与PDF模板相同的现有页面大小,还是可以创建页面大小稍大/稍小的新文档?非常感谢!那是一个恼人的问题。还可能需要注意,pageWidth和pageHeight以像素为单位。我使用612x792(用于72dpi)获得常规页面大小。