logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
ESchunk  
#1 Posted : Wednesday, November 13, 2019 3:07:41 PM(UTC)
ESchunk

Rank: Member

Groups: Registered
Joined: 8/13/2019(UTC)
Posts: 12
United States
Location: New York

I'm trying to add a PdfFreeTextAnnotation to a Pdf and cannot get the RichText and/or Content to display. I can add the annotation at the desired Rectangle, set the color but when I view it in the viewer the RichText does not appear. Below is the function I'm using to add the annotation. (I copied some of the values from an existing annotation added in Adobe Acrobat, e.g. DefaultStyle and DefaultAppearance.)

Code:
		private void AddFreeTextAnnotation(PdfPage page, float left, float top, float right, float bottom, string contents, string richText)
		{
			if (page.Annots == null)
				page.CreateAnnotations();
			var annot = new PdfFreeTextAnnotation(page);
			annot.Rectangle = new FS_RECTF(left, top, right, bottom);
			annot.Contents = contents;
			annot.RichText = richText;
			annot.Color = FS_COLOR.White;
			annot.DefaultStyle = "font: Helvetica 8.0pt; text-align:center; color:#FF0000";
			annot.DefaultAppearance = "0.898 0.1333 0.2157 rg /Helv 8 Tf";
			annot.RegenerateAppearances();
			page.Annots.Add(annot);
			page.Dispose();
		}




Thanks,

Ted
Paul Rayman  
#2 Posted : Thursday, November 14, 2019 10:57:04 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 844

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
Hi,

As noticed here https://pdfium.patagames...pAnnotation_RichText.htm
This property is ignored when regenerates the annotation’s appearance stream.
ESchunk  
#3 Posted : Thursday, November 14, 2019 2:19:01 PM(UTC)
ESchunk

Rank: Member

Groups: Registered
Joined: 8/13/2019(UTC)
Posts: 12
United States
Location: New York

Hi Paul,

Thanks for your reply. Does that mean it is not possible to add a PDFFreeTextAnnotation with RichText using Patagames SDK? If you can use Patagames would you mind sharing sample code as I'm not able to figure it out from the documentation.

Thanks,

Ted
Paul Rayman  
#4 Posted : Sunday, November 17, 2019 11:13:22 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 844

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
Unfortunately, it is impossible to use RichText property, since this field can contain quite complex HTML markup, so that the full HTML parsing is required. Unfortunately, HTML parsing is currently not implemented. You can use the Content property.
If you need any complex text markup, in this case you can insert PdfTextObject with the given attributes directly into the NormalAppearance property of the annotation.
For example:

Code:

var txt1 = PdfTextObject.Create("First line", 250, 300, PdfFont.CreateFont(doc, "Arial", FontCharSet.DEFAULT_CHARSET, true, true, false), 25);
txt1.StrokeColor = FS_COLOR.Green;
txt1.FillColor = FS_COLOR.Red;
txt1.RenderMode = TextRenderingModes.FillThenStroke;
Pdfium.FPDFPageObj_SetLineWidth(txt1.Handle, 1);

var txt2 = PdfTextObject.Create("Second line", 250, 275, PdfFont.CreateFont(doc, "Courier"), 20);
txt2.StrokeColor = FS_COLOR.Blue;
txt2.FillColor = FS_COLOR.Yellow;
txt2.RenderMode = TextRenderingModes.FillThenStroke;
Pdfium.FPDFPageObj_SetLineWidth(txt2.Handle, 0.01f);

var freeTextAnnotation = new PdfFreeTextAnnotation(annots.Page);
freeTextAnnotation.CreateEmptyAppearance(AppearanceStreamModes.Normal);
freeTextAnnotation.NormalAppearance.Add(txt1);
freeTextAnnotation.NormalAppearance.Add(txt2);
freeTextAnnotation.GenerateAppearance(AppearanceStreamModes.Normal);

annots.Add(freeTextAnnotation);

Edited by user Sunday, November 17, 2019 11:13:55 PM(UTC)  | Reason: Not specified

ESchunk  
#5 Posted : Tuesday, November 19, 2019 9:39:34 AM(UTC)
ESchunk

Rank: Member

Groups: Registered
Joined: 8/13/2019(UTC)
Posts: 12
United States
Location: New York

Sample Freetext Annot.pdf (12kb) downloaded 1 time(s).Paul, thanks for your reply and code sample. I was able to use your code to get close to what meets the applications requirement but was hoping you could share your thoughts on a few questions. The annotation requires the text to be using Helvetica, 8pt standard (non-bold) font. When I use the code below it appears to be bold. If you compare it to the sample annotation with the number 1 in the attached file which uses the Helvetica, 8pt standard font it looks "thicker" than the one created with the code below.

Code:

var txt1 = PdfTextObject.Create(contents, left, bottom, PdfFont.CreateFont(page.Document, "Helvetica"), 8);
txt1.StrokeColor = FS_COLOR.Red;
txt1.FillColor = FS_COLOR.White;
txt1.RenderMode = TextRenderingModes.FillThenStroke;
Pdfium.FPDFPageObj_SetLineWidth(txt1.Handle, .75f);

var pathObj = PdfPathObject.Create(FillModes.Winding, true);
pathObj.Path.AppendRect(new FS_RECTF(txt1.BoundingBox.left - 3, txt1.BoundingBox.top + 3, txt1.BoundingBox.right + 3, txt1.BoundingBox.bottom - 3));
pathObj.StrokeColor = FS_COLOR.Red;
pathObj.FillColor = FS_COLOR.White;
pathObj.CalcBoundingBox();

var freeTextAnnotation = new PdfFreeTextAnnotation(page.Annots.Page);
freeTextAnnotation.CreateEmptyAppearance(AppearanceStreamModes.Normal);
freeTextAnnotation.NormalAppearance.Add(pathObj);
freeTextAnnotation.NormalAppearance.Add(txt1);
freeTextAnnotation.GenerateAppearance(AppearanceStreamModes.Normal);

page.Annots.Add(freeTextAnnotation);


In the sample code you shared you have the line below. What is the purpose of this line?

Code:
Pdfium.FPDFPageObj_SetLineWidth(txt1.Handle, 1);


I'm trying to draw a border around the text that is .75pt thick. I found code from one of your prior posts and used the code below. How do you specify the line thickness?

Code:

var pathObj = PdfPathObject.Create(FillModes.Winding, true);
pathObj.Path.AppendRect(new FS_RECTF(txt1.BoundingBox.left - 3, txt1.BoundingBox.top + 3, txt1.BoundingBox.right + 3, txt1.BoundingBox.bottom - 3));
pathObj.StrokeColor = FS_COLOR.Red;
pathObj.FillColor = FS_COLOR.White;
pathObj.CalcBoundingBox();



Finally, in the attached file you will see a freetextannoation with multiple lines. From your sample code it would appear that I would need to calculate the x and y coordinates of each line. Is that correct or is there an easy way to add multiple lines of text separated by \r\n (even if it is not using the RichText property)?

Thanks,

Ted
Paul Rayman  
#6 Posted : Wednesday, November 20, 2019 9:05:37 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 844

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
Originally Posted by: ESchunk Go to Quoted Post
When I use the code below it appears to be bold. If you compare it to the sample annotation with the number 1 in the attached file which uses the Helvetica, 8pt standard font it looks "thicker" than the one created with the code below.


Try to use TextRenderingModes.Fill instead of FillThenStroke;

Quote:
In the sample code you shared you have the line below. What is the purpose of this line?
Code:
Pdfium.FPDFPageObj_SetLineWidth(txt1.Handle, 1);



The width of the stroke line. It doesn't matter if the TextRenderingModes.Fill mode is used.

Quote:
I'm trying to draw a border around the text that is .75pt thick. I found code from one of your prior posts and used the code below. How do you specify the line thickness?

Code:
Pdfium.FPDFPageObj_SetLineWidth(pathObj.Handle, ...)



Quote:
is there an easy way to add multiple lines of text separated by \r\n (even if it is not using the RichText property)?

Unfortunatelly no. You should calculate text objects yourself.

But...

if you no need rich text then the you may use the following code
Code:
string textOfAnnot = "When you build source code, the build engine creates assemblies and executable applications. In general, the build process is very similar across many different project types such as Windows, ASP.NET, mobile apps, and others. The build process is also similar across programming languages such as C#, Visual Basic, C++, and F#. By building your code often, you can quickly identify compile - time errors, such as incorrect syntax, misspelled keywords, and type mismatches. You can also detect and correct run - time errors, such as logic errors and semantic errors, by building and running debug versions of the code. A successful build validates that the application's source code contains correct syntax and that all static references to libraries, assemblies, and other components can resolve. An application executable is produced that can be tested for proper functioning in both a debugging environment and through a variety of manual and automated tests to validate code quality. Once the application has been fully tested, you can compile a release version to deploy to your customers. For an introduction to this process, see Walkthrough: Building an application..";
string fontName = "Helvetica";
int fontSize = 8;

var freeTextWithoutAp = new PdfFreeTextAnnotation(annots.Page);
freeTextWithoutAp.BorderStyle = new Patagames.Pdf.Net.Wrappers.PdfBorderStyle();
freeTextWithoutAp.BorderStyle.Style = BorderStyles.Solid;
freeTextWithoutAp.BorderStyle.Width = 0.75f;
freeTextWithoutAp.BorderStyle.DashPattern = new float[] { 3, 3 };
freeTextWithoutAp.BorderEffect = new PdfBorderEffect();
freeTextWithoutAp.BorderEffect.Effect = BorderEffects.None;
freeTextWithoutAp.BorderEffect.Intensity = 0;
freeTextWithoutAp.Intent = AnnotationIntent.FreeTextTypeWriter;
freeTextWithoutAp.Color = FS_COLOR.White;
freeTextWithoutAp.Contents = textOfAnnot;
freeTextWithoutAp.DefaultAppearance = $"/{fontName} {fontSize} Tf {1} {0} {0} rg {1} {0} {0} {1} {0} {0} Tm";
freeTextWithoutAp.Opacity = 1.0f;
freeTextWithoutAp.Rectangle = new FS_RECTF(0, 500, 450, 350);
freeTextWithoutAp.Subject = "Tbhis is a subj";
freeTextWithoutAp.Text = "This is a user";
freeTextWithoutAp.TextAlignment = JustifyTypes.Centered;
freeTextWithoutAp.RegenerateAppearances();
annots.Add(freeTextWithoutAp);



ESchunk  
#7 Posted : Wednesday, November 20, 2019 6:11:02 PM(UTC)
ESchunk

Rank: Member

Groups: Registered
Joined: 8/13/2019(UTC)
Posts: 12
United States
Location: New York

Thanks for you quick response.

When I used Fill (instead of FillThenStroke) the content does not appear.

This worked great:
Code:
Pdfium.FPDFPageObj_SetLineWidth(pathObj.Handle, ...)


I will try your solution for multiline text tomorrow.

Thanks,

Ted
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.