// =========================================================================== public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 PdfContentByte cb = writer.DirectContent; float gap = (document.PageSize.Width - 400) / 3; PictureBackdrop(gap, 500, cb, writer); PictureBackdrop(200 + 2 * gap, 500, cb, writer); PictureBackdrop(gap, 500 - 200 - gap, cb, writer); PictureBackdrop(200 + 2 * gap, 500 - 200 - gap, cb, writer); PdfTemplate tp; PdfTransparencyGroup group; tp = cb.CreateTemplate(200, 200); PictureCircles(0, 0, tp); group = new PdfTransparencyGroup(); group.Isolated = true; group.Knockout = true; tp.Group = group; cb.AddTemplate(tp, gap, 500); tp = cb.CreateTemplate(200, 200); PictureCircles(0, 0, tp); group = new PdfTransparencyGroup(); group.Isolated = true; group.Knockout = false; tp.Group = group; cb.AddTemplate(tp, 200 + 2 * gap, 500); tp = cb.CreateTemplate(200, 200); PictureCircles(0, 0, tp); group = new PdfTransparencyGroup(); group.Isolated = false; group.Knockout = true; tp.Group = group; cb.AddTemplate(tp, gap, 500 - 200 - gap); tp = cb.CreateTemplate(200, 200); PictureCircles(0, 0, tp); group = new PdfTransparencyGroup(); group.Isolated = false; group.Knockout = false; tp.Group = group; cb.AddTemplate(tp, 200 + 2 * gap, 500 - 200 - gap); } }
public virtual void TransparencyCheckTest1() { NUnit.Framework.Assert.That(() => { PdfWriter writer = new PdfWriter(new MemoryStream()); Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read); PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1" , @is); PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent); doc.AddNewPage(); PdfCanvas canvas = new PdfCanvas(doc.GetLastPage()); PdfFormXObject xObject = new PdfFormXObject(new Rectangle(100, 100)); PdfCanvas xObjCanvas = new PdfCanvas(xObject, doc); xObjCanvas.Rectangle(30, 30, 10, 10).Fill(); PdfTransparencyGroup group = new PdfTransparencyGroup(); xObject.SetGroup(group); canvas.AddXObject(xObject, new Rectangle(300, 300)); doc.Close(); } , NUnit.Framework.Throws.InstanceOf <PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.A_GROUP_OBJECT_WITH_AN_S_KEY_WITH_A_VALUE_OF_TRANSPARENCY_SHALL_NOT_BE_INCLUDED_IN_A_FORM_XOBJECT)) ; }
// --------------------------------------------------------------------------- public byte[] CreatePdf() { using (MemoryStream ms = new MemoryStream()) { // step 1 using (Document document = new Document(new Rectangle(850, 600))) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, ms); // step 3 document.Open(); // step 4 PdfContentByte canvas = writer.DirectContent; // add the clipped image Image img = Image.GetInstance( Path.Combine(Utility.ResourceImage, RESOURCE) ); float w = img.ScaledWidth; float h = img.ScaledHeight; canvas.Ellipse(1, 1, 848, 598); canvas.Clip(); canvas.NewPath(); canvas.AddImage(img, w, 0, 0, h, 0, -600); // Create a transparent PdfTemplate PdfTemplate t2 = writer.DirectContent.CreateTemplate(850, 600); PdfTransparencyGroup transGroup = new PdfTransparencyGroup(); transGroup.Put(PdfName.CS, PdfName.DEVICEGRAY); transGroup.Isolated = true; transGroup.Knockout = false; t2.Group = transGroup; // Add transparent ellipses to the template int gradationStep = 30; float[] gradationRatioList = new float[gradationStep]; for (int i = 0; i < gradationStep; i++) { /* * gotta love .NET, guess they forgot to copy java.lang.Math.toRadians */ double radians = (Math.PI / 180) * 90.0f / gradationStep * (i + 1); gradationRatioList[i] = 1 - (float)Math.Sin(radians); } for (int i = 1; i < gradationStep + 1; i++) { t2.SetLineWidth(5 * (gradationStep + 1 - i)); t2.SetGrayStroke(gradationRatioList[gradationStep - i]); t2.Ellipse(0, 0, 850, 600); t2.Stroke(); } // Create an image mask for the direct content PdfDictionary maskDict = new PdfDictionary(); maskDict.Put(PdfName.TYPE, PdfName.MASK); maskDict.Put(PdfName.S, new PdfName("Luminosity")); maskDict.Put(new PdfName("G"), t2.IndirectReference); PdfGState gState = new PdfGState(); gState.Put(PdfName.SMASK, maskDict); canvas.SetGState(gState); canvas.AddTemplate(t2, 0, 0); } return(ms.ToArray()); } }
// =========================================================================== public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 PdfContentByte cb = writer.DirectContent; float gap = (document.PageSize.Width - 400) / 3; PictureBackdrop(gap, 500, cb); PictureBackdrop(200 + 2 * gap, 500, cb); PictureBackdrop(gap, 500 - 200 - gap, cb); PictureBackdrop(200 + 2 * gap, 500 - 200 - gap, cb); PictureCircles(gap, 500, cb); cb.SaveState(); PdfGState gs1 = new PdfGState(); gs1.FillOpacity = 0.5f; cb.SetGState(gs1); PictureCircles(200 + 2 * gap, 500, cb); cb.RestoreState(); cb.SaveState(); PdfTemplate tp = cb.CreateTemplate(200, 200); PdfTransparencyGroup group = new PdfTransparencyGroup(); tp.Group = group; PictureCircles(0, 0, tp); cb.SetGState(gs1); cb.AddTemplate(tp, gap, 500 - 200 - gap); cb.RestoreState(); cb.SaveState(); tp = cb.CreateTemplate(200, 200); tp.Group = group; PdfGState gs2 = new PdfGState(); gs2.FillOpacity = 0.5f; gs2.BlendMode = PdfGState.BM_HARDLIGHT; tp.SetGState(gs2); PictureCircles(0, 0, tp); cb.AddTemplate(tp, 200 + 2 * gap, 500 - 200 - gap); cb.RestoreState(); cb.ResetRGBColorFill(); ColumnText ct = new ColumnText(cb); Phrase ph = new Phrase("Ungrouped objects\nObject opacity = 1.0"); ct.SetSimpleColumn(ph, gap, 0, gap + 200, 500, 18, Element.ALIGN_CENTER ); ct.Go(); ph = new Phrase("Ungrouped objects\nObject opacity = 0.5"); ct.SetSimpleColumn(ph, 200 + 2 * gap, 0, 200 + 2 * gap + 200, 500, 18, Element.ALIGN_CENTER ); ct.Go(); ph = new Phrase( "Transparency group\nObject opacity = 1.0\nGroup opacity = 0.5\nBlend mode = Normal" ); ct.SetSimpleColumn(ph, gap, 0, gap + 200, 500 - 200 - gap, 18, Element.ALIGN_CENTER ); ct.Go(); ph = new Phrase( "Transparency group\nObject opacity = 0.5\nGroup opacity = 1.0\nBlend mode = HardLight" ); ct.SetSimpleColumn(ph, 200 + 2 * gap, 0, 200 + 2 * gap + 200, 500 - 200 - gap, 18, Element.ALIGN_CENTER ); ct.Go(); } }