2010-10-11 6 views
7

Próbuję dostać mój dokument pdf na początek (0,0) jednak wydaje się, że obiekt dokumentu ma domyślny górny margines, którego nie mogę ustawić na 0. Czy jest jakiś sposób to zrobić?Jak mogę usunąć domyślny górny margines na dokumencie PDF za pomocą itextsharp?

Mój kod wygląda następującym

 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; 

....

właśnie can not uzyskać ustawić górny margines na 0. To po prostu nie robi opieki o moim ustawienie na (0, 0,0,0) i pozostawia górny margines (około 50f).

Odpowiedz

14

Musisz ustawić marginesy w dokumencie konstruktora, tak:

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

Nie trzeba będzie użyć metody Document.SetMargins(). Sądzę, że po utworzeniu nowej strony użyjesz SetMargins(), dzwoniąc pod numer Document.NewPage().

+1

Dzięki Jay, która zadziałała, jednak musiałem dodać "f" po wszystkich ceros. Dokument pdf = nowy Dokument (nowy prostokąt (288f, 144f), 0f, 0f, 0f, 0f); – Lilian

+0

@Lilian, dobry punkt, że spodziewają się pływaków. –

+1

i nie ma błędu, gdy używasz 0, to po prostu wstawia domyślne marginesy. 0f Po drodze! – TChadwick

1

Opcja 1:

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

Opcja 2:

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

przypadku, 595x842 jest papier A4.

Powiązane problemy