protected void ManipulatePdf(string dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); float x = 36; float y = 740; // Side of an equilateral triangle float side = 70; PdfShading.Axial axialShading = new PdfShading.Axial(new PdfDeviceCs.Rgb(), x, y, ColorConstants.PINK.GetColorValue(), x + side, y, ColorConstants.BLUE.GetColorValue()); PdfPattern.Shading shading = new PdfPattern.Shading(axialShading); PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage()); canvas.SetFillColorShading(shading); canvas.MoveTo(x, y); canvas.LineTo(x + side, y); canvas.LineTo(x + (side / 2), (float)(y + (side * Math.Sin(Math.PI / 3)))); canvas.ClosePathFillStroke(); pdfDoc.Close(); }
/// <summary> /// Builds the /// <see cref="iText.Kernel.Colors.Color"/> /// object representing the linear gradient with specified configuration /// that fills the target bounding box. /// </summary> /// <param name="targetBoundingBox">the bounding box to be filled in current space</param> /// <param name="contextTransform"> /// the transformation from the base coordinates space into /// the current space. The /// <see langword="null"/> /// value is valid and can be used /// if there is no transformation from base coordinates to current space /// specified, or it is equal to identity transformation. /// </param> /// <param name="document"> /// the /// <see cref="iText.Kernel.Pdf.PdfDocument"/> /// for which the linear gradient would be built. /// </param> /// <returns> /// the constructed /// <see cref="iText.Kernel.Colors.Color"/> /// or /// <see langword="null"/> /// if no color to be applied /// or base gradient vector has been specified /// </returns> public virtual Color BuildColor(Rectangle targetBoundingBox, AffineTransform contextTransform, PdfDocument document) { // TODO: DEVSIX-4136 the document argument would be required for opaque gradients (as we would need to create a mask form xObject) Point[] baseCoordinatesVector = GetGradientVector(targetBoundingBox, contextTransform); if (baseCoordinatesVector == null || this.stops.IsEmpty()) { // Can not create gradient color with 0 stops or null coordinates vector return(null); } // evaluate actual coordinates and transformation AffineTransform shadingTransform = new AffineTransform(); if (contextTransform != null) { shadingTransform.Concatenate(contextTransform); } AffineTransform gradientTransformation = GetCurrentSpaceToGradientVectorSpaceTransformation(targetBoundingBox , contextTransform); if (gradientTransformation != null) { try { if (targetBoundingBox != null) { targetBoundingBox = Rectangle.CalculateBBox(JavaUtil.ArraysAsList(gradientTransformation.InverseTransform( new Point(targetBoundingBox.GetLeft(), targetBoundingBox.GetBottom()), null), gradientTransformation.InverseTransform (new Point(targetBoundingBox.GetLeft(), targetBoundingBox.GetTop()), null), gradientTransformation.InverseTransform (new Point(targetBoundingBox.GetRight(), targetBoundingBox.GetBottom()), null), gradientTransformation .InverseTransform(new Point(targetBoundingBox.GetRight(), targetBoundingBox.GetTop()), null))); } shadingTransform.Concatenate(gradientTransformation); } catch (NoninvertibleTransformException) { LogManager.GetLogger(GetType()).Error(iText.IO.LogMessageConstant.UNABLE_TO_INVERT_GRADIENT_TRANSFORMATION ); } } PdfShading.Axial axial = CreateAxialShading(baseCoordinatesVector, this.stops, this.spreadMethod, targetBoundingBox ); if (axial == null) { return(null); } PdfPattern.Shading shading = new PdfPattern.Shading(axial); if (!shadingTransform.IsIdentity()) { double[] matrix = new double[6]; shadingTransform.GetMatrix(matrix); shading.SetMatrix(new PdfArray(matrix)); } return(new PatternColor(shading)); }
protected void ManipulatePdf(String dest) { PageSize pageSize = new PageSize(150, 300); PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); pdfDoc.SetDefaultPageSize(pageSize); PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage()); PdfShading.Axial axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), 0, pageSize.GetHeight(), ColorConstants.WHITE.GetColorValue(), 0, 0, ColorConstants.GREEN.GetColorValue()); PdfPattern.Shading pattern = new PdfPattern.Shading(axial); canvas.SetFillColorShading(pattern); canvas.Rectangle(0, 0, pageSize.GetWidth(), pageSize.GetHeight()); canvas.Fill(); pdfDoc.Close(); }
public virtual void PatternColorColoredAxialPatternTest() { String name = "patternColorColoredAxialPatternTest.pdf"; PdfWriter writer = new PdfWriter(destinationFolder + name); writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION); PdfDocument document = new PdfDocument(writer); PdfPage page = document.AddNewPage(); PdfCanvas canvas = new PdfCanvas(page); PdfShading axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), 36, 716, new float[] { 1, .784f, 0 }, 396, 788, new float[] { 0, 0, 1 }, new bool[] { true, true }); canvas.SetFillColor(new PatternColor(new PdfPattern.Shading(axial))); canvas.Rectangle(30, 300, 400, 400).Fill(); canvas.Release(); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder)); }
public void HandleEvent(Event evt) { // Retrieve document | 获取 PDF 文档 PdfDocumentEvent docEvent = (PdfDocumentEvent)evt; PdfDocument pdf = docEvent.GetDocument(); PdfPage page = docEvent.GetPage(); int pdfTotalPageNumber = pdf.GetNumberOfPages(); int currentPageNumber = pdf.GetPageNumber(page); // Do not add footer on cover and back cover | 封面和封底不加 //if (currentPageNumber == 1 || currentPageNumber == pdfTotalPageNumber) //{ // return; //} // Width of text in footer | 文字宽度 float textWidth = pdfFont.GetWidth(footer["text"].ToString(), Convert.ToSingle(footer["fontSize"])); // Color of text | 文字颜色 int[] rgb = footer["fontColor"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x.Trim())).ToArray(); //// Doc parameters 文档参数 Rectangle pageSize = page.GetPageSize(); PdfCanvas pdfCanvas = new PdfCanvas( page.GetLastContentStream(), page.GetResources(), pdf); Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize); canvas.SetFontSize(Convert.ToSingle(footer["fontSize"])) .SetFont(pdfFont) .SetFontColor(new DeviceRgb(rgb[0], rgb[1], rgb[2])); // Footer text/page NO. and position | 文字/页码及位置 string footerText = footer["text"].ToString(); string footerPageNum = (currentPageNumber - 1 < 10 ? "0" : "") + (currentPageNumber - 1); float[] footerTextPos = new float[2]; float[] footerPageNumPos = new float[2]; float[] roundRectPos = new float[2]; TextAlignment alignment = TextAlignment.CENTER; // Draw gradient background | 渐变背景 PdfShading.Axial axial; if (currentPageNumber % 2 == 0) { // Align left in even number page, which page NO. is a odd | 偶数页左对齐(页码为奇数) footerTextPos[0] = Convert.ToSingle(footer["left"]); footerTextPos[1] = pageSize.GetBottom() + 19.7f; footerPageNumPos[0] = textWidth + Convert.ToSingle(footer["left"]) + 9.5f; footerPageNumPos[1] = pageSize.GetBottom() + 21.6f; roundRectPos[0] = textWidth + Convert.ToSingle(footer["left"]) + 4.0f; roundRectPos[1] = pageSize.GetBottom() + 22.0f; alignment = TextAlignment.LEFT; axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), 0, pageSize.GetHeight() - 30, DeviceRgb.WHITE.GetColorValue(), pageSize.GetWidth(), pageSize.GetHeight() - 30, new DeviceRgb(190, 230, 255).GetColorValue()); } else { // Align right in odd number page, which page NO. is a even |奇数页右对齐(页码为偶数) footerTextPos[0] = pageSize.GetWidth() - Convert.ToSingle(footer["left"]) - 24.0f; footerTextPos[1] = pageSize.GetBottom() + 19.7f; footerPageNumPos[0] = pageSize.GetWidth() - Convert.ToSingle(footer["left"]) - 5.5f; footerPageNumPos[1] = pageSize.GetBottom() + 21.6f; roundRectPos[0] = pageSize.GetWidth() - Convert.ToSingle(footer["left"]) - 20.0f; roundRectPos[1] = pageSize.GetBottom() + 22.0f; alignment = TextAlignment.RIGHT; axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), 0, pageSize.GetHeight() - 30, new DeviceRgb(190, 230, 255).GetColorValue(), pageSize.GetWidth(), pageSize.GetHeight() - 30, DeviceRgb.WHITE .GetColorValue()); } // Draw gradient background and round corner rect | 渐变背景和圆角矩形 PdfPattern.Shading pattern = new PdfPattern.Shading(axial); pdfCanvas.SetFillColorShading(pattern) .Rectangle(0, pageSize.GetBottom() + 14.0f, pageSize.GetWidth(), 24.0f) .Fill() .RoundRectangle(roundRectPos[0], roundRectPos[1], 20.0f, 9.0f, 2.0f) .SetFillColor(new DeviceRgb(18, 98, 170)) .Fill(); // Draw text canvas.ShowTextAligned(footerText, footerTextPos[0], footerTextPos[1], alignment); Canvas canvas2 = new Canvas(pdfCanvas, pdf, pageSize); canvas2.SetFontSize(7.2f); canvas2.SetFont(pdfFont); canvas2.SetFontColor(DeviceRgb.WHITE); // Draw page NO. canvas2.ShowTextAligned(footerPageNum, footerPageNumPos[0], footerPageNumPos[1], alignment); }