/// <summary>Calculates the common rectangle which includes all the input rectangles.</summary> /// <param name="rectangles">list of input rectangles.</param> /// <returns>common rectangle.</returns> public static iText.Kernel.Geom.Rectangle GetCommonRectangle(params iText.Kernel.Geom.Rectangle[] rectangles ) { float ury = -float.MaxValue; float llx = float.MaxValue; float lly = float.MaxValue; float urx = -float.MaxValue; foreach (iText.Kernel.Geom.Rectangle rectangle in rectangles) { if (rectangle == null) { continue; } iText.Kernel.Geom.Rectangle rec = rectangle.Clone(); if (rec.GetY() < lly) { lly = rec.GetY(); } if (rec.GetX() < llx) { llx = rec.GetX(); } if (rec.GetY() + rec.GetHeight() > ury) { ury = rec.GetY() + rec.GetHeight(); } if (rec.GetX() + rec.GetWidth() > urx) { urx = rec.GetX() + rec.GetWidth(); } } return(new iText.Kernel.Geom.Rectangle(llx, lly, urx - llx, ury - lly)); }
/// <summary>Check if this rectangle and the passed rectangle overlap</summary> /// <param name="rect">a rectangle which is to be checked if it overlaps the passed rectangle</param> /// <param name="epsilon"> /// if greater than zero, then this is the maximum distance that one rectangle can go to another, but /// they will not overlap, if less than zero, then this is the minimum required distance between the /// rectangles so that they do not overlap /// </param> /// <returns>true if there is overlap of some kind</returns> public virtual bool Overlaps(iText.Kernel.Geom.Rectangle rect, float epsilon) { // Two rectangles do not overlap if any of the following holds // The first rectangle lies to the left of the second rectangle or touches very slightly if ((this.GetX() + this.GetWidth()) < (rect.GetX() + epsilon)) { return(false); } // The first rectangle lies to the right of the second rectangle or touches very slightly if ((this.GetX() + epsilon) > (rect.GetX() + rect.GetWidth())) { return(false); } // The first rectangle lies to the bottom of the second rectangle or touches very slightly if ((this.GetY() + this.GetHeight()) < (rect.GetY() + epsilon)) { return(false); } // The first rectangle lies to the top of the second rectangle or touches very slightly if ((this.GetY() + epsilon) > (rect.GetY() + rect.GetHeight())) { return(false); } return(true); }
/// <summary>Check if this rectangle and the passed rectangle overlap</summary> /// <param name="rect"/> /// <returns>true if there is overlap of some kind</returns> public virtual bool Overlaps(iText.Kernel.Geom.Rectangle rect) { // Two rectangles do not overlap if any of the following holds // 1. the lower left corner of the second rectangle is to the right of the upper-right corner of the first. return(!(this.GetX() + this.GetWidth() < rect.GetX() || // 2. the lower left corner of the second rectangle is above the upper right corner of the first. this.GetY() + this.GetHeight() < rect.GetY() || // 3. the upper right corner of the second rectangle is to the left of the lower-left corner of the first. this.GetX() > rect.GetX() + rect.GetWidth() || // 4. the upper right corner of the second rectangle is below the lower left corner of the first. this.GetY() > rect.GetY() + rect.GetHeight())); }
private Rectangle GetUpperOrLowerAnnotationRect(Rectangle buttonRect, PdfFont font, IAnnotation annotation, Pdf.IWord target, bool upper = true) { var contentBounds = GetContentBounds(annotation.Content, target.Parent.Width / 2, font); var targetWidthHalf = buttonRect.GetWidth() / 2; var helpWidthHalf = contentBounds.width / 2; var annotHeight = contentBounds.height + 6; float annotY; if (upper) { annotY = buttonRect.GetY() + buttonRect.GetHeight() + 5; } else { annotY = buttonRect.GetY() - annotHeight - 10; } var helpRect = new Rectangle(buttonRect.GetX() + targetWidthHalf - helpWidthHalf - 5, annotY, contentBounds.width + 10, annotHeight); return(helpRect); }
/// <summary>Check if this rectangle contains the passed rectangle.</summary> /// <remarks> /// Check if this rectangle contains the passed rectangle. /// A rectangle will envelop itself, meaning that for any rectangle /// <paramref name="rect"/> /// the expression /// <c>rect.contains(rect)</c> /// always returns true. /// </remarks> /// <param name="rect">a rectangle which is to be checked if it is fully contained inside this rectangle.</param> /// <returns>true if this rectangle contains the passed rectangle, false otherwise.</returns> public virtual bool Contains(iText.Kernel.Geom.Rectangle rect) { float llx = this.GetX(); float lly = this.GetY(); float urx = llx + this.GetWidth(); float ury = lly + this.GetHeight(); float rllx = rect.GetX(); float rlly = rect.GetY(); float rurx = rllx + rect.GetWidth(); float rury = rlly + rect.GetHeight(); return(llx - EPS <= rllx && lly - EPS <= rlly && rurx <= urx + EPS && rury <= ury + EPS); }
public void Draw(PdfCanvas canvas, iText.Kernel.Geom.Rectangle drawArea) { float coordY = drawArea.GetY() + lineWidth / 2 + offset; canvas .SaveState() .SetStrokeColor(color) .SetLineWidth(lineWidth) .MoveTo(drawArea.GetX(), coordY) .LineTo(drawArea.GetX() + drawArea.GetWidth(), coordY) .Stroke() .RestoreState(); }
/// <summary> /// Creates the copy of given /// <see cref="Rectangle"/> /// </summary> /// <param name="rect"> /// the copied /// <see cref="Rectangle"/> /// </param> public Rectangle(iText.Kernel.Geom.Rectangle rect) : this(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight()) { }
/// <summary>Check if this rectangle and the passed rectangle overlap</summary> /// <param name="rect"/> /// <returns>true if there is overlap of some kind</returns> public virtual bool Overlaps(iText.Kernel.Geom.Rectangle rect) { // Two rectangles do not overlap if any of the following holds return(!(this.GetX() + this.GetWidth() < rect.GetX() || this.GetY() + this.GetHeight() < rect.GetY() || this .GetX() > rect.GetX() + rect.GetWidth() || this.GetY() > rect.GetY() + rect.GetHeight())); }
/// <summary>Appends a rectangle to the current path as a complete subpath.</summary> public virtual void Rectangle(iText.Kernel.Geom.Rectangle rect) { Rectangle(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight()); }
public Task WriteAnnotatedPdfAsync(string pdfDocumentPath, IEnumerable <IAnnotation> annotations, string filePath) { using (var reader = new PdfReader(pdfDocumentPath)) using (var outFile = File.Open(filePath, FileMode.Create)) using (var writer = new PdfWriter(outFile)) using (var doc = new PdfDocument(reader, writer)) { var acroForm = PdfAcroForm.GetAcroForm(doc, true); foreach (var ann in annotations) { var targets = ann.SelectedTargets; if (targets == null || targets.Count < 1) { targets = ann.Subject.Appearances ?? throw new ArgumentException($"The subject word {ann.Subject.Text} for a given annotation has no appearances."); } foreach (var trg in targets) { var page = doc.GetPage(trg.Parent.Index + 1); var font = PdfFontFactory.CreateRegisteredFont(iText.IO.Font.Constants.StandardFonts.HELVETICA); var coord = trg.GetPdfCoords(); var buttonRect = new Rectangle(coord.Llx, coord.Lly, coord.width, coord.height); var txRect = GetAnnotationRect(buttonRect, font, ann, trg); var textFieldName = Guid.NewGuid().ToString("n"); var textField = PdfFormField.CreateText(doc, txRect, textFieldName); textField.SetValue(ann.Content, font, FontSize); textField.SetColor(ColorConstants.DARK_GRAY); textField.SetBackgroundColor(ColorConstants.LIGHT_GRAY); textField.SetReadOnly(true); textField.SetMultiline(true); textField.SetVisibility(PdfFormField.HIDDEN); textField.SetBorderColor(ColorConstants.LIGHT_GRAY); textField.SetFieldFlags(4097); acroForm.AddField(textField, page); var enter = PdfAction.CreateHide(textFieldName, false); var exit = PdfAction.CreateHide(textFieldName, true); var btn = PdfFormField.CreatePushButton(doc, buttonRect, Guid.NewGuid().ToString("n"), string.Empty); btn.SetBackgroundColor(null); btn.SetBorderWidth(0); btn.SetAdditionalAction(PdfName.E, enter); btn.SetAdditionalAction(PdfName.X, exit); acroForm.AddField(btn, page); var underline = PdfTextMarkupAnnotation.CreateUnderline(new Rectangle(buttonRect.GetX(), buttonRect.GetY() - 2, buttonRect.GetWidth(), 3), new float[] { buttonRect.GetX() + buttonRect.GetWidth(), buttonRect.GetY() + buttonRect.GetHeight(), buttonRect.GetX(), buttonRect.GetY() + buttonRect.GetHeight(), buttonRect.GetX() + buttonRect.GetWidth(), buttonRect.GetY() - 2, buttonRect.GetX(), buttonRect.GetY() - 2, }); underline.SetColor(ColorConstants.YELLOW); page.AddAnnotation(underline); } } } return(Task.CompletedTask); }