c# - iTextSharp : PDF Gradient from Spot1 to Spot2

标签 c# pdf itext

据我了解,PDF 文件不能有从一种 ppot 颜色到另一种专色的渐变。使用 iTextSharp,如果我尝试这样做,当渐变停止点上的专色不匹配时会导致异常。

但是,Adobe Illustator CS6 能够创建包含从一种专色到另一种专色的线性渐变的 PDF 文件。

查看生成的 PDF 文件,我看到以下内容:

13 0 obj
<</Colorants 15 0 R/Subtype/NChannel>>
endobj
15 0 obj
<</Spot#20Blue 16 0 R/Spot#20Red 17 0 R>>
endobj
16 0 obj
[/Separation/Spot#20Blue/DeviceRGB<</C0[1.0 1.0 1.0]/C1[0.0 0.0 1.0]/Domain[0 1]/FunctionType 2/N 1.0/Range[0.0 1.0 0.0 1.0 0.0 1.0]>>]
endobj
17 0 obj
[/Separation/Spot#20Red/DeviceCMYK<</C0[0.0 0.0 0.0 0.0]/C1[0.0 0.993988 1.0 0.0]/Domain[0 1]/FunctionType 2/N 1.0/Range[0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0]>>]
endobj
9 0 obj
<</Color[20224 32768 65535]/Dimmed false/Editable true/Preview true/Printed true/Title(Layer 1)/Visible true>>
endobj 

根据以上内容,看起来多专色渐变可以使用“着色剂”字典来完成。

有谁知道如何使用 iTextSharp 或 iText (java) 执行此操作?我似乎无法在 iTextSharp 源代码中找到对 Colorants 的引用。

最佳答案

如果您手动定义类型 2 轴向 PdfShading 而不是使用 PdfShading.SimpleAxial,这似乎是可能的:

var dest = @"C:\publish\shaded_fill.pdf";

var document = new Document();
var writer = PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create));
document.Open();

var spotBlue = new PdfSpotColor("Spot Blue", new BaseColor(0.0f, 0.0f, 1.0f));
var spotRed = new PdfSpotColor("Spot Red", new CMYKColor(0.0f, 0.993988f, 1.0f, 0.0f));
var devn = new PdfDeviceNColor(new[] { spotBlue, spotRed });

var functionType2 = PdfFunction.Type2(
    writer,
    new[] { 0.0f, 1.0f },
    null,
    new[] { 0.0f, 1.0f },
    new[] { 1.0f, 0.0f },
    1.0f);

var functionType3 = PdfFunction.Type3(
    writer,
    new[] { 0.0f, 1.0f },
    null,
    new[] { functionType2 },
    new float[] { },
    new[] { 0.0f, 1.0f });

var shading = PdfShading.Type2(
    writer,
    new DeviceNColor(devn, new[] { 1.0f, 1.0f }),
    new[] { 0.0f, 0.0f, 1.0f, 0.0f },
    new[] { 0.0f, 1.0f },
    functionType3,
    new[] { true, true });

var pattern = new PdfShadingPattern(shading);

var x = 40f;
var y = 641.89f;
var w = 400f;
var h = 150f;

var canvas = writer.DirectContent;
canvas.SaveState();
canvas.Rectangle(x, y, w, h);
canvas.Clip();
canvas.NewPath();
canvas.SetShadingFill(pattern);
canvas.ConcatCTM(-w, 0f, 0f, w, x + w, y + (h / 2f));
canvas.PaintShading(shading);
canvas.RestoreState();

document.Close();

关于c# - iTextSharp : PDF Gradient from Spot1 to Spot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260224/

相关文章:

c# - 使用互操作将 Excel 转为 pdf

javascript - 在浏览器中显示 pdf 以进行 ajax 响应

c# - 分割 10k 页 PDF 时出现内存泄漏(iTextSharp PDF API)

c# - 当从代码后面单击按钮时,检查 Dataitemtemplate devexpress gridview 中的复选框状态

c# - 如何在不影响性能的情况下执行 LINQ GroupBy、Select 和 Take?

c# - LINQ to 对象列表差异

c# - .NET 继承 (WinForms) 表单 - VS 设计器问题

linux - 后记为 PDF 缩放以适合 A4

java - 如何用虚线给文本加下划线?

pdf - 如何使用 iText 将 XHTML 嵌套列表转换为 pdf?