如何在 iTextSharp 中绘制垂直渐变?
How to draw vertical gradient in iTextSharp?
我正在尝试在 iTextSharp pdf 文档的底部绘制垂直渐变:
PdfShading shading
= PdfShading.SimpleAxial(pdfWriter, 0, document.PageSize.Height,
document.PageSize.Width, 0, BaseColor.WHITE, BaseColor.GREEN);
PdfShadingPattern pattern = new PdfShadingPattern(shading);
pdfContentByte.SetShadingFill(pattern);
pdfContentByte.Rectangle(0, 0, document.PageSize.Width, 70);
pdfContentByte.Fill();
这会在我想要创建它的确切位置创建一个渐变,但渐变是从左(白色)到右(绿色)的水平方向。
我希望渐变从顶部(白色)到底部(绿色)垂直。
像这里的人那样修改坐标()并没有解决问题。我也尝试过旋转文档,但效果不佳。
您使用的坐标有误。在 Java 中,您需要这样的东西:
public void createPdf(String dest) throws IOException, DocumentException {
Rectangle pageSize = new Rectangle(150, 300);
Document document = new Document(pageSize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfShading shading = PdfShading.simpleAxial(writer,
0, pageSize.getHeight(),
0, 0,
BaseColor.WHITE, BaseColor.GREEN);
PdfShadingPattern pattern = new PdfShadingPattern(shading);
PdfContentByte canvas = writer.getDirectContent();
canvas.setShadingFill(pattern);
canvas.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
canvas.fill();
document.close();
}
有关完整示例代码,请参阅 GradientTopToBottom。
你看出区别了吗?
- 你从左上角(
0, document.PageSize.Height
)走到右下角(document.PageSize.Width, 0
)。那是对角线。
- 您想从顶部 (
0, document.PageSize.Height
) 转到底部 (0, 0
),这会导致以下结果:gradient_top_to_bottom.pdf
我正在尝试在 iTextSharp pdf 文档的底部绘制垂直渐变:
PdfShading shading
= PdfShading.SimpleAxial(pdfWriter, 0, document.PageSize.Height,
document.PageSize.Width, 0, BaseColor.WHITE, BaseColor.GREEN);
PdfShadingPattern pattern = new PdfShadingPattern(shading);
pdfContentByte.SetShadingFill(pattern);
pdfContentByte.Rectangle(0, 0, document.PageSize.Width, 70);
pdfContentByte.Fill();
这会在我想要创建它的确切位置创建一个渐变,但渐变是从左(白色)到右(绿色)的水平方向。
我希望渐变从顶部(白色)到底部(绿色)垂直。
像这里的人那样修改坐标(
您使用的坐标有误。在 Java 中,您需要这样的东西:
public void createPdf(String dest) throws IOException, DocumentException {
Rectangle pageSize = new Rectangle(150, 300);
Document document = new Document(pageSize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfShading shading = PdfShading.simpleAxial(writer,
0, pageSize.getHeight(),
0, 0,
BaseColor.WHITE, BaseColor.GREEN);
PdfShadingPattern pattern = new PdfShadingPattern(shading);
PdfContentByte canvas = writer.getDirectContent();
canvas.setShadingFill(pattern);
canvas.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
canvas.fill();
document.close();
}
有关完整示例代码,请参阅 GradientTopToBottom。
你看出区别了吗?
- 你从左上角(
0, document.PageSize.Height
)走到右下角(document.PageSize.Width, 0
)。那是对角线。 - 您想从顶部 (
0, document.PageSize.Height
) 转到底部 (0, 0
),这会导致以下结果:gradient_top_to_bottom.pdf