如何通过代码为PDF文本设置Fill Overprint(填充叠印)
解决CMYK PDF中黑色文本叠印失效问题
问题说明
通过C#生成HTML转RGB PDF,再经Ghostscript转换为CMYK PDF后,在Adobe Acrobat的「Print Production」-「Output Preview」中取消勾选Process Black时,黑色文本变为白色,预期效果应为黑色文本开启叠印,透出下方黄色背景显示为黄色。已尝试设置theDoc.Rendering.ColorSpace = XRendering.ColorSpaceType.Cmyk;和theDoc.Rendering.Overprint = true;但未生效,需通过代码实现类似Illustrator中「Fill Overprint」的效果。
现有实现代码
C# 生成PDF代码
static void Main(string[] args) { // Create a new PDF document Doc theDoc = new Doc(); // HTML content string htmlContent = @" <html> <body style='background-color: yellow;'> <p style='color: black;'> Black text should become yellow when Process Black is unchecked in Output Preview in Acrobat </p> </body> </html>"; // Add the HTML to the document theDoc.HtmlOptions.Engine = EngineType.MSHtml; theDoc.Rendering.ColorSpace = XRendering.ColorSpaceType.Cmyk; theDoc.Rendering.Overprint = true; theDoc.AddImageHtml(htmlContent); foreach (IndirectObject io in theDoc.ObjectSoup) { if (io is StreamObject) { StreamObject so = (StreamObject)theDoc.ObjectSoup[io.ID]; so.Decompress(); string content = so.GetText(); // CMYK issue. // Pure yellow should stay pure yellow. Without this fix it makes ~97% yellow & ~5% Cyan string newContent = content.Replace("0 0 0 rg", "0 0 0 1 k") .Replace("0 0 0 RG", "0 0 0 1 K") .Replace("1 1 0 RG", "0 0 1 0 K") .Replace("1 1 0 rg", "0 0 1 0 k"); // Only set new content if changed if (newContent != content) so.SetText(newContent); so.Compress(); } } // Save the document theDoc.Save("output.pdf"); theDoc.Clear(); }
Ghostscript转换命令
gswin64c -q -dQUIET -dBATCH -dNOPAUSE -r300 -sDEVICE=pdfwrite -sOutputFile=output_cmyk.pdf -dProcessColorModel=/DeviceCMYK -sColorConversionStrategy=CMYK -dOverrideICC -sDefaultCMYKProfile=ISOnewspaper26v4.icc -dPDFX -f -dOPM=1 -dCompatibilityLevel=1.4 -dOverprint=/enable PDFX_def3.ps output.pdf
PDFX_def3.ps配置文件
systemdict /ProcessColorModel known { systemdict /ProcessColorModel get dup /DeviceGray ne exch /DeviceCMYK ne and } { true } ifelse { (ERROR: ProcessColorModel must be /DeviceGray or DeviceCMYK.)= /ProcessColorModel cvx /rangecheck signalerror } if % Define entries to the document Info dictionary : % /ICCProfile (ISOnewspaper26v4.icc) def % Customize or remove. [ /GTS_PDFXVersion (PDF/X-1a:2001) % Must be so (the standard requires). /Title (Classified Ad) % Customize. /Trapped /False % Must be so (Ghostscript doesn't provide other). /DOCINFO pdfmark % Define an ICC profile : currentdict /ICCProfile known { [/_objdef {icc_PDFX} /type /stream /OBJ pdfmark [{icc_PDFX} << /N systemdict /ProcessColorModel get /DeviceGray eq {1} {4} ifelse >> /PUT pdfmark [{icc_PDFX} ICCProfile (r) file /PUT pdfmark } if % Define the output intent dictionary : [/_objdef {OutputIntent_PDFX} /type /dict /OBJ pdfmark [{OutputIntent_PDFX} << /Type /OutputIntent % Must be so (the standard requires). /S /GTS_PDFX % Must be so (the standard requires). /OutputCondition (ISOnewspaper26v4) % Customize /Info (none) % Customize /OutputConditionIdentifier (ISOnewspaper26v4) % Customize /RegistryName (http://www.color.org) % Must be so (the standard requires). currentdict /ICCProfile known { /DestOutputProfile {icc_PDFX} % Must be so (see above). } if >> /PUT pdfmark [{Catalog} << /OutputIntents [ {OutputIntent_PDFX} ] >> /PUT pdfmark
解决方案
PDF中通过Tr操作符控制叠印行为:1 Tr开启填充叠印,0 Tr关闭填充叠印。需要在黑色文本的颜色设置前添加叠印开启指令,同时确保背景黄色关闭叠印(避免影响其他元素)。
修改后的C#代码(核心部分)
修改内容流替换逻辑,新增叠印控制指令:
foreach (IndirectObject io in theDoc.ObjectSoup) { if (io is StreamObject) { StreamObject so = (StreamObject)theDoc.ObjectSoup[io.ID]; so.Decompress(); string content = so.GetText(); // 修正CMYK颜色并添加叠印控制 string newContent = content.Replace("0 0 0 rg", "1 Tr 0 0 0 1 k") // 开启填充叠印+设置CMYK黑色 .Replace("0 0 0 RG", "1 Tr 0 0 0 1 K") .Replace("1 1 0 RG", "0 Tr 0 0 1 0 K") // 关闭填充叠印+设置CMYK黄色 .Replace("1 1 0 rg", "0 Tr 0 0 1 0 k"); // Only set new content if changed if (newContent != content) so.SetText(newContent); so.Compress(); } }
原理说明
1 Tr:开启填充叠印后,黑色文本不会挖空下方的黄色背景,当Process Black被取消时,黑色不输出,下方黄色背景自然透出。0 Tr:黄色背景关闭叠印,避免干扰其他元素的正常显示。- 保留Ghostscript命令中的
dOverprint=/enable参数,确保转换过程中叠印设置被正确保留。
内容的提问来源于stack exchange,提问作者K S
相关产品推荐
相关产品推荐

