如何使用itextsharp删除pdf文档的默认上边距?

Lil*_*ian 8 margin itextsharp

我试图让我的pdf文档从(0,0)开始,但是看起来文档对象有一个默认的上边距,我不能设置为0.有没有办法做到这一点?

我的代码如下所示

        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;
Run Code Online (Sandbox Code Playgroud)

....

我无法将上边距设置为0.它只是不关心我的设置为(0,0,0,0)并留下上边距(约50f).

Jay*_*ggs 15

您需要在Document构造函数中设置边距,如下所示:

Document pdf = new Document(new Rectangle(288f, 144f), 0, 0, 0, 0); 
Run Code Online (Sandbox Code Playgroud)

您不需要使用该Document.SetMargins()方法.我相信你SetMargins()通过调用创建新页面后会使用Document.NewPage().

  • 感谢Jay所做的工作,但是我必须在所有ceros之后加上“ f”。文件pdf =新文件(新矩形(288f,144f),0f,0f,0f,0f); (2认同)