iText 7.1.4 中的透明颜色
Color with transparency in iText 7.1.4
我可以在我的程序中使用透明的颜色吗?我想在我的 PDF 文件中使用这种颜色。我使用 iText 7.1.4 创建我的 pdf,但我不知道如何将 transpanrent 设置为 DeviceRgb
:
类型
public static DeviceRgb ToDeviceRgb(this System.Windows.Media.Color color) =>
new DeviceRgb(color.R, color.G, color.B);
是否可以在 iText7 中使用带透明度的颜色?
更新:
我试着按照 Uladzimir Asipchuk 告诉我的去做,但我没有看到任何结果:
我可以在我的程序中编辑的产品卡片模板(边距、填充、颜色、字体等)。
在设置模板时,某些颜色可能有一个 alpha cannel(透明),我想在我的 PDF 文档中看到这个透明因素。
因此,正如 Uladzimir Asipchuk 建议我的那样,我将不透明度级别传递给 SetBackgroundColor 方法中的第二个参数:
public override Table CreateTemplate(Product product)
{
if(product == null) throw new ArgumentNullException(nameof(product));
// Create a table of the product card
var productTable = new Table(new UnitValue[] { UnitValue.CreatePercentValue(40), UnitValue.CreatePercentValue(60) })
.SetWidth(UnitValue.CreatePercentValue(100))
.SetBackgroundColor(Settings.BackgroundColor.ToDeviceRgb(), 0.3f) // Here!!
.SetMarginBottom(10)
.SetKeepTogether(true);
// Here we create a cell of the header,
// image, description, notes, prices of out product card
return productTable;
}
而且你在截图上怎么看,我没有等于0.3f的颜色透明
iText 支持背景的透明颜色。请查看那里的样本:https://github.com/itext/itext7/tree/develop/layout/src/test/resources/com/itextpdf/layout/OpacityTest
例如,使用下一个片段我可以获得满意的结果:
Table table = new Table(1);
table.addCell(new Cell().setBackgroundColor(new DeviceRgb(200, 100, 50), 1f).add(new Paragraph("Cell with Opacoty 1")));
table.addCell(new Cell().setBackgroundColor(new DeviceRgb(200, 100, 50), 0.5f).add(new Paragraph("Cell with Opacoty 0.5")));
table.addCell(new Cell().setBackgroundColor(new DeviceRgb(200, 100, 50), 0.1f).add(new Paragraph("Cell with Opacoty 0.1")));
doc.add(table);
我可以在我的程序中使用透明的颜色吗?我想在我的 PDF 文件中使用这种颜色。我使用 iText 7.1.4 创建我的 pdf,但我不知道如何将 transpanrent 设置为 DeviceRgb
:
public static DeviceRgb ToDeviceRgb(this System.Windows.Media.Color color) =>
new DeviceRgb(color.R, color.G, color.B);
是否可以在 iText7 中使用带透明度的颜色?
更新:
我试着按照 Uladzimir Asipchuk 告诉我的去做,但我没有看到任何结果:
我可以在我的程序中编辑的产品卡片模板(边距、填充、颜色、字体等)。 在设置模板时,某些颜色可能有一个 alpha cannel(透明),我想在我的 PDF 文档中看到这个透明因素。 因此,正如 Uladzimir Asipchuk 建议我的那样,我将不透明度级别传递给 SetBackgroundColor 方法中的第二个参数:
public override Table CreateTemplate(Product product)
{
if(product == null) throw new ArgumentNullException(nameof(product));
// Create a table of the product card
var productTable = new Table(new UnitValue[] { UnitValue.CreatePercentValue(40), UnitValue.CreatePercentValue(60) })
.SetWidth(UnitValue.CreatePercentValue(100))
.SetBackgroundColor(Settings.BackgroundColor.ToDeviceRgb(), 0.3f) // Here!!
.SetMarginBottom(10)
.SetKeepTogether(true);
// Here we create a cell of the header,
// image, description, notes, prices of out product card
return productTable;
}
而且你在截图上怎么看,我没有等于0.3f的颜色透明
iText 支持背景的透明颜色。请查看那里的样本:https://github.com/itext/itext7/tree/develop/layout/src/test/resources/com/itextpdf/layout/OpacityTest
例如,使用下一个片段我可以获得满意的结果:
Table table = new Table(1);
table.addCell(new Cell().setBackgroundColor(new DeviceRgb(200, 100, 50), 1f).add(new Paragraph("Cell with Opacoty 1")));
table.addCell(new Cell().setBackgroundColor(new DeviceRgb(200, 100, 50), 0.5f).add(new Paragraph("Cell with Opacoty 0.5")));
table.addCell(new Cell().setBackgroundColor(new DeviceRgb(200, 100, 50), 0.1f).add(new Paragraph("Cell with Opacoty 0.1")));
doc.add(table);