Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        private static Color?GetColor(PdfPattern.Shading shading, float alpha)
        {
            var shadingDict        = shading.GetShading();
            var shadingConstructed = PdfShading.MakeShading(shadingDict);

            switch (shadingConstructed)
            {
            case PdfShading.Axial axial:
                var axialFunction = (PdfDictionary)axial.GetFunction();
                GetFunctionDetails(axialFunction);
                var coords = GetItemsFloat(axial.GetCoords());
                var domain = GetItemsFloat(axial.GetDomain());
                var extend = GetItemsBool(axial.GetExtend());

                break;

            case PdfShading.CoonsPatchMesh coonsPatchMesh:
                break;

            case PdfShading.FreeFormGouraudShadedTriangleMesh freeFormGouraudShadedTriangleMesh: break;

            case PdfShading.FunctionBased functionBased: break;

            case PdfShading.LatticeFormGouraudShadedTriangleMesh latticeFormGouraudShadedTriangleMesh:
                break;

            case PdfShading.Radial radial: break;

            case PdfShading.TensorProductPatchMesh tensorProductPatchMesh: break;
            }

            return(null);
        }
        private void CheckInShadingPattern(PdfObject @object)
        {
            String newContentString = PdfACheckerTestUtils.GetStreamWithValue(@object);

            byte[]     newContent = newContentString.GetBytes(System.Text.Encoding.UTF8);
            PdfStream  stream     = new PdfStream(newContent);
            PdfPattern pattern    = new PdfPattern.Shading(stream);

            pdfA1Checker.CheckPdfObject(pattern.GetPdfObject());
        }
Exemplo n.º 4
0
        /// <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));
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
        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);
        }