2010-10-11 34 views
7

我试图让我的pdf文档从(0,0)开始,但是似乎文档对象有一个默认的顶部边距,我不能设置为0. 有没有办法做到这一点?如何使用itextsharp删除PDF文档的默认顶部边距?

我的代码如下所示

 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; 

....

我只是不容到达设置上边距为0。只是不约我的设置(0护理, 0,0,0)并留下顶部边距(大约50f)。

回答

14

你需要设置页边距在文档的构造,就像这样:

Document pdf = new Document(new Rectangle(288f, 144f), 0, 0, 0, 0); 

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

+1

非常感谢杰伊,但是我必须在所有ceros之后加入“f”。文档pdf =新文档(新的矩形(288f,144f),0f,0f,0f,0f); – Lilian 2010-10-12 13:55:13

+0

@Lilian,他们期待着漂浮点。 – 2011-03-23 01:24:32

+1

并且在使用0时没有错误,它只是放入默认边距。 0f一路! – TChadwick 2012-10-18 19:13:58

1

选项1:

Document doc = new Document(); 
doc.setMargins(0 , 0 , 0 , 0); 

选项2:

Document pdf = new Document(new Rectangle(595 , 842), 0, 0, 0, 0); 

在哪里,595x842是A4尺寸的纸张。