2012-02-11 137 views
12

我有一個模板PDF文件,內嵌了一個PDF表單域。我使用PdfStamper填充這些域。另外,我希望能夠更改生成PDF的邊距。有什麼方法可以修改加蓋的PDF頁面頁邊距嗎?使用iTextSharp設置頁邊距

+0

您是否需要保留與您的PDF模板**相同的**現有頁面尺寸,或者是否可以創建頁面尺寸稍大/更小的新文檔? – kuujinbo 2012-02-19 08:49:52

回答

13

我知道的唯一方法就是這樣。

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

然而,這將限制利潤率也

+2

非常感謝!這是一個令人討厭的問題。也可能要注意pageWidth和pageHeight是以像素爲單位的。我使用了612 x 792(72dpi)來獲得常規頁面大小。 – James 2013-01-07 22:57:06

16

你可以做到這一切在同一行。

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

setMaring Impelemented作爲




    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() 這個解決方案爲空pdf文檔工作。



    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 
        { 


        } 

       } 

      }