// Private Methods private void addMarkerLineToChart(float y1) { _template.SetLineWidth(HorizontalLineWidth); _template.SetColorStroke(HorizontalGridColor); _template.MoveTo(Margin + _leftMargin, y1); _template.LineTo(Margin + _leftMargin - 4, y1); _template.Stroke(); }
// Private Methods private void addMarkerLineToChart(float x1) { _template.SetLineWidth(HorizontalLineWidth); _template.SetColorStroke(HorizontalGridColor); _template.MoveTo(x1, Margin + _bottomMargin); _template.LineTo(x1, Margin + _bottomMargin - 4); _template.Stroke(); }
private void drawCircle() { _template.SetLineWidth(1f); _template.Circle(_circleCenterX0, _circleCenterY0, _circleRadius); _template.SetColorStroke(BaseColor.GRAY); _template.Stroke(); _template.SetLineWidth(0f); }
private void SetPen(Pen2 pen) { template.SetRGBColorStroke(pen.Color.R, pen.Color.G, pen.Color.B); template.SetLineWidth(pen.Width); switch (pen.DashCap) { case DashCap2.Round: template.SetLineCap(PdfContentByte.LINE_CAP_ROUND); break; } switch (pen.DashStyle) { case DashStyle2.Solid: template.SetLineDash(0f); break; case DashStyle2.Dash: template.SetLineDash(pen.DashPattern, pen.DashOffset); break; case DashStyle2.Custom: template.SetLineDash(pen.DashPattern, pen.DashOffset); break; } }
public void ImageTaggingExpansionTest() { String filename = "TextExpansionTest.pdf"; Document doc = new Document(PageSize.LETTER, 72, 72, 72, 72); PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(DEST_FOLDER + filename, FileMode.Create)); writer.SetTagged(); doc.Open(); Chunk c1 = new Chunk("ABC"); c1.SetTextExpansion("the alphabet"); Paragraph p1 = new Paragraph(); p1.Add(c1); doc.Add(p1); PdfTemplate t = writer.DirectContent.CreateTemplate(6, 6); t.SetLineWidth(1f); t.Circle(3f, 3f, 1.5f); t.SetGrayFill(0); t.FillStroke(); Image i = Image.GetInstance(t); Chunk c2 = new Chunk(i, 100, -100); doc.Add(c2); Chunk c3 = new Chunk("foobar"); c3.SetTextExpansion("bar bar bar"); Paragraph p3 = new Paragraph(); p3.Add(c3); doc.Add(p3); doc.Close(); CompareTool compareTool = new CompareTool(); String error = compareTool.CompareByContent(DEST_FOLDER + filename, SOURCE_FOLDER + "cmp_" + filename, DEST_FOLDER, "diff_"); if (error != null) { Assert.Fail(error); } }
public PdfTemplate AddPageFooter(int PageNumber) { PdfTemplate footerTemplate = cb.CreateTemplate(500, 55); footerTemplate.BeginText(); BaseFont bf2 = BaseFont.CreateFont(BaseFont.TIMES_ITALIC, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); footerTemplate.SetFontAndSize(bf2, 11); footerTemplate.SetColorStroke(BaseColor.DARK_GRAY); footerTemplate.SetColorFill(BaseColor.GRAY); int al = -200; footerTemplate.SetLineWidth(3); footerTemplate.LineTo(500, footerTemplate.YTLM); string texttoadd = "Page: " + PageNumber.ToString(); float widthoftext = 500.0f - bf2.GetWidthPoint(texttoadd, 11); footerTemplate.ShowTextAligned(al, texttoadd, widthoftext, 30, 0); footerTemplate.EndText(); return(footerTemplate); }
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 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 canvas = writer.DirectContent; // line widths canvas.SaveState(); for (int i = 25; i > 0; i--) { canvas.SetLineWidth((float)i / 10); canvas.MoveTo(50, 806 - (5 * i)); canvas.LineTo(320, 806 - (5 * i)); canvas.Stroke(); } canvas.RestoreState(); // line cap canvas.MoveTo(350, 800); canvas.LineTo(350, 750); canvas.MoveTo(540, 800); canvas.LineTo(540, 750); canvas.Stroke(); canvas.SaveState(); canvas.SetLineWidth(8); canvas.SetLineCap(PdfContentByte.LINE_CAP_BUTT); canvas.MoveTo(350, 790); canvas.LineTo(540, 790); canvas.Stroke(); canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND); canvas.MoveTo(350, 775); canvas.LineTo(540, 775); canvas.Stroke(); canvas.SetLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE); canvas.MoveTo(350, 760); canvas.LineTo(540, 760); canvas.Stroke(); canvas.RestoreState(); // join miter canvas.SaveState(); canvas.SetLineWidth(8); canvas.SetLineJoin(PdfContentByte.LINE_JOIN_MITER); canvas.MoveTo(387, 700); canvas.LineTo(402, 730); canvas.LineTo(417, 700); canvas.Stroke(); canvas.SetLineJoin(PdfContentByte.LINE_JOIN_ROUND); canvas.MoveTo(427, 700); canvas.LineTo(442, 730); canvas.LineTo(457, 700); canvas.Stroke(); canvas.SetLineJoin(PdfContentByte.LINE_JOIN_BEVEL); canvas.MoveTo(467, 700); canvas.LineTo(482, 730); canvas.LineTo(497, 700); canvas.Stroke(); canvas.RestoreState(); // line dash canvas.SaveState(); canvas.SetLineWidth(3); canvas.MoveTo(50, 660); canvas.LineTo(320, 660); canvas.Stroke(); canvas.SetLineDash(6, 0); canvas.MoveTo(50, 650); canvas.LineTo(320, 650); canvas.Stroke(); canvas.SetLineDash(6, 3); canvas.MoveTo(50, 640); canvas.LineTo(320, 640); canvas.Stroke(); canvas.SetLineDash(15, 10, 5); canvas.MoveTo(50, 630); canvas.LineTo(320, 630); canvas.Stroke(); float[] dash1 = { 10, 5, 5, 5, 20 }; canvas.SetLineDash(dash1, 5); canvas.MoveTo(50, 620); canvas.LineTo(320, 620); canvas.Stroke(); float[] dash2 = { 9, 6, 0, 6 }; canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND); canvas.SetLineDash(dash2, 0); canvas.MoveTo(50, 610); canvas.LineTo(320, 610); canvas.Stroke(); canvas.RestoreState(); // miter limit PdfTemplate hooks = canvas.CreateTemplate(300, 120); hooks.SetLineWidth(8); hooks.MoveTo(46, 50); hooks.LineTo(65, 80); hooks.LineTo(84, 50); hooks.Stroke(); hooks.MoveTo(87, 50); hooks.LineTo(105, 80); hooks.LineTo(123, 50); hooks.Stroke(); hooks.MoveTo(128, 50); hooks.LineTo(145, 80); hooks.LineTo(162, 50); hooks.Stroke(); hooks.MoveTo(169, 50); hooks.LineTo(185, 80); hooks.LineTo(201, 50); hooks.Stroke(); hooks.MoveTo(210, 50); hooks.LineTo(225, 80); hooks.LineTo(240, 50); hooks.Stroke(); canvas.SaveState(); canvas.SetMiterLimit(2); canvas.AddTemplate(hooks, 300, 600); canvas.RestoreState(); canvas.SaveState(); canvas.SetMiterLimit(2.1f); canvas.AddTemplate(hooks, 300, 550); canvas.RestoreState(); } }
// =========================================================================== 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 canvas = writer.DirectContent; String text = "AWAY again"; BaseFont bf = BaseFont.CreateFont(); canvas.BeginText(); // line 1 canvas.SetFontAndSize(bf, 16); canvas.MoveText(36, 806); canvas.MoveTextWithLeading(0, -24); canvas.ShowText(text); // line 2 canvas.SetWordSpacing(20); canvas.NewlineShowText(text); // line 3 canvas.SetCharacterSpacing(10); canvas.NewlineShowText(text); canvas.SetWordSpacing(0); canvas.SetCharacterSpacing(0); // line 4 canvas.SetHorizontalScaling(50); canvas.NewlineShowText(text); canvas.SetHorizontalScaling(100); // line 5 canvas.NewlineShowText(text); canvas.SetTextRise(15); canvas.SetFontAndSize(bf, 12); canvas.SetColorFill(BaseColor.RED); canvas.ShowText("2"); canvas.SetColorFill(GrayColor.GRAYBLACK); // line 6 canvas.SetLeading(56); canvas.NewlineShowText("Changing the leading: " + text); canvas.SetLeading(24); canvas.NewlineText(); // line 7 PdfTextArray array = new PdfTextArray("A"); array.Add(120); array.Add("W"); array.Add(120); array.Add("A"); array.Add(95); array.Add("Y again"); canvas.ShowText(array); canvas.EndText(); canvas.SetColorFill(BaseColor.BLUE); canvas.BeginText(); canvas.SetTextMatrix(360, 770); canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); canvas.SetFontAndSize(bf, 24); canvas.ShowText(text); canvas.EndText(); canvas.BeginText(); canvas.SetTextMatrix(360, 730); canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE); canvas.SetFontAndSize(bf, 24); canvas.ShowText(text); canvas.EndText(); canvas.BeginText(); canvas.SetTextMatrix(360, 690); canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); canvas.SetFontAndSize(bf, 24); canvas.ShowText(text); canvas.EndText(); canvas.BeginText(); canvas.SetTextMatrix(360, 650); canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE); canvas.SetFontAndSize(bf, 24); canvas.ShowText(text); canvas.EndText(); PdfTemplate template = canvas.CreateTemplate(200, 36); template.SetLineWidth(2); for (int i = 0; i < 6; i++) { template.MoveTo(0, i * 6); template.LineTo(200, i * 6); } template.Stroke(); canvas.SaveState(); canvas.BeginText(); canvas.SetTextMatrix(360, 610); canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_CLIP); canvas.SetFontAndSize(bf, 24); canvas.ShowText(text); canvas.EndText(); canvas.AddTemplate(template, 360, 610); canvas.RestoreState(); canvas.SaveState(); canvas.BeginText(); canvas.SetTextMatrix(360, 570); canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE_CLIP); canvas.SetFontAndSize(bf, 24); canvas.ShowText(text); canvas.EndText(); canvas.AddTemplate(template, 360, 570); canvas.RestoreState(); canvas.SaveState(); canvas.BeginText(); canvas.SetTextMatrix(360, 530); canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE_CLIP); canvas.SetFontAndSize(bf, 24); canvas.ShowText(text); canvas.EndText(); canvas.AddTemplate(template, 360, 530); canvas.RestoreState(); canvas.SaveState(); canvas.BeginText(); canvas.SetTextMatrix(360, 490); canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_CLIP); canvas.SetFontAndSize(bf, 24); canvas.ShowText(text); canvas.EndText(); canvas.AddTemplate(template, 360, 490); canvas.RestoreState(); } }