Word spacing an issue in the Aspose PDF file

Hello Aspose Team


I am observing a strange outcome after I generate a pdf via Aspose API’s.
The below code sets the Font to be "Arial"and Font size to be 20 -> After executing this code we get a PDF file that has extra/wide space in between the header text words. Attaching herewith the image of the issue in discussion wherein there appears to be extra spaces in between the header text.

I tried below 2 workarounds but to no luck :frowning:

1) Downloaded and used the latest Aspose PDF “aspose-pdf-11.3.0” to generate the pdf but that didn’t solve the issue.

2) I tried to use the API setWordSpacing() s with different input values but to no luck. Can you please help me to know if this is an issue with Aspose PDF API’s or incorrect usage of setWordSpacing API ? Your quick reply on this issue much appreciated.

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(pdfFilePath);

TextFragmentAbsorber absorber = new TextFragmentAbsorber();
pdfDocument.getPages().accept(absorber);
TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

for(Iterator iterator = textFragmentCollection.iterator(); iterator.hasNext():wink:
{
TextFragment textFragment = iterator.next();
String fontName = textFragment.getTextState().getFont().getFontName();
if(fontName.equals(PDF_HEADER_FONT)){
com.aspose.pdf.Font font = FontRepository.findFont(“Arial”);
textFragment.getTextState().setFont(font);
textFragment.getTextState().setFontSize(20.0f);
textFragment.getTextState().setWordSpacing(0);
}
}
pdfDocument.save();

Let me know if you need any other info from my side.

Regards
-Amit

Hello Aspose Team


This issue is critical to our project. Hence any quick fix / workaround / suggestion will be much appreciated…

Regards
-Amit

Hi Amit,


Sorry for the delayed response.

Can you please share the resource PDF file, so that we can test the scenario in our environment. We are sorry for this inconvenience.

Hello Shahbaz


As requested please find attached herewith the resource PDF file.

Let us know your observation on the same

Regards
-Amit

Hi Amit,


Thanks for sharing the resource file.

I have tested the scenario and have managed to reproduce same problem. For the sake of correction, I have logged it as PDFNEWJAVA-35690 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Hello,


Do you have any news ?

Hi Sabastien,

Thank for your patience. We have investigate the issue and would like to update you that the words and spaces in the header of this document are not linked to each other (it has 5 independent text fragment with their own positions). So even if you are changing the font size, the starting position of every fragment remains unchanged. But, if you need, you can calculate a new positions of fragments by yourself. Please find code snippet below, it will help you to accomplish the task.

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("c:/pdftest/HeaderWordSpaceIssue_704724.pdf");

TextFragmentAbsorber absorber = new TextFragmentAbsorber();
pdfDocument.getPages().accept(absorber);

TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

double delta = 0;
int index = 0;
double oldWidth = 0;
int pageNumber = 1;

for (java.util.Iterator<TextFragment> iterator = textFragmentCollection.iterator(); iterator.hasNext();) {
    TextFragment textFragment = iterator.next();

    if (textFragment.getPage().getNumber() != pageNumber) {
        pageNumber = textFragment.getPage().getNumber();
        index = 0;
        delta = 0;
    }

    String fontName = textFragment.getTextState().getFont().getFontName();

    if (fontName.equals("Arial")) {
        com.aspose.pdf.Font font = FontRepository.findFont("Arial");

        oldWidth = textFragment.getRectangle().getWidth();

        textFragment.getTextState().setFont(font);
        textFragment.getTextState().setFontSize(20.0f);
        textFragment.getTextState().setWordSpacing(0);

        textFragment.setPosition(new Position(
            textFragment.getPosition().getXIndent() - delta,
            textFragment.getPosition().getYIndent()
        ));

        delta = delta - textFragment.getRectangle().getWidth() + oldWidth;
        index++;
    }
}

pdfDocument.save("c:/pdftest/FontReplace.pdf");

Please feel free to contact us for any further assistance.

Best Regards,