protected override void CheckColor(PdfWriter writer, int key, Object obj1) { switch (key) { case PdfIsoKeys.PDFISOKEY_COLOR: if (obj1 is ExtendedColor) { ExtendedColor ec = (ExtendedColor)obj1; switch (ec.Type) { case ExtendedColor.TYPE_CMYK: CheckColor(writer, PdfIsoKeys.PDFISOKEY_CMYK, obj1); break; case ExtendedColor.TYPE_GRAY: return; case ExtendedColor.TYPE_RGB: CheckColor(writer, PdfIsoKeys.PDFISOKEY_RGB, obj1); break; case ExtendedColor.TYPE_SEPARATION: SpotColor sc = (SpotColor)ec; CheckColor(writer, PdfIsoKeys.PDFISOKEY_COLOR, sc.PdfSpotColor.AlternativeCS); break; case ExtendedColor.TYPE_SHADING: ShadingColor xc = (ShadingColor)ec; CheckColor(writer, PdfIsoKeys.PDFISOKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace); break; case ExtendedColor.TYPE_PATTERN: PatternColor pc = (PatternColor)ec; CheckColor(writer, PdfIsoKeys.PDFISOKEY_COLOR, pc.Painter.DefaultColor); break; } } else if (obj1 is BaseColor) { CheckColor(writer, PdfIsoKeys.PDFISOKEY_RGB, obj1); } break; case PdfIsoKeys.PDFISOKEY_CMYK: if (rgbUsed) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicergb.and.devicecmyk.colorspaces.cannot.be.used.both.in.one.file")); } cmykUsed = true; break; case PdfIsoKeys.PDFISOKEY_RGB: if (cmykUsed) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicergb.and.devicecmyk.colorspaces.cannot.be.used.both.in.one.file")); } rgbUsed = true; break; } }
private void Form1_Load(object sender, EventArgs e) { //Test file name string TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"); //Standard iTextSharp setup using (FileStream fs = new FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (Document doc = new Document(PageSize.LETTER)) { using (PdfWriter w = PdfWriter.GetInstance(doc, fs)) { //Open the document for writing doc.Open(); //Create a shading object. The (x,y)'s appear to be document-level instead of cell-level so they need to be played with PdfShading shading = PdfShading.SimpleAxial(w, 0, 700, 300, 700, BaseColor.BLUE, BaseColor.RED); //Create a pattern from our shading object PdfShadingPattern pattern = new PdfShadingPattern(shading); //Create a color from our patter ShadingColor color = new ShadingColor(pattern); //Create a standard two column table PdfPTable t = new PdfPTable(2); //Add a text cell setting the background color through object initialization t.AddCell(new PdfPCell(new Phrase("Hello")) { BackgroundColor = color }); //Add another cell with everything inline. Notice that the (x,y)'s have been updated t.AddCell(new PdfPCell(new Phrase("World")) { BackgroundColor = new ShadingColor(new PdfShadingPattern(PdfShading.SimpleAxial(w, 400, 700, 600, 700, BaseColor.PINK, BaseColor.CYAN))) }); //Add the table to the document doc.Add(t); //Close the document doc.Close(); } } } this.Close(); }
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { //Create a shading object with cell-specific coords PdfShading shading = PdfShading.SimpleAxial(w, position.Left, position.Bottom, position.Right, position.Top, BaseColor.BLUE, BaseColor.RED); //Create a pattern from our shading object PdfShadingPattern pattern = new PdfShadingPattern(shading); //Create a color from our patter ShadingColor color = new ShadingColor(pattern); //Get the background canvas. NOTE, If using an older version of iTextSharp (4.x) you might need to get the canvas in a different way PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; //Set the background color of the given rectable to our shading pattern position.BackgroundColor = color; //Fill the rectangle cb.Rectangle(position); }
/** * Business logic that checks if a certain object is in conformance with PDF/X. * @param writer the writer that is supposed to write the PDF/X file * @param key the type of PDF ISO conformance that has to be checked * @param obj1 the object that is checked for conformance */ public static void CheckPDFXConformance(PdfWriter writer, int key, Object obj1) { if (writer == null || !writer.IsPdfX()) { return; } int conf = writer.PDFXConformance; switch (key) { case PdfIsoKeys.PDFISOKEY_COLOR: switch (conf) { case PdfWriter.PDFX1A2001: if (obj1 is ExtendedColor) { ExtendedColor ec = (ExtendedColor)obj1; switch (ec.Type) { case ExtendedColor.TYPE_CMYK: case ExtendedColor.TYPE_GRAY: return; case ExtendedColor.TYPE_RGB: throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed")); case ExtendedColor.TYPE_SEPARATION: SpotColor sc = (SpotColor)ec; CheckPDFXConformance(writer, PdfIsoKeys.PDFISOKEY_COLOR, sc.PdfSpotColor.AlternativeCS); break; case ExtendedColor.TYPE_SHADING: ShadingColor xc = (ShadingColor)ec; CheckPDFXConformance(writer, PdfIsoKeys.PDFISOKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace); break; case ExtendedColor.TYPE_PATTERN: PatternColor pc = (PatternColor)ec; CheckPDFXConformance(writer, PdfIsoKeys.PDFISOKEY_COLOR, pc.Painter.DefaultColor); break; } } else if (obj1 is BaseColor) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed")); } break; } break; case PdfIsoKeys.PDFISOKEY_CMYK: break; case PdfIsoKeys.PDFISOKEY_RGB: if (conf == PdfWriter.PDFX1A2001) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed")); } break; case PdfIsoKeys.PDFISOKEY_FONT: if (!((BaseFont)obj1).IsEmbedded()) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("all.the.fonts.must.be.embedded.this.one.isn.t.1", ((BaseFont)obj1).PostscriptFontName)); } break; case PdfIsoKeys.PDFISOKEY_IMAGE: PdfImage image = (PdfImage)obj1; if (image.Get(PdfName.SMASK) != null) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("the.smask.key.is.not.allowed.in.images")); } switch (conf) { case PdfWriter.PDFX1A2001: PdfObject cs = image.Get(PdfName.COLORSPACE); if (cs == null) { return; } if (cs.IsName()) { if (PdfName.DEVICERGB.Equals(cs)) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed")); } } else if (cs.IsArray()) { if (PdfName.CALRGB.Equals(((PdfArray)cs)[0])) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.calrgb.is.not.allowed")); } } break; } break; case PdfIsoKeys.PDFISOKEY_GSTATE: PdfDictionary gs = (PdfDictionary)obj1; PdfObject obj = gs.Get(PdfName.BM); if (obj != null && !PdfGState.BM_NORMAL.Equals(obj) && !PdfGState.BM_COMPATIBLE.Equals(obj)) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("blend.mode.1.not.allowed", obj.ToString())); } obj = gs.Get(PdfName.CA); double v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("transparency.is.not.allowed.ca.eq.1", v)); } obj = gs.Get(PdfName.ca_); v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("transparency.is.not.allowed.ca.eq.1", v)); } break; case PdfIsoKeys.PDFISOKEY_LAYER: throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("layers.are.not.allowed")); } }
/** * Business logic that checks if a certain object is in conformance with PDF/X. * @param writer the writer that is supposed to write the PDF/X file * @param key the type of PDF/X conformance that has to be checked * @param obj1 the object that is checked for conformance */ public static void CheckPDFXConformance(PdfWriter writer, int key, Object obj1) { if (writer == null || !writer.IsPdfX()) { return; } int conf = writer.PDFXConformance; switch (key) { case PDFXKEY_COLOR: switch (conf) { case PdfWriter.PDFX1A2001: if (obj1 is ExtendedColor) { ExtendedColor ec = (ExtendedColor)obj1; switch (ec.Type) { case ExtendedColor.TYPE_CMYK: case ExtendedColor.TYPE_GRAY: return; case ExtendedColor.TYPE_RGB: throw new PdfXConformanceException("Colorspace RGB is not allowed."); case ExtendedColor.TYPE_SEPARATION: SpotColor sc = (SpotColor)ec; CheckPDFXConformance(writer, PDFXKEY_COLOR, sc.PdfSpotColor.AlternativeCS); break; case ExtendedColor.TYPE_SHADING: ShadingColor xc = (ShadingColor)ec; CheckPDFXConformance(writer, PDFXKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace); break; case ExtendedColor.TYPE_PATTERN: PatternColor pc = (PatternColor)ec; CheckPDFXConformance(writer, PDFXKEY_COLOR, pc.Painter.DefaultColor); break; } } else if (obj1 is Color) { throw new PdfXConformanceException("Colorspace RGB is not allowed."); } break; } break; case PDFXKEY_CMYK: break; case PDFXKEY_RGB: if (conf == PdfWriter.PDFX1A2001) { throw new PdfXConformanceException("Colorspace RGB is not allowed."); } break; case PDFXKEY_FONT: if (!((BaseFont)obj1).IsEmbedded()) { throw new PdfXConformanceException("All the fonts must be embedded. This one isn't: " + ((BaseFont)obj1).PostscriptFontName); } break; case PDFXKEY_IMAGE: PdfImage image = (PdfImage)obj1; if (image.Get(PdfName.SMASK) != null) { throw new PdfXConformanceException("The /SMask key is not allowed in images."); } switch (conf) { case PdfWriter.PDFX1A2001: PdfObject cs = image.Get(PdfName.COLORSPACE); if (cs == null) { return; } if (cs.IsName()) { if (PdfName.DEVICERGB.Equals(cs)) { throw new PdfXConformanceException("Colorspace RGB is not allowed."); } } else if (cs.IsArray()) { if (PdfName.CALRGB.Equals(((PdfArray)cs)[0])) { throw new PdfXConformanceException("Colorspace CalRGB is not allowed."); } } break; } break; case PDFXKEY_GSTATE: PdfDictionary gs = (PdfDictionary)obj1; PdfObject obj = gs.Get(PdfName.BM); if (obj != null && !PdfGState.BM_NORMAL.Equals(obj) && !PdfGState.BM_COMPATIBLE.Equals(obj)) { throw new PdfXConformanceException("Blend mode " + obj.ToString() + " not allowed."); } obj = gs.Get(PdfName.CA); double v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfXConformanceException("Transparency is not allowed: /CA = " + v); } obj = gs.Get(PdfName.ca_); v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfXConformanceException("Transparency is not allowed: /ca = " + v); } break; case PDFXKEY_LAYER: throw new PdfXConformanceException("Layers are not allowed."); } }