Browse Source

Rework getRectangleOnRotatedPage method logic and add test

DEVSIX-8877

Autoported commit.
Original commit hash: [c072e4ccd]
pull/38/head
Fedorov Alexandr 2 months ago
committed by iText Software
parent
commit
445a3b3081
  1. 51
      itext.tests/itext.forms.tests/itext/forms/RectangleIntegrationTest.cs
  2. 5
      itext.tests/itext.forms.tests/resources/itext/forms/RectangleTest/cmp_RectangleTest.pdf
  3. 9
      itext.tests/itext.kernel.tests/itext/kernel/geom/RectangleTest.cs
  4. 56
      itext/itext.kernel/itext/kernel/geom/Rectangle.cs
  5. 2
      port-hash

51
itext.tests/itext.forms.tests/itext/forms/RectangleIntegrationTest.cs

@ -0,0 +1,51 @@
using System;
using iText.Forms.Fields;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using iText.Test;
namespace iText.Forms {
[NUnit.Framework.Category("IntegrationTest")]
public class RectangleIntegrationTest {
private static readonly String DESTINATION_FOLDER = TestUtil.GetOutputPath() + "/forms/RectangleTest/";
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/forms/RectangleTest/";
[NUnit.Framework.OneTimeSetUp]
public static void InitDestinationFolder() {
ITextTest.CreateOrClearDestinationFolder(DESTINATION_FOLDER);
}
[NUnit.Framework.Test]
public virtual void CreatePdfWithSignatureFields() {
String outPdf = DESTINATION_FOLDER + "RectangleTest.pdf";
String cmpPdf = SOURCE_FOLDER + "cmp_RectangleTest.pdf";
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + "RectangleTest.pdf");
PdfDocument pdfDoc = new PdfDocument(writer);
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
for (int i = 0; i <= 3; i++) {
int rotation = 90 * i;
PdfPage page = pdfDoc.AddNewPage();
page.SetRotation(rotation);
float x = 20;
float y = 500;
float width = 100;
float height = 50;
float spacing = 50;
for (int j = 1; j <= 3; j++) {
Rectangle rect = new Rectangle(x, y, width, height);
String fieldName = "page" + i + "_Signature" + j;
PdfFormField signatureField = new SignatureFormFieldBuilder(pdfDoc, fieldName).SetPage(page).SetWidgetRectangle
(rect).CreateSignature();
form.AddField(signatureField);
x += width + spacing;
}
}
pdfDoc.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_"
));
}
}
}

5
itext.tests/itext.forms.tests/resources/itext/forms/RectangleTest/cmp_RectangleTest.pdf

@ -0,0 +1,5 @@
SIGN HERE SIGN HERE SIGN HERE
SIGN HERE SIGN HERE SIGN HERE
SIGN HERE SIGN HERE SIGN HERE
SIGN HERE SIGN HERE SIGN HERE

9
itext.tests/itext.kernel.tests/itext/kernel/geom/RectangleTest.cs

@ -443,6 +443,9 @@ namespace iText.Kernel.Geom {
NUnit.Framework.Assert.IsTrue(PageSize.A4.EqualsWithEpsilon(page.GetPageSize()));
// Test rectangle
Rectangle testRectangle = new Rectangle(200, 200, 100, 200);
Rectangle testRectangle2 = new Rectangle(20, 500, 100, 50);
Rectangle testRectangle3 = new Rectangle(170, 500, 100, 50);
Rectangle testRectangle4 = new Rectangle(320, 500, 100, 50);
NUnit.Framework.Assert.AreEqual(0, page.GetRotation());
NUnit.Framework.Assert.IsTrue(new Rectangle(200, 200, 100, 200).EqualsWithEpsilon(Rectangle.GetRectangleOnRotatedPage
(testRectangle, page)));
@ -458,6 +461,12 @@ namespace iText.Kernel.Geom {
NUnit.Framework.Assert.AreEqual(270, page.GetRotation());
NUnit.Framework.Assert.IsTrue(new Rectangle(200, 542, 200, 100).EqualsWithEpsilon(Rectangle.GetRectangleOnRotatedPage
(testRectangle, page)));
NUnit.Framework.Assert.IsTrue(new Rectangle(500, 722, 50, 100).EqualsWithEpsilon(Rectangle.GetRectangleOnRotatedPage
(testRectangle2, page)));
NUnit.Framework.Assert.IsTrue(new Rectangle(500, 572, 50, 100).EqualsWithEpsilon(Rectangle.GetRectangleOnRotatedPage
(testRectangle3, page)));
NUnit.Framework.Assert.IsTrue(new Rectangle(500, 422, 50, 100).EqualsWithEpsilon(Rectangle.GetRectangleOnRotatedPage
(testRectangle4, page)));
page.SetRotation(360);
NUnit.Framework.Assert.AreEqual(0, page.GetRotation());
NUnit.Framework.Assert.IsTrue(new Rectangle(200, 200, 100, 200).EqualsWithEpsilon(Rectangle.GetRectangleOnRotatedPage

56
itext/itext.kernel/itext/kernel/geom/Rectangle.cs

@ -104,54 +104,70 @@ namespace iText.Kernel.Geom {
}
/// <summary>
/// Gets the rectangle as it looks on the rotated page
/// and returns the rectangle in coordinates relevant to the true page origin.
/// Transforms a rectangle defined in the space of unrotated origin (bottom-left)
/// into coordinates as it would appear on a passed as parameter rotated page.
/// </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.
/// Transforms a rectangle defined in the space of unrotated origin (bottom-left)
/// into coordinates as it would appear on a passed as parameter rotated page.
/// <para />
/// This method is useful when adding annotations, form fields, or other elements
/// to a PDF page that has a rotation. The iText coordinate system always
/// uses the bottom-left corner as origin, regardless of page rotation.
/// This method compensates for that rotation, returning a rectangle positioned
/// correctly in the true page coordinate space.
/// </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>
/// <param name="rect">the rectangle defined in page-space coordinates relative to the unrotated origin.</param>
/// <param name="page">
/// the
/// <see cref="iText.Kernel.Pdf.PdfPage"/>
/// to which the rectangle will be added.
/// The rotation of this page is used to transform the coordinates.
/// </param>
/// <returns>
/// a new
/// <see cref="Rectangle"/>
/// with corrected coordinates suitable for placement
/// in the rotated coordinate space of the page.
/// </returns>
public static iText.Kernel.Geom.Rectangle GetRectangleOnRotatedPage(iText.Kernel.Geom.Rectangle rect, PdfPage
page) {
iText.Kernel.Geom.Rectangle resultRect = rect;
iText.Kernel.Geom.Rectangle rectangleOnRotatedPage = rect;
int rotation = page.GetRotation();
if (0 != rotation) {
if (rotation != 0) {
iText.Kernel.Geom.Rectangle pageSize = page.GetPageSize();
float x = rect.GetLeft();
float y = rect.GetBottom();
float width = rect.GetWidth();
float height = rect.GetHeight();
switch ((rotation / 90) % 4) {
case 1: {
// 90 degrees
resultRect = new iText.Kernel.Geom.Rectangle(pageSize.GetWidth() - resultRect.GetTop(), resultRect.GetLeft
(), resultRect.GetHeight(), resultRect.GetWidth());
rectangleOnRotatedPage = new iText.Kernel.Geom.Rectangle(pageSize.GetWidth() - y - height, x, height, width
);
break;
}
case 2: {
// 180 degrees
resultRect = new iText.Kernel.Geom.Rectangle(pageSize.GetWidth() - resultRect.GetRight(), pageSize.GetHeight
() - resultRect.GetTop(), resultRect.GetWidth(), resultRect.GetHeight());
rectangleOnRotatedPage = new iText.Kernel.Geom.Rectangle(pageSize.GetWidth() - x - width, pageSize.GetHeight
() - y - height, width, height);
break;
}
case 3: {
// 270 degrees
resultRect = new iText.Kernel.Geom.Rectangle(resultRect.GetLeft(), pageSize.GetHeight() - resultRect.GetRight
(), resultRect.GetHeight(), resultRect.GetWidth());
rectangleOnRotatedPage = new iText.Kernel.Geom.Rectangle(y, pageSize.GetHeight() - x - width, height, width
);
break;
}
case 4:
default: {
// 0 degrees
break;
}
}
}
return resultRect;
return rectangleOnRotatedPage;
}
/// <summary>Calculates the bounding box of passed points.</summary>

2
port-hash

@ -1 +1 @@
5d8f7167958507348d480fcc31edf338d74f9844
c072e4ccd471ed3f9820368e1047e989cba4e1c8
Loading…
Cancel
Save