/// <summary> /// Draws content on a page using colors created from a separation colorspace. /// </summary> /// <param name="page">The page to draw to.</param> private static void DrawSeparationColorSpace(PDFPage page) { PDFRgbColor black = new PDFRgbColor(0, 0, 0); PDFBrush blackBrush = new PDFBrush(black); //PDF4NET v5: PDFFont font = new PDFFont(PDFFontFace.Helvetica, 16); PDFStandardFont font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16); //PDF4NET v5: page.Canvas.DrawText("Separation colorspace", font, null, blackBrush, 50, 50); page.Canvas.DrawString("Separation colorspace", font, blackBrush, 50, 50); //PDF4NET v5: PDFType2Function tintTransform = new PDFType2Function(); PDFExponentialFunction tintTransform = new PDFExponentialFunction(); tintTransform.C0 = new double[] { 0, 0, 0, 0 }; tintTransform.C1 = new double[] { 0, 0.53f, 1, 0 }; tintTransform.Exponent = 1; tintTransform.Domain = new double[] { 0, 1 }; tintTransform.Range = new double[] { 0, 1, 0, 1, 0, 1, 0, 1 }; PDFSeparationColorSpace separationCS = new PDFSeparationColorSpace(); separationCS.Colorant = "PANTONE Orange 021 C"; separationCS.TintTransform = tintTransform; PDFSeparationColor orange = new PDFSeparationColor(separationCS); orange.Tint = 0.9; //PDF4NET v5: tintTransform = new PDFType2Function(); tintTransform = new PDFExponentialFunction(); tintTransform.C0 = new double[] { 0, 0, 0, 0 }; tintTransform.C1 = new double[] { 0, 0.75f, 0.9f, 0 }; tintTransform.Exponent = 1; tintTransform.Domain = new double[] { 0, 1 }; tintTransform.Range = new double[] { 0, 1, 0, 1, 0, 1, 0, 1 }; separationCS = new PDFSeparationColorSpace(); separationCS.Colorant = "PANTONE Warm Red C"; separationCS.TintTransform = tintTransform; PDFSeparationColor warmRed = new PDFSeparationColor(separationCS); warmRed.Tint = 0.4; PDFBrush orangeBrush = new PDFBrush(orange); PDFPen warmRedPen = new PDFPen(warmRed, 5); page.Canvas.DrawRoundRectangle(warmRedPen, orangeBrush, 50, 100, 400, 150, 20, 20); }
protected override void TransformOperator(PDFContentStreamOperator input, List <PDFContentStreamOperator> output) { PDFContentStreamOperator op = input; switch (input.Type) { case PDFContentStreamOperatorType.SaveGraphicsState: strokeSeparationStack.Push(isStrokeSeparationActive); fillSeparationStack.Push(isFillSeparationActive); break; case PDFContentStreamOperatorType.RestoreGraphicsState: isStrokeSeparationActive = strokeSeparationStack.Pop(); isFillSeparationActive = fillSeparationStack.Pop(); break; case PDFContentStreamOperatorType.MoveTo: case PDFContentStreamOperatorType.LineTo: case PDFContentStreamOperatorType.CCurveTo: case PDFContentStreamOperatorType.VCurveTo: case PDFContentStreamOperatorType.YCurveTo: case PDFContentStreamOperatorType.Rectangle: case PDFContentStreamOperatorType.CloseSubpath: operators.Add(input); op = null; break; case PDFContentStreamOperatorType.SetRgbFill: case PDFContentStreamOperatorType.SetCmykFill: case PDFContentStreamOperatorType.SetGrayFill: isFillSeparationActive = false; break; case PDFContentStreamOperatorType.SetRgbStroke: case PDFContentStreamOperatorType.SetCmykStroke: case PDFContentStreamOperatorType.SetGrayStroke: isStrokeSeparationActive = false; break; case PDFContentStreamOperatorType.SetFillColorSpace: PDFSetFillColorSpaceOperator fillColorSpaceOperator = input as PDFSetFillColorSpaceOperator; PDFSeparationColorSpace fillSepCs = fillColorSpaceOperator.ColorSpace as PDFSeparationColorSpace; isFillSeparationActive = (fillSepCs != null) && (fillSepCs.Colorant == separationName); break; case PDFContentStreamOperatorType.SetStrokeColorSpace: PDFSetStrokeColorSpaceOperator strokeColorSpaceOperator = input as PDFSetStrokeColorSpaceOperator; PDFSeparationColorSpace strokeSepCs = strokeColorSpaceOperator.ColorSpace as PDFSeparationColorSpace; isStrokeSeparationActive = (strokeSepCs != null) && (strokeSepCs.Colorant == separationName); break; case PDFContentStreamOperatorType.SetClipNonZero: case PDFContentStreamOperatorType.SetClipEvenOdd: output.AddRange(operators); operators.Clear(); break; case PDFContentStreamOperatorType.EndPath: if (operators.Count > 0) { op = null; operators.Clear(); } else { // we had a sequence "path W/W* n", points already cleared by W/W*, n needs to be kept } break; case PDFContentStreamOperatorType.Stroke: case PDFContentStreamOperatorType.CloseStroke: // The path painting operator is discarded and the path points are cleared if ((isStrokeSeparationActive && !keepSeparation) || (!isStrokeSeparationActive && keepSeparation)) { if (operators.Count > 0) { op = null; } else // we had a sequence "path W/W* S/s", points already cleared by W, S/s needs to be replaced by n { op = new PDFContentStreamOperator(PDFContentStreamOperatorType.EndPath); } } else { output.AddRange(operators); } operators.Clear(); break; case PDFContentStreamOperatorType.FillNonZero: case PDFContentStreamOperatorType.FillNonZero2: case PDFContentStreamOperatorType.FillOddEven: // The path painting operator is discarded and the path points are cleared if ((isFillSeparationActive && !keepSeparation) || (!isFillSeparationActive && keepSeparation)) { if (operators.Count > 0) { op = null; } else // we had a sequence "path W/W* f/f*", points already cleared by W, f/f* needs to be replaced by n { op = new PDFContentStreamOperator(PDFContentStreamOperatorType.EndPath); } } else { output.AddRange(operators); } operators.Clear(); break; case PDFContentStreamOperatorType.CloseFillNonZeroStroke: case PDFContentStreamOperatorType.CloseFillEvenOddStroke: case PDFContentStreamOperatorType.FillNonZeroStroke: case PDFContentStreamOperatorType.FillEvenOddStroke: int discardContent = 0; // The path painting operator is transformed into no-op when the path needs to be discarded if ((isStrokeSeparationActive && !keepSeparation) || (!isStrokeSeparationActive && keepSeparation)) { discardContent += 1; } if ((isFillSeparationActive && !keepSeparation) || (!isFillSeparationActive && keepSeparation)) { discardContent += 2; } switch (discardContent) { case 3: // Discard all if (operators.Count > 0) { op = null; } else // we had a sequence "path W/W* B/B*/b/b*", points already cleared by W, B/B*/b/b* needs to be replaced by n { op = new PDFContentStreamOperator(PDFContentStreamOperatorType.EndPath); } break; case 2: // Discard fill if ((input.Type == PDFContentStreamOperatorType.CloseFillNonZeroStroke) || (input.Type == PDFContentStreamOperatorType.CloseFillEvenOddStroke)) { op = new PDFContentStreamOperator(PDFContentStreamOperatorType.CloseStroke); } else { op = new PDFContentStreamOperator(PDFContentStreamOperatorType.Stroke); } break; case 1: // Discard stroke if ((input.Type == PDFContentStreamOperatorType.CloseFillNonZeroStroke) || (input.Type == PDFContentStreamOperatorType.FillNonZeroStroke)) { op = new PDFContentStreamOperator(PDFContentStreamOperatorType.FillNonZero); } else { op = new PDFContentStreamOperator(PDFContentStreamOperatorType.FillOddEven); } break; case 0: output.AddRange(operators); break; } operators.Clear(); break; case PDFContentStreamOperatorType.DisplayXObject: PDFDisplayImageXObjectOperator diop = input as PDFDisplayImageXObjectOperator; if (diop != null) { PDFSeparationColorSpace imageSepCs = diop.Image.ColorSpace as PDFSeparationColorSpace; bool isImageSeparationActive = (imageSepCs != null) && (imageSepCs.Colorant == separationName); if ((isImageSeparationActive && !keepSeparation) || (!isImageSeparationActive && keepSeparation)) { op = null; } } break; case PDFContentStreamOperatorType.ShowText: case PDFContentStreamOperatorType.ShowText2: case PDFContentStreamOperatorType.ShowText3: case PDFContentStreamOperatorType.ShowText4: // The text showing operator is simply discarded if ((isFillSeparationActive && !keepSeparation) || (!isFillSeparationActive && keepSeparation)) { op = null; } break; case PDFContentStreamOperatorType.SetShading: PDFSetShadingOperator sso = input as PDFSetShadingOperator; PDFSeparationColorSpace shadingSepCs = sso.Shading.ColorSpace as PDFSeparationColorSpace; bool isShadingSeparationActive = (shadingSepCs != null) && (shadingSepCs.Colorant == separationName); if ((isShadingSeparationActive && !keepSeparation) || (!isShadingSeparationActive && keepSeparation)) { op = null; } break; } if (op != null) { output.Add(op); } }
private static void DrawColorsAndColorSpaces(PDFPage page, PDFFont titleFont, PDFFont sectionFont, Stream iccStream) { PDFBrush brush = new PDFBrush(); page.Canvas.DrawString("Colors and colorspaces", titleFont, brush, 20, 50); page.Canvas.DrawString("DeviceRGB", sectionFont, brush, 20, 70); PDFPen rgbPen = new PDFPen(PDFRgbColor.DarkRed, 4); PDFBrush rgbBrush = new PDFBrush(PDFRgbColor.LightGoldenrodYellow); page.Canvas.DrawRectangle(rgbPen, rgbBrush, 20, 85, 250, 100); page.Canvas.DrawString("DeviceCMYK", sectionFont, brush, 340, 70); PDFPen cmykPen = new PDFPen(new PDFCmykColor(1, 0.5, 0, 0.1), 4); PDFBrush cmykBrush = new PDFBrush(new PDFCmykColor(0, 0.5, 0.43, 0)); page.Canvas.DrawRectangle(cmykPen, cmykBrush, 340, 85, 250, 100); page.Canvas.DrawString("DeviceGray", sectionFont, brush, 20, 200); PDFPen grayPen = new PDFPen(new PDFGrayColor(0.1), 4); PDFBrush grayBrush = new PDFBrush(new PDFGrayColor(0.75)); page.Canvas.DrawRectangle(grayPen, grayBrush, 20, 215, 250, 100); page.Canvas.DrawString("Indexed", sectionFont, brush, 340, 200); PDFIndexedColorSpace indexedColorSpace = new PDFIndexedColorSpace(); indexedColorSpace.ColorCount = 2; indexedColorSpace.BaseColorSpace = new PDFRgbColorSpace(); indexedColorSpace.ColorTable = new byte[] { 192, 0, 0, 0, 0, 128 }; PDFIndexedColor indexedColor0 = new PDFIndexedColor(indexedColorSpace); indexedColor0.ColorIndex = 0; PDFIndexedColor indexedColor1 = new PDFIndexedColor(indexedColorSpace); indexedColor1.ColorIndex = 1; PDFPen indexedPen = new PDFPen(indexedColor0, 4); PDFBrush indexedBrush = new PDFBrush(indexedColor1); page.Canvas.DrawRectangle(indexedPen, indexedBrush, 340, 215, 250, 100); page.Canvas.DrawString("CalGray", sectionFont, brush, 20, 330); PDFCalGrayColorSpace calGrayColorSpace = new PDFCalGrayColorSpace(); PDFCalGrayColor calGrayColor1 = new PDFCalGrayColor(calGrayColorSpace); calGrayColor1.Gray = 0.6; PDFCalGrayColor calGrayColor2 = new PDFCalGrayColor(calGrayColorSpace); calGrayColor2.Gray = 0.2; PDFPen calGrayPen = new PDFPen(calGrayColor1, 4); PDFBrush calGrayBrush = new PDFBrush(calGrayColor2); page.Canvas.DrawRectangle(calGrayPen, calGrayBrush, 20, 345, 250, 100); page.Canvas.DrawString("CalRGB", sectionFont, brush, 340, 330); PDFCalRgbColorSpace calRgbColorSpace = new PDFCalRgbColorSpace(); PDFCalRgbColor calRgbColor1 = new PDFCalRgbColor(calRgbColorSpace); calRgbColor1.Red = 0.1; calRgbColor1.Green = 0.5; calRgbColor1.Blue = 0.25; PDFCalRgbColor calRgbColor2 = new PDFCalRgbColor(calRgbColorSpace); calRgbColor2.Red = 0.6; calRgbColor2.Green = 0.1; calRgbColor2.Blue = 0.9; PDFPen calRgbPen = new PDFPen(calRgbColor1, 4); PDFBrush calRgbBrush = new PDFBrush(calRgbColor2); page.Canvas.DrawRectangle(calRgbPen, calRgbBrush, 340, 345, 250, 100); page.Canvas.DrawString("L*a*b", sectionFont, brush, 20, 460); PDFLabColorSpace labColorSpace = new PDFLabColorSpace(); PDFLabColor labColor1 = new PDFLabColor(labColorSpace); labColor1.L = 90; labColor1.A = -40; labColor1.B = 120; PDFLabColor labColor2 = new PDFLabColor(labColorSpace); labColor2.L = 45; labColor2.A = 90; labColor2.B = -34; PDFPen labPen = new PDFPen(labColor1, 4); PDFBrush labBrush = new PDFBrush(labColor2); page.Canvas.DrawRectangle(labPen, labBrush, 20, 475, 250, 100); page.Canvas.DrawString("Icc", sectionFont, brush, 340, 460); PDFIccColorSpace iccColorSpace = new PDFIccColorSpace(); byte[] iccData = new byte[iccStream.Length]; iccStream.Read(iccData, 0, iccData.Length); iccColorSpace.IccProfile = iccData; iccColorSpace.AlternateColorSpace = new PDFRgbColorSpace(); iccColorSpace.ColorComponents = 3; PDFIccColor iccColor1 = new PDFIccColor(iccColorSpace); iccColor1.ColorComponents = new double[] { 0.45, 0.1, 0.22 }; PDFIccColor iccColor2 = new PDFIccColor(iccColorSpace); iccColor2.ColorComponents = new double[] { 0.21, 0.76, 0.31 }; PDFPen iccPen = new PDFPen(iccColor1, 4); PDFBrush iccBrush = new PDFBrush(iccColor2); page.Canvas.DrawRectangle(iccPen, iccBrush, 340, 475, 250, 100); page.Canvas.DrawString("Separation", sectionFont, brush, 20, 590); PDFExponentialFunction tintTransform = new PDFExponentialFunction(); tintTransform.Domain = new double[] { 0, 1 }; tintTransform.Range = new double[] { 0, 1, 0, 1, 0, 1, 0, 1 }; tintTransform.Exponent = 1; tintTransform.C0 = new double[] { 0, 0, 0, 0 }; tintTransform.C1 = new double[] { 1, 0.5, 0, 0.1 }; PDFSeparationColorSpace separationColorSpace = new PDFSeparationColorSpace(); separationColorSpace.AlternateColorSpace = new PDFCmykColorSpace(); separationColorSpace.Colorant = "Custom Blue"; separationColorSpace.TintTransform = tintTransform; PDFSeparationColor separationColor1 = new PDFSeparationColor(separationColorSpace); separationColor1.Tint = 0.23; PDFSeparationColor separationColor2 = new PDFSeparationColor(separationColorSpace); separationColor2.Tint = 0.74; PDFPen separationPen = new PDFPen(separationColor1, 4); PDFBrush separationBrush = new PDFBrush(separationColor2); page.Canvas.DrawRectangle(separationPen, separationBrush, 20, 605, 250, 100); page.Canvas.DrawString("Pantone", sectionFont, brush, 340, 590); PDFPen pantonePen = new PDFPen(PDFPantoneColor.ReflexBlue, 4); PDFBrush pantoneBrush = new PDFBrush(PDFPantoneColor.RhodamineRed); page.Canvas.DrawRectangle(pantonePen, pantoneBrush, 340, 605, 250, 100); page.Canvas.CompressAndClose(); }