2017-05-04 72 views
0

我正在做一個圖像到pdf程序。使用帶間距的itextsharp將圖像轉換爲PDF

我想設置的圖像作爲PDF文件的大小,並與50

在上面多餘的空間我想這碼

   using (var imageStream = new FileStream(imagelocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
       { 
        var image = Image.GetInstance(imageStream); 

        Document document = new Document(new Rectangle(image.Width, image.Height), 0, 0, 0, 0); 
        using (var stream = new FileStream(pdfOutput, FileMode.Create, FileAccess.Write, FileShare.None)) 
        { 
         PdfWriter.GetInstance(document, stream); 
         document.Open(); 
         document.Add(image); 
         document.Close(); 
        } 
       } 

大小,但問題是它不有邊距,

當我嘗試此代碼

Document document = new Document(new Rectangle(image.Width, image.Height), 0, 50, 50, 0); 

它裁剪IM的一部分年齡的空間。我怎麼做這個工作?

回答

0

通過將文檔邊距設置爲「0,50,50,0」,您還設置了一個邊距右邊,這可能不是您想要的。邊緣頂部是在圖像上獲得一些空間的正確方法。 或者,您可以在添加圖像之前在文檔中添加一些空行,例如:document.Add(new Paragraph(Chunk.NewLine))

您可以使用image.ScaleToFit()並傳入類似document.PageSize .Height和document.PageSize.Width使圖像適合您的頁面。

0

這是我的解決方案。我沒有意識到這個簡單的代碼將起作用

Document document = new Document(new Rectangle(image.Width, image.Height + 150), 0, 0, 150, 0); 
相關問題