How to get ContentCreator Rect as points?

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
whoit
User
Posts: 269
Joined: Tue Jul 07, 2015 3:30 pm

How to get ContentCreator Rect as points?

Post by whoit »

Hi -

I'm using ShowTextBlock from ContentCreator.

After I add text, RotateCS, and then TranslateCS, I'd like to determine if any part of the resulting rectangle is off the page.
I'd like to check the 4 corner points of the rect against the page to see if any are outside the left, top, right, or bottom values.

How can I do this?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get ContentCreator Rect as points?

Post by Sasha - Tracker Dev Team »

Hello Wayne,

This is a very wrong way of doing this. The correct matrix should be specified BEFORE you add the text so that the text would be placed in the needed position.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
whoit
User
Posts: 269
Joined: Tue Jul 07, 2015 3:30 pm

Re: How to get ContentCreator Rect as points?

Post by whoit »

Perhaps I haven't been clear with my goal...

I'm trying to position text angled across the face of a document (lower-left to upper-right, for example), as large as possible -
what would be considered a typical watermark.

If not, then I need to set a textblock, font size, angle, and position, then determine
if it is either too large or too small, make changes, test again....
I can easily get the angle and the max length, but the font size will vary based on the
length of the text and therefore the need for repetition/testing....

Is there an API to do this without my having to calculate everything?
whoit
User
Posts: 269
Joined: Tue Jul 07, 2015 3:30 pm

Re: How to get ContentCreator Rect as points?

Post by whoit »

OK, how about this then:

How can I get the corner points of ShowTextBlock
AFTER using RotateCS?

Thanks,
Wayne
User avatar
Paul - Tracker Supp
Site Admin
Posts: 6831
Joined: Wed Mar 25, 2009 10:37 pm
Location: Chemainus, Canada
Contact:

Re: How to get ContentCreator Rect as points?

Post by Paul - Tracker Supp »

Hi Wayne,

Alex was unable to get to our post today, you should hear from him tomorrow.

hth
Best regards

Paul O'Rorke
Tracker Support North America
http://www.tracker-software.com
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get ContentCreator Rect as points?

Post by Sasha - Tracker Dev Team »

Hello Wayne,

Here's a sample that you can try in the FullDemo application:

Code: Select all

private void placeRotatedWatermarkToolStripMenuItem_Click(object sender, EventArgs e)
{
	PDFXEdit.IPXC_Document doc = pxcInst.NewDocument();

	PDFXEdit.PXC_Rect rcMedia;
	rcMedia.left = 0;
	rcMedia.bottom = 0;
	rcMedia.top = 800;
	rcMedia.right = 600;
	PDFXEdit.IPXC_UndoRedoData urData;
	PDFXEdit.IPXC_Page page = doc.Pages.InsertPage(0, ref rcMedia, out urData);

	PXC_Matrix mRot = new PXC_Matrix();
	auxInst.MathHelper.Matrix_Reset(ref mRot);
	mRot = auxInst.MathHelper.Matrix_Rotate(ref mRot, -page.Rotation);
	auxInst.MathHelper.Rect_Transform(ref mRot, ref rcMedia);
	var dX = rcMedia.left;
	var dY = rcMedia.bottom;
	auxInst.MathHelper.Matrix_Translate(ref mRot, -dX, -dY);
	rcMedia.left -= dX;
	rcMedia.right -= dX;
	rcMedia.top -= dY;
	rcMedia.bottom -= dY;

	PXC_Rect rcContentRect;
	PDFXEdit.IPXC_ContentCreator CC = doc.CreateContentCreator();
	CC.ShowTextBlock("Test Text", ref rcMedia, ref rcMedia, (uint)PXC_DrawTextFlags.DTF_CalcSizeOnly, -1, null, null, null, out rcContentRect);


	PXC_Rect rcS = rcMedia;
	double dRotation = 45.0;
	PXC_Rect rcR = rcContentRect;
	PXC_Matrix m = new PXC_Matrix();
	auxInst.MathHelper.Matrix_Reset(ref m);
	m = auxInst.MathHelper.Matrix_Rotate(ref m, dRotation);
	auxInst.MathHelper.Rect_Transform(ref m, ref rcR);
	PXC_Rect rcRes;
	//Fit rect to rect
	{
		var k1 = (rcR.right - rcR.left) / Math.Abs(rcR.top - rcR.bottom);
		var k2 = (rcS.right - rcS.left) / Math.Abs(rcS.top - rcS.bottom);
				
		if (k1 >= k2)
		{
			var h = (rcS.right - rcS.left) / k1;
			rcRes = rcS;
			rcRes.bottom += (Math.Abs(rcRes.top - rcRes.bottom) - h) / 2;
			rcRes.top = rcRes.bottom + h;
		}
		else
		{
			var w = (rcS.right - rcS.left) * k1;
			rcRes = rcS;
			rcRes.left += ((rcRes.right - rcRes.left) - w) / 2;
			rcRes.right = rcRes.left + w;
		}
	}
	PXC_Matrix mm = new PXC_Matrix();
	auxInst.MathHelper.Matrix_Reset(ref mm);
	mm = auxInst.MathHelper.Matrix_RectToRect(rcR, rcRes);
	PXC_Matrix mResMatrix = auxInst.MathHelper.Matrix_Multiply(ref m, ref mm);
	mRot = auxInst.MathHelper.Matrix_Invert(ref mRot);
	mResMatrix = auxInst.MathHelper.Matrix_Multiply(ref mResMatrix, ref mRot);
	CC.ConcatCS(mResMatrix);
	CC.ShowTextBlock("Test Text", ref rcMedia, ref rcMedia, 0, -1, null, null, null, out rcContentRect);

	page.PlaceContent(CC.Detach(), (UInt32)PDFXEdit.PXC_PlaceContentFlags.PlaceContent_After);

	pdfCtl.OpenDocFrom(doc);
}
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
whoit
User
Posts: 269
Joined: Tue Jul 07, 2015 3:30 pm

Re: How to get ContentCreator Rect as points?

Post by whoit »

Thanks Alex....I'll dig into the code this week....
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17823
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: How to get ContentCreator Rect as points?

Post by Tracker Supp-Stefan »

:D
whoit
User
Posts: 269
Joined: Tue Jul 07, 2015 3:30 pm

Re: How to get ContentCreator Rect as points?

Post by whoit »

OK, I finally tested the sample code from earlier in this thread.
It works, but I have two different problems now:

1) It distorts (shears) the text based on the rotation angle.
That is, if the text rotation angle is 0, 90, 180, 270 then everything is fine.
However, if the angle is something like 37, then the text gets sheared.

Here's a sample of the output at 37 degrees:
http://www.coastallogic.com/public/wayn ... fWEHTR.pdf

2) If the PDF pages are rotated (internally within the PDF) then the text is completely off the page, unlike within your Editor
(screenshot: http://www.coastallogic.com/public/wayn ... rkSnap.png )

Here's my code - I had to convert yours since we do not use the Editor SDK but other than SDK calls, it should be exactly the same:

Code: Select all

        private void placeRotatedWatermark(string wmText)
        {
            double pageHeight, pageWidth;
            currentPage.GetDimension(out pageWidth, out pageHeight);

            PXC_Rect rcMedia;
            rcMedia.left = 0;
            rcMedia.bottom = 0;
            rcMedia.top = pageHeight;
            rcMedia.right = pageWidth;

            PXC_Matrix mRot = new PXC_Matrix();
            auxInst.MathHelper.Matrix_Reset(ref mRot);
            mRot = auxInst.MathHelper.Matrix_Rotate(ref mRot, -currentPage.Rotation);
            auxInst.MathHelper.Rect_Transform(ref mRot, ref rcMedia);
            var dX = rcMedia.left;
            var dY = rcMedia.bottom;
            auxInst.MathHelper.Matrix_Translate(ref mRot, -dX, -dY);
            rcMedia.left -= dX;
            rcMedia.right -= dX;
            rcMedia.top -= dY;
            rcMedia.bottom -= dY;

            PXC_Rect rcContentRect;
            IPXC_ContentCreator CC = pDoc.CreateContentCreator();

            uint nFlags = (uint)PXC_DrawTextFlags.DTF_RichText; //default is 0
            nFlags = nFlags | (uint)PXC_DrawTextFlags.DTF_CalcSizeOnly;
            CC.ShowTextBlock(wmText, ref rcMedia, ref rcMedia, nFlags, -1, null, null, null, out rcContentRect);

            PXC_Rect rcS = rcMedia;
            double dRotation = 45.0;
            PXC_Rect rcR = rcContentRect;
            PXC_Matrix m = new PXC_Matrix();
            auxInst.MathHelper.Matrix_Reset(ref m);
            m = auxInst.MathHelper.Matrix_Rotate(ref m, dRotation);
            auxInst.MathHelper.Rect_Transform(ref m, ref rcR);
            PXC_Rect rcRes;
            //Fit rect to rect
            {
                var k1 = (rcR.right - rcR.left) / Math.Abs(rcR.top - rcR.bottom);
                var k2 = (rcS.right - rcS.left) / Math.Abs(rcS.top - rcS.bottom);

                if (k1 >= k2)
                {
                    var h = (rcS.right - rcS.left) / k1;
                    rcRes = rcS;
                    rcRes.bottom += (Math.Abs(rcRes.top - rcRes.bottom) - h) / 2;
                    rcRes.top = rcRes.bottom + h;
                }
                else
                {
                    var w = (rcS.right - rcS.left) * k1;
                    rcRes = rcS;
                    rcRes.left += ((rcRes.right - rcRes.left) - w) / 2;
                    rcRes.right = rcRes.left + w;
                }
            }

            PXC_Matrix mm = new PXC_Matrix();
            auxInst.MathHelper.Matrix_Reset(ref mm);
            mm = auxInst.MathHelper.Matrix_RectToRect(rcR, rcRes);
            PXC_Matrix mResMatrix = auxInst.MathHelper.Matrix_Multiply(ref m, ref mm);
            mRot = auxInst.MathHelper.Matrix_Invert(ref mRot);
            mResMatrix = auxInst.MathHelper.Matrix_Multiply(ref mResMatrix, ref mRot);
            CC.ConcatCS(mResMatrix);
            // Remove calcsize bit and show the text
            nFlags = nFlags ^ (uint)PXC_DrawTextFlags.DTF_CalcSizeOnly;
            CC.ShowTextBlock(wmText, ref rcMedia, ref rcMedia, nFlags, -1, null, null, null, out rcContentRect);

            currentPage.PlaceContent(CC.Detach(), (UInt32)PXC_PlaceContentFlags.PlaceContent_After);
        }
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get ContentCreator Rect as points?

Post by Sasha - Tracker Dev Team »

Hello whoit,

1)Our sample works fine - no distortions. Please provide a fully working sample project on this matter that we can test. Also, provide a file with the results (the link you gave us is broken).
As a piece of advice, try without the line-height parameter - if you are using it that is. Read Remarks here for more details:
https://sdkhelp.pdf-xchange.com/vi ... wTextBlock

2) Try commenting this piece of code and see whether it works:

Code: Select all

			//var dX = rcMedia.left;
			//var dY = rcMedia.bottom;
			//auxInst.MathHelper.Matrix_Translate(ref mRot, -dX, -dY);
			//rcMedia.left -= dX;
			//rcMedia.right -= dX;
			//rcMedia.top -= dY;
			//rcMedia.bottom -= dY;
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
whoit
User
Posts: 269
Joined: Tue Jul 07, 2015 3:30 pm

Re: How to get ContentCreator Rect as points?

Post by whoit »

Here is a link to sample C# code that shows the issue with skewed output.
www.coastallogic.com/wayne/TrackerTest_Watermark.zip

Here's a link to an example file where the text is skewed, and the angle of rotation is 45degrees which should not fit corner to corner!
(37 degrees is closer to the correct angle for a Letter size landscape page)
http://www.coastallogic.com/wayne/Lette ... eWEHTR.pdf

It also include my PDF test files which are rotated to various degrees (0, 90, 180, 270)

I've noticed that your Editor works perfectly when rotating the watermark, and when using a rotated PDF....

Thanks.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get ContentCreator Rect as points?

Post by Sasha - Tracker Dev Team »

Hello Wayne,

Found a typo in my code:

Code: Select all

var w = Math.Abs(rcS.top - rcS.bottom) * k1; //this is correct
With this it should work correctly.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
whoit
User
Posts: 269
Joined: Tue Jul 07, 2015 3:30 pm

Re: How to get ContentCreator Rect as points?

Post by whoit »

Perfect!

Thanks again...
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get ContentCreator Rect as points?

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply