2014-09-20 101 views
1

即時通訊使用itextsharp導出圖像爲pdf。 ----我想使圖像的邊緣平滑(曲線邊緣), ----以及通過itextsharp圖像屬性獲取圖像的寬度和高度(同時從磁盤獲取圖像) ----和還如何設置PDF頁面如何平滑圖像的邊緣從磁盤/導出到pdf(c#)

的背景顏色以下是獲取圖像,並添加到PDF:

pdfDoc.Open(); 
     //pdfDoc.Add(new iTextSharp.text.Paragraph("Welcome to dotnetfox")); 
     iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(@"C:\Users\Admin\Desktop\logoall.bmp"); 
     // gif.ScaleToFit(500, 100); 
     pdfDoc.Add(gif); 

以下是使網格圖像並保存到磁盤:

Grid companysnapshot = values[0] as Grid; //companysnap shot 

     companysnapshot.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity)); 
     int companywidth = (int)Math.Round(companysnapshot.ActualWidth); 
     int companyheight = (int)Math.Round(companysnapshot.ActualHeight); 
     companywidth = companywidth == 0 ? 1 : companywidth; 
     companyheight = companyheight == 0 ? 1 : companyheight; 

     RenderTargetBitmap rtbmp = new RenderTargetBitmap(companywidth, companyheight, 96d, 96d, PixelFormats.Default); 
     rtbmp.Render(companysnapshot); 
     BmpBitmapEncoder encoder = new BmpBitmapEncoder(); 
     encoder.Frames.Add(BitmapFrame.Create(rtbmp)); 
     FileStream fs1 = File.Create(@"C:\Users\Admin\Desktop\companyss.bmp"); 
     encoder.Save(fs1); 
     fs1.Close(); 

請鱈魚我出來這個!

+0

位圖圖像總是一個矩形。在這種情況下,「平滑邊緣」是什麼意思? – usr2564301 2014-09-20 09:23:15

回答

1

您已將多個問題合併爲一個帖子。這不是你應該提問的方式。

總之:

問題1:什麼是圖像的大小?您有Image實例gif。這個形象的智慧是gif.ScaledWidthjpg.ScaledHeight。還有其他方法可以獲得寬度和高度,但這種方式總是會以用戶單位爲您提供將在PDF中使用的大小。

如果您不縮放圖像,ScaledWidthScaledHeight會以像素爲單位給出圖像的原始大小。像素將被iText視爲用戶單位。在PDF中,用戶單位默認對應一個點(72點對應1英寸)。

問題2:如何顯示帶圓角的圖像?

某些圖像格式(如PNG)允許透明。您可以創建一個圖像,以便通過使角部透明來模仿圓角的效果。

如果這不是一個選項,你應該應用剪切路徑。這在我書中第10章的ClippingPath示例中得到了證明。

移植到C#,示例將是這樣的:

Image img = Image.GetInstance(some_path_to_an_image); 
float w = img.ScaledWidth; 
float h = img.ScaledHeight; 
PdfTemplate t = writer.DirectContent.CreateTemplate(w, h); 
t.Ellipse(0, 0, w, h); 
t.Clip(); 
t.NewPath(); 
t.AddImage(img, w, 0, 0, h, 0, -600); 
Image clipped = Image.GetInstance(t); 

當然:這個剪輯如resulting PDF所示的圖像爲橢圓形。您需要用RoundRectangle()方法替換示例中的Ellipse()方法。

問題3:如何爲每個頁面分配背景顏色?

這是一個重複的問題。請閱讀回答以下問題:

添加一個背景顏色使用頁面事件完成後,你會發現怎麼辦代碼這在上面提到的問題中。

+0

進行更改後,pdf正在生成,但剪切後的圖像沒有顯示在pdf中。 – 2014-09-20 12:29:18

+0

被剪輯的圖像在其屬性中獲取gif和模板...仍然沒有在pdf中顯示剪切圖像 – 2014-09-20 12:39:59

+1

I希望你不要從字面上複製這個例子。如果你這樣做了,你在一個'y = -600'的位置添加圖像。這是在頁面的可見區域之外的方式。如果您在應用代碼之前*確實想過,並且如果您確實調整了x和y座標,請告訴我們您做了什麼。 – 2014-09-20 16:05:49