We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Text AROUND circle in aspose.pdf?

I would like to know if there is a known approach to writing text in a circle or arc or curve. etc.

Adding text to a (circular) shape places the text Inside the shape, not around it (obviously).
Is there a setting I’m missing somewhere to get the desired behavior?
circle-example.png (43.3 KB)

Included the same modified otput pdf:
circle-example.pdf (74.6 KB)

Path generation code snippet:

float PosX = 214.44000244140625;
float PosY = 631.15997314453125;
float Radius = 171.552001953125;
Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)(page.PageInfo.Width), (float)page.PageInfo.Height);
page.Paragraphs.Add(graph);
Aspose.Pdf.Drawing.Circle circle = new Aspose.Pdf.Drawing.Circle(PosX, PosY, Radius);
TextFragment textFragment = new TextFragment(“LOREM IPSUM DOLOR STI AMET”);
textFragment.TextState.FontSize = 16;
textFragment.BaselinePosition = new Position(0, 0);
textFragment.HorizontalAlignment = HorizontalAlignment.Left;
textFragment.VerticalAlignment = VerticalAlignment.Top;
textFragment.Margin = new MarginInfo(0, 0, 0, 0);
textFragment.TextState.Font = FontRepository.FindFont(“Tahoma”);
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Black
circle.Text = textFragment;
graph.Shapes.Add(circle);

Trying to do this in a “different” way.

Any ideas why the SVG here is added but is not visble when opening the file in editor programs such as illustrator?

And any feedback on why it is adding just a circle, instead of the text that is included within the SVG as depicted by the SVG?
It seems the image parser isn’t capable of actually computing the SVG values. Even the SVG opacity is being ignored.

// Set coordinates
int lowerLeftX = 0;
int lowerLeftY = 0;
int upperRightX = (int)Math.Round(((float)page.CropBox.LLX * 2) + (float)page.CropBox.Width);
int upperRightY = (int)Math.Round(((float)page.CropBox.LLY * 2) + (float)page.CropBox.Height);

Text = "data:image/svg+xml;utf8,<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 1024 1024\"><circle id=\"curve\" cx=\"50%\" cy=\"50%\" r=\"43%\" opacity=\"1\"/><text width=\"500\" font-family=\"Tahoma\" font-size=\"16pt\" fill=\"#000\"><textPath alignment-baseline=\"baseline\" href=\"#curve\">a dsfasd asd asa a dfads adsf asdf asdf</textPath></text></svg>";
// Load image into stream
MemoryStream imageStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(Text));
// Add image to Images collection of Page Resources
page.Resources.Images.Add(imageStream);
// Using GSave operator: this operator saves current graphics state
page.Contents.Add(new Aspose.Pdf.Operators.GSave());
// Create Rectangle and Matrix objects
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
XImage ximage = page.Resources.Images[page.Resources.Images.Count];
// Using Do operator: this operator draws image
page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
// Using GRestore operator: this operator restores graphics state
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());

Note:
Chrome compativle SVG - no change. just a circle being added.

Text = "data:image/svg+xml;utf8,<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 1024 1024\"><path id=\"curve\" d=\"M 512 512 m -440.32, 0 a 440.32,440.32 0 1,0 880.64,0 a 440.32,440.32 0 1,0 -880.64,0\" opacity=\"0\" fill=\"#000\"/><text font-family=\"Tahoma\" font-size=\"16pt\" fill=\"#000\"><textPath alignment-baseline=\"baseline\" href=\"#curve\">a dsfasd asd asa a dfads adsf asdf asdf</textPath></text></svg>";

The following code runs out of memory when adding the stamp:

> PdfFileStamp fileStamp = new PdfFileStamp();
> fileStamp.BindPdf(pdf);
> Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();
> stamp.BindImage(file);
> stamp.SetOrigin(0, 0);
> stamp.Rotation = 90.0F;
> stamp.IsBackground = true;
> stamp.Pages = new int[] {1};
> fileStamp.AddStamp(stamp); // <- out of memory exception.

Save this text as SVG to use as file:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024"><path id="curve" d="M 512 512 m -440.32, 0 a 440.32,440.32 0 1,0 880.64,0 a 440.32,440.32 0 1,0 -880.64,0" opacity="0" fill="#000"/><text font-family="Tahoma" font-size="16pt" fill="#000"><textPath alignment-baseline="baseline" href="#curve">a dsfasd asd asa a dfads adsf asdf asdf</textPath></text></svg>

@TraitDev

We need to investigate the possibility of adding text in a curve path. Also, we tried to use below code snippet and it generated a SVG with black circle only:

Aspose.Pdf.LoadOptions loadopt = new Aspose.Pdf.SvgLoadOptions();
var svgdata = @"<svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 1024 1024""><path id=""curve"" d=""M 512 512 m -440.32, 0 a 440.32,440.32 0 1,0 880.64,0 a 440.32,440.32 0 1,0 -880.64,0"" opacity=""0"" fill=""#000""/><text font-family=""Tahoma"" font-size=""16pt"" fill=""#000""><textPath alignment-baseline=""baseline"" href=""#curve"">a dsfasd asd asa a dfads adsf asdf asdf</textPath></text></svg>";
MemoryStream imageStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(svgdata));
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(imageStream, loadopt);

doc.Save(dataDir + @"SVG to PDF.pdf");

Therefore, an issue as PDFNET-52576 has been logged in our issue tracking system. We will further look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

Thank you.

In the case of the SVG it just seems like it isn’t being parsed.
Using the same SVG as an HTML document does work.

In both cases (just the circle, or the Html conversion) it seems that the SVG is not being kept as a vector, but rather flattened (merged to a single image).

I believe the Filestamp issue probably needs to be split or have a ticket as well.
I tried looking into it a bit, I see obvious reason why it is running out of memory when using an SVG as the image.

@asad.ali
I developed my own system to rotate TextFragment(s) and place them accordingly.

Is there any way to rotate a textSegment instead?

Using separated text fragments generates individual characters.
This isn’t all that bad, but ideally I need to allow the end result to be Copy/Pasted from the PDF.
Currently, the text is split into New Characters per letter when doing so.

s
o
m
e
t
h
i
n
g
e
l
s
e
g
o
e
s
h
e
r
e

I attempted to a few fragment options:
textFragment.IsInLineParagraph = true;
textFragment.IsKeptWithNext = true;

It appears that each fragment is just treated as a separate paragraph.
If that is correct, then the TextSegment would be the one I need to modify the position and rotation of to obtain the end result.

Double Bonus:
A way to warp (distort) text fragment’s based on Rectangle while preserving the font would be great if possible.

@TraitDev

At the moment, we have recorded this information within the same ticket. We will create a separate task for it as soon as investigation is done.

Can you please share the complete code snippet that you used to generate the above output?

You could use an existing property i.e. TextFragment.TextState.Rotation in order to achieve similar results. However, it would not help you achieving the rendering of the text on a curved path.

As I said, It’s a compromise. All TextFragments are their own paragraph (or just about).
It looks correct, but you cannot copy the text or you get a string with new lines like shown above.

Maybe a ticket can be addeded requesting Rotation be added to TextSegment?
A matrix transform for each segment/character would also be useful if it were added…

@TraitDev

Sure, we will creating corresponding requests in our issue management system and will be sharing with you shortly.

@TraitDev

The issue related to SVG and OOM Exception has been registered as PDFNET-52593 in our issue tracking system as well as a feature request to introduce rotation property for TextSegment has also been logged as PDFNET-52592. All the tickets have been associated with this forum thread and you will receive a notification as soon as they are resolved.

We are sorry for the inconvenience.