static private PdfTemplate CreateBarcodeTemplate(PdfContentByte over, Barcode barcode, bool opaque, bool withBorder) { PdfTemplate template = over.CreateTemplate(0, 0); float borderSize = opaque && withBorder ? barcode.X * 3 : 0; // 3 ширины полоски if (opaque) { // Создадим п/у залитый белым цветом template.SetColorFill(BaseColor.WHITE); template.Rectangle(-borderSize, -borderSize, barcode.BarcodeSize.Width + borderSize * 2, barcode.BarcodeSize.Height + borderSize * 2); template.Fill(); } iTextSharp.text.Rectangle boundingBox = barcode.PlaceBarcode(template, BaseColor.BLACK, BaseColor.BLACK); if (borderSize != 0.0) { boundingBox.Right += borderSize; boundingBox.Left -= borderSize; boundingBox.Top += borderSize; if (barcode.Font != null) { // Если текст отображается, то не нужно расширять, потому что там и так место под текстом есть. boundingBox.Bottom -= borderSize; } } // Обрежем шаблон до размера barCode + граница template.BoundingBox = boundingBox; return(template); }
public static Image CreateRectangle(PdfWriter writer, float x, float y, float width, float height, BaseColor backColor) { PdfTemplate template = writer.DirectContent.CreateTemplate(width, height); template.SetColorFill(backColor); template.Rectangle(x, y, width, height); template.Fill(); writer.ReleaseTemplate(template); return(Image.GetInstance(template)); }
public void Sign(String src, String name, String dest, ICollection <X509Certificate> chain, ICipherParameters pk, String digestAlgorithm, CryptoStandard subfilter, String reason, String location) { // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileStream os = new FileStream(dest, FileMode.Create); PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0'); // Creating the appearance PdfSignatureAppearance appearance = stamper.SignatureAppearance; appearance.Reason = reason; appearance.Location = location; appearance.SetVisibleSignature(name); // Creating the appearance for layer 0 PdfTemplate n0 = appearance.GetLayer(0); float x = n0.BoundingBox.Left; float y = n0.BoundingBox.Bottom; float width = n0.BoundingBox.Width; float height = n0.BoundingBox.Height; n0.SetColorFill(BaseColor.LIGHT_GRAY); n0.Rectangle(x, y, width, height); n0.Fill(); // Creating the appearance for layer 2 PdfTemplate n2 = appearance.GetLayer(2); ColumnText ct = new ColumnText(n2); ct.SetSimpleColumn(n2.BoundingBox); Paragraph p = new Paragraph("This document was signed by Bruno Specimen."); ct.AddElement(p); ct.Go(); // Creating the signature IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, subfilter); }
public Chap1013() { Console.WriteLine("Chapter 10 Example 13: Spot Color"); // step 1: creation of a document-object Document document = new Document(); try { // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap1013.pdf", FileMode.Create)); BaseFont bf = BaseFont.CreateFont("Helvetica", "winansi", BaseFont.NOT_EMBEDDED); // step 3: we open the document document.Open(); // step 4: we add some content PdfContentByte cb = writer.DirectContent; // Note: I made up these names unless someone give me a PANTONE swatch as gift ([email protected]) PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f)); PdfSpotColor spc_rgb = new PdfSpotColor("PANTONE 147", 0.9f, new Color(114, 94, 38)); PdfSpotColor spc_g = new PdfSpotColor("PANTONE 100 CV", 0.5f, new GrayColor(0.9f)); // Stroke a rectangle with CMYK alternate cb.SetColorStroke(spc_cmyk, .5f); cb.SetLineWidth(10f); // draw a rectangle cb.Rectangle(100, 700, 100, 100); // add the diagonal cb.MoveTo(100, 700); cb.LineTo(200, 800); // stroke the lines cb.Stroke(); // Fill a rectangle with CMYK alternate cb.SetColorFill(spc_cmyk, spc_cmyk.Tint); cb.Rectangle(250, 700, 100, 100); cb.Fill(); // Stroke a circle with RGB alternate cb.SetColorStroke(spc_rgb, spc_rgb.Tint); cb.SetLineWidth(5f); cb.Circle(150f, 500f, 100f); cb.Stroke(); // Fill the circle with RGB alternate cb.SetColorFill(spc_rgb, spc_rgb.Tint); cb.Circle(150f, 500f, 50f); cb.Fill(); // example with colorfill cb.SetColorFill(spc_g, spc_g.Tint); cb.MoveTo(100f, 200f); cb.LineTo(200f, 250f); cb.LineTo(400f, 150f); cb.Fill(); document.NewPage(); String text = "Some text to show"; document.Add(new Paragraph(text, FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk)))); document.Add(new Paragraph(text, FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.5f)))); // example with template PdfTemplate t = cb.CreateTemplate(500f, 500f); // Stroke a rectangle with CMYK alternate t.SetColorStroke(new SpotColor(spc_cmyk, .5f)); t.SetLineWidth(10f); // draw a rectangle t.Rectangle(100, 10, 100, 100); // add the diagonal t.MoveTo(100, 10); t.LineTo(200, 100); // stroke the lines t.Stroke(); // Fill a rectangle with CMYK alternate t.SetColorFill(spc_g, spc_g.Tint); t.Rectangle(100, 125, 100, 100); t.Fill(); t.BeginText(); t.SetFontAndSize(bf, 20f); t.SetTextMatrix(1f, 0f, 0f, 1f, 10f, 10f); t.ShowText("Template text upside down"); t.EndText(); t.Rectangle(0, 0, 499, 499); t.Stroke(); cb.AddTemplate(t, -1.0f, 0.00f, 0.00f, -1.0f, 550f, 550f); } catch (Exception de) { Console.Error.WriteLine(de.Message); Console.Error.WriteLine(de.StackTrace); } // step 5: we close the document document.Close(); }
public void FillEllipse(Brush2 brush, float x, float y, float width, float height) { SetBrush(brush); template.Ellipse(x, currentHeight - y, x + width, currentHeight - (y + height)); template.Fill(); }
public Chap1014() { Console.WriteLine("Chapter 10 Example 14: colored patterns"); // step 1: creation of a document-object Document document = new Document(PageSize.A4, 50, 50, 50, 50); Document.Compress = false; try { // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap1014.pdf", FileMode.Create)); // step 3: we open the document document.Open(); // step 4: we add some content PdfContentByte cb = writer.DirectContent; PdfTemplate tp = cb.CreateTemplate(400, 300); PdfPatternPainter pat = cb.CreatePattern(15, 15, null); pat.Rectangle(5, 5, 5, 5); pat.Fill(); PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f)); SpotColor spot = new SpotColor(spc_cmyk); tp.SetPatternFill(pat, spot, .9f); tp.Rectangle(0, 0, 400, 300); tp.Fill(); cb.AddTemplate(tp, 50, 50); PdfPatternPainter pat2 = cb.CreatePattern(10, 10, null); pat2.SetLineWidth(2); pat2.MoveTo(-5, 0); pat2.LineTo(10, 15); pat2.Stroke(); pat2.MoveTo(0, -5); pat2.LineTo(15, 10); pat2.Stroke(); cb.SetLineWidth(1); cb.SetColorStroke(new Color(System.Drawing.Color.Black)); cb.SetPatternFill(pat2, new Color(System.Drawing.Color.Red)); cb.Rectangle(100, 400, 30, 210); cb.FillStroke(); cb.SetPatternFill(pat2, new Color(System.Drawing.Color.LightGreen)); cb.Rectangle(150, 400, 30, 100); cb.FillStroke(); cb.SetPatternFill(pat2, new Color(System.Drawing.Color.Blue)); cb.Rectangle(200, 400, 30, 130); cb.FillStroke(); cb.SetPatternFill(pat2, new GrayColor(0.5f)); cb.Rectangle(250, 400, 30, 80); cb.FillStroke(); cb.SetPatternFill(pat2, new GrayColor(0.7f)); cb.Rectangle(300, 400, 30, 170); cb.FillStroke(); cb.SetPatternFill(pat2, new GrayColor(0.9f)); cb.Rectangle(350, 400, 30, 40); cb.FillStroke(); } catch (Exception de) { Console.Error.WriteLine(de.Message); Console.Error.WriteLine(de.StackTrace); } // step 5: we close the document document.Close(); }