Adding Annotation(s)

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

Adding Annotation(s)

Post by whoit »

Do you have some sample code that describes how to add annotation(s) to an existing PDF using CoreAPI?

I've managed to create a very basic annotation, but there's a lot of information missing from the documentation.

My goal is a simple textbox like this:
Image

This is what I have so far but I cannot control
1) The border color / alpha
2) The internal color / alpha
3) The text alignment
4) The font parameters - size, name, color
5) I'd like to flatten the annotation so that it cannot be edited once applied.

Code: Select all

 
PXC_Rect myRect;
myRect.bottom = 100;
myRect.top = 200;
myRect.left = 100;
myRect.right = 200;
IPXS_Inst pxInst = pxcInst.GetExtension("PXS");
uint nTextMUAtom = pxInst.StrToAtom("FreeText");
Debug.WriteLine("textmarkup atom is:" + nTextMUAtom);
IPXC_Annotation pAnnot = inPage.InsertNewAnnot(nTextMUAtom, myRect);
IPXC_AnnotData_FreeText TextMUData = (IPXC_AnnotData_FreeText)pAnnot.Data;
TextMUData.Contents = "Hello World";
        
// These seem to have no effect
//TextMUData.Color.SetRGB(0.0f, 0.0f, 1.0f);
//TextMUData.FColor.SetRGB(0, 0, 0);
//TextMUData.SColor.SetRGB(1.0f, 0.0f, 1.0f);

PXC_AnnotBorder myBorder = new PXC_AnnotBorder();
myBorder.nWidth = 4;
myBorder.nStyle = PXC_AnnotBorderStyle.ABS_Dashed;
TextMUData.set_Border(myBorder);

//TextMUData.TextRotation = 45; // no effect ?
//TextMUData.DefaultTextColor.SetRGB(1.0f, 0.0f, 1.0f); // no effect ?

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

Re: Adding Annotation(s)

Post by Sasha - Tracker Dev Team »

Hello Wayne,

Here's a sample, that answers all of your's 5 questions:

Code: Select all

PXC_Rect rc;
IPXC_Document doc = CreateNewDocument(out rc);
PXC_Rect rcAnnot;
rcAnnot.left = 100;
rcAnnot.bottom = 600;
rcAnnot.top = 700;
rcAnnot.right = 300;

IPXS_Inst pxsInst = (IPXS_Inst)pdfCtl.Inst.GetExtension("PXS");
//Getting Free Text annotation atom for the InsertNewAnnot method
uint nTextBox = pxsInst.StrToAtom("FreeText");
IPXC_Page pPage = doc.Pages[0];
IPXC_Annotation Annot = pPage.InsertNewAnnot(nTextBox, ref rcAnnot, 0);
if (Annot == null)
	return;
//Filling the annotation with needed text
IPXC_AnnotData_FreeText FTData = (IPXC_AnnotData_FreeText)Annot.Data;
var textColor = auxInst.CreateColor(ColorType.ColorType_RGB);
var fColor = auxInst.CreateColor(ColorType.ColorType_RGB);
var sColor = auxInst.CreateColor(ColorType.ColorType_RGB);
//Stroke Color
sColor.SetRGB(0, 0.2f, 0.2f);
FTData.SColor = sColor;
//Fill Color
fColor.SetRGB(0.4f, 0, 0.4f);
FTData.FColor = fColor;
//Text Color
textColor.SetRGB(0.6f, 0.6f, 0);
//Annotation's opacity
FTData.Opacity = 0.7;
//Font Size, Name, Color
FTData.DefaultFontSize = 18;
FTData.DefaultTextColor = textColor;
FTData.DefaultFont = doc.CreateNewFont2("ArialMT");
//Text alignment
FTData.DefaultTextAlign = (int)PXC_TextJustification.TJ_Middle;
FTData.Contents = "Hello World";
Annot.Data = FTData;

//Flattening the annotation
IPXC_Inst pxcInst = (IPXC_Inst)pdfCtl.Inst.GetExtension("PXC");
IPXC_AnnotsList al = pxcInst.CreateAnnotsList();
al.Insert(Annot);
doc.FlattenAnnotations(0, al);

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: Adding Annotation(s)

Post by whoit »

Great!
Thanks Alex - i'll post again with my results.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Adding Annotation(s)

Post by Sasha - Tracker Dev Team »

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

Re: Adding Annotation(s)

Post by whoit »

Ok, it looks like everything is working...

One more question though - this method:

Code: Select all

Annot.TextRotation
seems to be limited to 0,90,180,270
Is there an enum for this setting?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Adding Annotation(s)

Post by Sasha - Tracker Dev Team »

Hello Wayne,

It's being limited to the values that you have given. Here's a piece of our inner code:

Code: Select all

//LONG nRotation is an input value
nRotation = (nRotation % 360);
nRotation = (nRotation / 90) * 90;
m_nTextRotation = nRotation;
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
whoit
User
Posts: 269
Joined: Tue Jul 07, 2015 3:30 pm

Re: Adding Annotation(s)

Post by whoit »

Ok, two more:

1) I see the Annotation "Opacity" setting but is there a way to set the Stroke alpha separately from the Fill alpha?
I looked at IColor but didn't see an RGBA....

2) How do I use this Enum to align text "Left and Middle" or "Center and Middle" or "Right and Bottom", etc?
enum PXC_TextJustification
{
TJ_Default = -1,
TJ_Left = 0,
TJ_Center = 1,
TJ_Right = 2,
TJ_Justify = 3,
TJ_JustifyAll = 4,
TJ_Radix = 5,
TJ_Top = 0,
TJ_Bottom = 2,
TJ_Middle = 1,
};
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Adding Annotation(s)

Post by Sasha - Tracker Dev Team »

Hello Wayne,

The opacity is being set for the annotation only - no separate alpha for the colors.

Can you do that for the annotations in the end user Editor? As far as I remember the alignment in the free text annotations is horizontal only. These flags are widely used thus other values are available.

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

Re: Adding Annotation(s)

Post by whoit »

OK, got it.

No, the vertical alignment doesn't work in the editor....oh well.

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

Re: Adding Annotation(s)

Post by Sasha - Tracker Dev Team »

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

Re: Adding Annotation(s)

Post by whoit »

Hi Alex -

I found an issue with the Dash style border.

When I use this setup:

Code: Select all

myBorder.DashArray[0] = 4.0f; //width
myBorder.DashArray[1] = 2.0f; //width
myBorder.DashArray[2] = 4.0f; //width
myBorder.DashArray[3] = 8.0f; //width
myBorder.nDashCount = 3; //Number of dashes used
I get this expected result:
Image

However, if I change the "nDashCount" to 4:

Code: Select all

myBorder.DashArray[0] = 4.0f; //width
myBorder.DashArray[1] = 2.0f; //width
myBorder.DashArray[2] = 4.0f; //width
myBorder.DashArray[3] = 8.0f; //width
myBorder.nDashCount = 4; //Number of dashes used
I get this result:
Image


Why am I not seeing the desired pattern?
Is there some max combined size for dash patterns?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Adding Annotation(s)

Post by Sasha - Tracker Dev Team »

Hello Wayne,

Everything worked as you have specified - I've modified your sample picture:
Snap2Dash.png
Snap2Dash.png (5.75 KiB) Viewed 3677 times
Also, in case of the rectangle annotations - the start of the dash pattern would be the left bottom corner (visually).

In case of your first sample - if your pattern has a odd number of dashes - they would change cyclically. Here's the illustration from the PDF specification:
oddDashPattern.png
(7.08 KiB) Not downloaded yet

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

Re: Adding Annotation(s)

Post by whoit »

Well, that makes sense!

Thanks again,
Wayne
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17824
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Adding Annotation(s)

Post by Tracker Supp-Stefan »

:D
Post Reply