PDF output does not center text

I have a word document with a piece of text that is centered.

Using the TextFragmentAbsorber, I find the piece of text and replace it with something else
and then save the document as a pdf.

When I look at the pdf, the replaced piece of text is no longer centered.

Is there a way to keep it centered?

Hi Dirk,

Thanks for your inquiry. Could you please attach your input Word and output Pdf documents here for testing? I will investigate the issue on my side and provide you more information.

Best Regards,

I've modified the code to use a .pdf as the source file. I replace the text using the textfragmentabsorber.

I've added the source pdf file that I use.

I replace the <> with another text, but the text does not remain centered.

Hi Dirk,


Thanks for the additional information. I feel your query is more related to Aspose.Pdf, I will move your request in Aspose.Pdf forum. My colleagues from Aspose.Pdf component team will answer you shortly.

Best regards,

Hi Dirk,


Thanks for sharing the resource file.

I have tested the scenario using Aspose.Pdf for .NET 7.6.0 and as per my observations, the text remains center aligned when replaced with new string. Please take a look over the attached output document which I have generated.

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document("c:/pdftest/brunokaart.pdf");

//create TextAbsorber object to find all instances of the input search phrase

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("<>");

//accept the absorber for a particular page

pdfDocument.Pages[1].Accept(textFragmentAbsorber);

//get the extracted text fragments

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

//loop through the fragments

foreach (TextFragment textFragment in textFragmentCollection)

{

//update text and other properties

textFragment.Text = "New Phrase";

textFragment.TextState.Font = FontRepository.FindFont("Verdana");

textFragment.TextState.FontSize = 16;

}

pdfDocument.Save(“c:/pdftest/TextReplace_output.pdf”);

Try this with a longer string like 'This is a new phrase', then you'll clearly see the difference.

The beginning of 'New Phrase' is on the exact location as '<>', but it should be slightly more to the left to remain centered.

Hi Dirk,


Thanks
for sharing the details.

I
have tested the scenario and I am able to reproduce the same problem. For the
sake of correction, I have logged it in our issue tracking system as
PDFNEWNET-34723. We
will investigate this issue in details and will keep you updated on the status
of a correction.

We
apologize for your inconvenience.

It has been quite a while since i've heard from you on this subject. Has there been any progress?

By the way, I've upgraded to version 7.8.0.0 but the problem remains.

Hi Dirk,


Sorry for the inconvenience faced. I’m afraid your reported issue is still not revolved due to other priority tasks. However I’ve requested the development team to share an ETA at their earliest. As soon as I get a feedback I’ll update you via this forum thread.

Thanks for your patience and cooperation.

Best Regards,

Hi,


Thanks for your patience.

We have further investigated the problem reported earlier and as per our observations, Automatically center alignment is not possible. However it’s possible to center alignment programmatically:

1. compute x coordinate
2. set the text fragment position:

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document("c:/pdftest/brunokaart.pdf");

//create TextAbsorber object to find all instances of the input search phrase

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("<>");

//accept the absorber for a particular page

pdfDocument.Pages[1].Accept(textFragmentAbsorber);

//get the extracted text fragments

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

//loop through the fragments

foreach (TextFragment textFragment in textFragmentCollection)

{

//update text and other properties

textFragment.Text = "This is a new phrase";

textFragment.TextState.Font = FontRepository.FindFont("Verdana");

textFragment.TextState.FontSize = 16;

textFragment.Position = new Position(pdfDocument.Pages[1].Rect.LLX +(pdfDocument.Pages[1].Rect.Width - textFragment.Rectangle.Width) / 2, textFragment.Position.YIndent);

}

pdfDocument.Save(“c:/pdftest/TextReplaceOutput.pdf”);