/// <summary>Get the rectangle representation of the intersection between this rectangle and the passed rectangle /// </summary> /// <param name="rect">the rectangle to find the intersection with</param> /// <returns> /// the intersection rectangle if the passed rectangles intersects with this rectangle, /// a rectangle representing a line if the intersection is along an edge or /// a rectangle representing a point if the intersection is a single point, /// null otherwise /// </returns> public virtual iText.Kernel.Geom.Rectangle GetIntersection(iText.Kernel.Geom.Rectangle rect) { iText.Kernel.Geom.Rectangle result = null; //Calculate possible lower-left corner and upper-right corner float llx = Math.Max(x, rect.x); float lly = Math.Max(y, rect.y); float urx = Math.Min(GetRight(), rect.GetRight()); float ury = Math.Min(GetTop(), rect.GetTop()); //If width or height is non-negative, there is overlap and we can construct the intersection rectangle float width = urx - llx; float height = ury - lly; if (JavaUtil.FloatCompare(width, 0) >= 0 && JavaUtil.FloatCompare(height, 0) >= 0) { if (JavaUtil.FloatCompare(width, 0) < 0) { width = 0; } if (JavaUtil.FloatCompare(height, 0) < 0) { height = 0; } result = new iText.Kernel.Geom.Rectangle(llx, lly, width, height); } return(result); }
public void ManipulatePdf( string pdfSrc, string pdfDest, string text, string imageName, float fontSize, string fontName) { MemoryStream pdfStream = new(File.ReadAllBytes(pdfSrc)); PdfDocument pdfDoc = new(new PdfReader(pdfStream), new PdfWriter(pdfDest)); Document doc = new(pdfDoc); PdfFont font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(fontName)); Paragraph paragraph = new Paragraph(text).SetFont(font).SetFontSize(fontSize).SetFontColor(iText.Kernel.Colors.ColorConstants.CYAN); //DrawText(text, new Font(StandardFonts.HELVETICA, 14.0f), System.Drawing.Color.Aqua, 500, IMG + imageName); //ImageData img = ImageDataFactory.Create(IMG + imageName); //float w = img.GetWidth(); //float h = img.GetHeight(); float radio = 20.2f; PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.3f); // Implement transformation matrix usage in order to scale image for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) { PdfPage pdfPage = pdfDoc.GetPage(i); Rectangle pageSize = pdfPage.GetPageSize(); float x = (pageSize.GetLeft() + pageSize.GetRight()) / 2; float y = (pageSize.GetTop() + pageSize.GetBottom()) / 2; PdfCanvas over = new PdfCanvas(pdfPage); over.SaveState(); over.SetExtGState(gs1); //if (i % 2 == 1) //{ // doc.ShowTextAligned(paragraph, x, y-200, i, TextAlignment.CENTER, VerticalAlignment.TOP, 10); // doc.ShowTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, 10); // } // else //{ doc.ShowTextAligned(paragraph, x - 200, y + 160, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio); doc.ShowTextAligned(paragraph, x - 100, y + 80, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio); doc.ShowTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio); doc.ShowTextAligned(paragraph, x + 100, y - 80, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio); doc.ShowTextAligned(paragraph, x + 200, y - 160, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio); //over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 80), true); //over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 60), true); //over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 40), true); //over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 2), true); //} over.RestoreState(); } doc.Close(); }
public Task <byte[]> ManipulatePdf( StampSimple fileSource) { using MemoryStream pdfDest = new(); using MemoryStream pdfStream = new(fileSource.ImageSource); var pdfDoc = new PdfDocument(new PdfReader(pdfStream), new PdfWriter(pdfDest)); var doc = new Document(pdfDoc); foreach (TextProperties text in fileSource.Texts) { var font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(text.FontName)); iText.Kernel.Colors.Color fontColor = iText.Kernel.Colors.ColorConstants.CYAN; switch (text.Color) { case "WHITE": fontColor = iText.Kernel.Colors.ColorConstants.WHITE; break; case "BLACK": fontColor = iText.Kernel.Colors.ColorConstants.BLACK; break; case "BLUE": fontColor = iText.Kernel.Colors.ColorConstants.BLUE; break; case "DARKGRAY": fontColor = iText.Kernel.Colors.ColorConstants.DARK_GRAY; break; case "GRAY": fontColor = iText.Kernel.Colors.ColorConstants.GRAY; break; case "GREEN": fontColor = iText.Kernel.Colors.ColorConstants.GREEN; break; case "MAGENTA": fontColor = iText.Kernel.Colors.ColorConstants.MAGENTA; break; case "ORANGE": fontColor = iText.Kernel.Colors.ColorConstants.ORANGE; break; case "PINK": fontColor = iText.Kernel.Colors.ColorConstants.PINK; break; case "RED": fontColor = iText.Kernel.Colors.ColorConstants.RED; break; case "YELLOW": fontColor = iText.Kernel.Colors.ColorConstants.YELLOW; break; } var paragraph = new Paragraph(text.Text).SetFont(font).SetFontSize(text.FontSize).SetFontColor(fontColor); var gs1 = new PdfExtGState().SetFillOpacity(text.Opacity); for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) { PdfPage pdfPage = pdfDoc.GetPage(i); Rectangle pageSize = pdfPage.GetPageSize(); float x = (pageSize.GetLeft() + pageSize.GetRight()) / 2; float y = (pageSize.GetTop() + pageSize.GetBottom()) / 2; var over = new PdfCanvas(pdfPage); over.SaveState(); over.SetExtGState(gs1); doc.ShowTextAligned(paragraph, text.PosX, text.PosY, i, TextAlignment.CENTER, VerticalAlignment.TOP, text.Radio); over.RestoreState(); } } doc.Close(); return(Task.FromResult(pdfDest.ToArray())); }
/// <summary> /// Gets the rectangle as it looks on the rotated page /// and returns the rectangle in coordinates relevant to the true page origin. /// </summary> /// <remarks> /// Gets the rectangle as it looks on the rotated page /// and returns the rectangle in coordinates relevant to the true page origin. /// This rectangle can be used to add annotations, fields, and other objects /// to the rotated page. /// </remarks> /// <param name="rect">the rectangle as it looks on the rotated page.</param> /// <param name="page">the page on which one want to process the rectangle.</param> /// <returns>the newly created rectangle with translated coordinates.</returns> public static iText.Kernel.Geom.Rectangle GetRectangleOnRotatedPage(iText.Kernel.Geom.Rectangle rect, PdfPage page) { iText.Kernel.Geom.Rectangle resultRect = rect; int rotation = page.GetRotation(); if (0 != rotation) { iText.Kernel.Geom.Rectangle pageSize = page.GetPageSize(); switch ((rotation / 90) % 4) { case 1: { // 90 degrees resultRect = new iText.Kernel.Geom.Rectangle(pageSize.GetWidth() - resultRect.GetTop(), resultRect.GetLeft (), resultRect.GetHeight(), resultRect.GetWidth()); break; } case 2: { // 180 degrees resultRect = new iText.Kernel.Geom.Rectangle(pageSize.GetWidth() - resultRect.GetRight(), pageSize.GetHeight () - resultRect.GetTop(), resultRect.GetWidth(), resultRect.GetHeight()); break; } case 3: { // 270 degrees resultRect = new iText.Kernel.Geom.Rectangle(resultRect.GetLeft(), pageSize.GetHeight() - resultRect.GetRight (), resultRect.GetHeight(), resultRect.GetWidth()); break; } case 4: default: { // 0 degrees break; } } } return(resultRect); }