2010-10-11 142 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尺寸的紙張。