2017-04-20 171 views
1

我正在使用iTextSharp和產品所有者的PDF工作,並決定在背景上爲它貼上一個「貼紙」文字框。我正在搜索互聯網,無法找到如何實現這一效果的提示。爲它提供的設計如下: enter image description hereItextSharp平滑背景陰影

任何人更有經驗的iText或iTextsharp幫助我建議如何實現這種效果嗎? 親切的問候 卡羅爾

+0

我的回答有幫助嗎?或者還有問題嗎? – mkl

回答

2

使用里斯答案提示和鏈接,人們可以很容易地實現一個輔助方法這樣繪製陰影圓角矩形:

void DrawRoundedShadedRectangle(PdfWriter writer, float x, float y, float w, float h, float r, float shade, BaseColor innerColor, BaseColor shadeColor, BaseColor outerColor) 
{ 
    PdfContentByte canvas = writer.DirectContent; 

    canvas.SaveState(); 
    canvas.Rectangle(x - shade, y + r, w + 2 * shade, h - 2*r); 
    canvas.Clip(); 
    canvas.NewPath(); 
    PdfShading shadingRight = PdfShading.SimpleAxial(writer, x + w, y, x + w + shade, y, shadeColor, outerColor, false, false); 
    canvas.PaintShading(shadingRight); 
    PdfShading shadingLeft = PdfShading.SimpleAxial(writer, x, y, x - shade, y, shadeColor, outerColor, false, false); 
    canvas.PaintShading(shadingLeft); 
    canvas.RestoreState(); 

    canvas.SaveState(); 
    canvas.Rectangle(x + r, y - shade, w - 2 * r, h + 2 * shade); 
    canvas.Clip(); 
    canvas.NewPath(); 
    PdfShading shadingTop = PdfShading.SimpleAxial(writer, x, y + h, x, y + h + shade, shadeColor, outerColor, false, false); 
    canvas.PaintShading(shadingTop); 
    PdfShading shadingBottom = PdfShading.SimpleAxial(writer, x, y, x, y - shade, shadeColor, outerColor, false, false); 
    canvas.PaintShading(shadingBottom); 
    canvas.RestoreState(); 

    canvas.SaveState(); 
    canvas.Rectangle(x + w - r, y + h - r, r + shade, r + shade); 
    canvas.Clip(); 
    canvas.NewPath(); 
    PdfShading shadingTopRight = PdfShading.SimpleRadial(writer, x + w - r, y + h - r, r, x + w - r, y + h - r, r + shade, shadeColor, outerColor); 
    canvas.PaintShading(shadingTopRight); 
    canvas.RestoreState(); 

    canvas.SaveState(); 
    canvas.Rectangle(x - shade, y + h - r, r + shade, r + shade); 
    canvas.Clip(); 
    canvas.NewPath(); 
    PdfShading shadingTopLeft = PdfShading.SimpleRadial(writer, x + r, y + h - r, r, x + r, y + h - r, r + shade, shadeColor, outerColor); 
    canvas.PaintShading(shadingTopLeft); 
    canvas.RestoreState(); 

    canvas.SaveState(); 
    canvas.Rectangle(x - shade, y - shade, r + shade, r + shade); 
    canvas.Clip(); 
    canvas.NewPath(); 
    PdfShading shadingBottomLeft = PdfShading.SimpleRadial(writer, x + r, y + r, r, x + r, y + r, r + shade, shadeColor, outerColor); 
    canvas.PaintShading(shadingBottomLeft); 
    canvas.RestoreState(); 

    canvas.SaveState(); 
    canvas.Rectangle(x + w - r, y - shade, r + shade, r + shade); 
    canvas.Clip(); 
    canvas.NewPath(); 
    PdfShading shadingBottomRight = PdfShading.SimpleRadial(writer, x + w - r, y + r, r, x + w - r, y + r, r + shade, shadeColor, outerColor); 
    canvas.PaintShading(shadingBottomRight); 
    canvas.RestoreState(); 

    canvas.SaveState(); 
    canvas.SetColorFill(innerColor); 
    canvas.SetColorStroke(innerColor); 
    canvas.RoundRectangle(x, y, w, h, r); 
    canvas.FillStroke(); 
    canvas.RestoreState(); 
} 

例如,

DrawRoundedShadedRectangle(writer, 100, 500, 200, 100, 10, 20, BaseColor.GREEN, BaseColor.RED, BaseColor.WHITE); 

繪製此:

Green-Red-White

而這種代碼

DrawRoundedShadedRectangle(writer, writer.PageSize.GetLeft(100), 300, writer.PageSize.Width - 200, 100, 10, 10, BaseColor.ORANGE, BaseColor.YELLOW, BaseColor.WHITE); 
PdfContentByte canvas = writer.DirectContent; 
canvas.SaveState(); 
canvas.Rectangle(writer.PageSize.Left, writer.PageSize.Bottom, writer.PageSize.Right, 300); 
canvas.Clip(); 
canvas.NewPath(); 
DrawRoundedShadedRectangle(writer, writer.PageSize.GetLeft(100), 100, writer.PageSize.Width - 200, 200, 10, 10, BaseColor.WHITE, BaseColor.YELLOW, BaseColor.WHITE); 
canvas.RestoreState(); 

繪本

Orange/White - Yellow - White

通過調整[R adius的樹蔭,以及所有顏色參數這應該變得很類似於提供的設計。

+1

謝謝,那正是我所想要的。 – Karol

1

這可以用iText來完成。 這是在畫布上使用繪圖操作來達到效果的問題。 你的情況 - 一個圓角矩形,填充橙色 - 一個圓角矩形,它的下面,填充白色

+0

你會如何實現平滑的陰影? 請注意,這是一種在各個方向uniorm ... – Karol

+1

http://developers.itextpdf.com/content/best-itext-questions-stackoverview/absolute-positioning-lines-and-shapes/itext7-how -add-shading-pattern-custom-shape –