Italic font style leads to wrong text calculation and cut off the last letter

Hi there,
I use Java Aspose.Pdf library to place the text stamp onto the pdf document. I’ve faced with issue using italic font style.
I create the TextStamp and fill respective TextState with font (Courier), font size (190) and font style (Italic) properties.

So, this is the problems I need to solve

  1. TextState.measureString(String str) should calculate the width of the string represented with this text state. But it doesn’t take into account the font properties from the text state. The size of the text stays the same for different fonts.
    Not sure whether this is a bug or desired behavior from development team.

  2. I place the word “Preview” with the font size (190), font (Courier), font size (190) and font style (Italic) and it appeared to be cut off. The last letter “w” is cut off, but there’s still free space on the page. If I change this ending “w” to double “v”, means “vv”, it displayed correctly. I guess it happens due to the last one is individual letter representation and “w” is too wide symbol. This problem happens only for Italic font style. And could be solved with smaller font size = 170. But it’s not the solution and not the great workaround.

Could you help me with that, please?

Best regards,
Vlada

@ladavilada

Thank you for contacting support.

We would like to request you to share a narrowed down sample application along with source and generated files so that we may investigate to help you out. Before sharing requested data, please ensure using Aspose.PDF for .NET 18.5 in your environment.

Hi team,

I’m using Aspose.PDF for Java 17.12 version. I’ve tried with 18.5 version, problem remains the same.

Here’s the code snippet, where I’ve added text stamp on the document page. TextStamp, Page, etc. were taken from com.aspose.pdf package.

public void addStamp(Page page) {

    TextStamp textStamp = new TextStamp("Preview");

    textStamp.getTextState().setFont(FontRepository.findFont("Courier", true));
    textStamp.getTextState().setFontSize(190);
    textStamp.getTextState().setFontStyle(FontStyles.Italic);

    // based on the page size and stamp width
    Dimension stampDimension = getDimensionToFit();

    // compute the width to be set for the text stamp
    textStamp.setWidth(stampDimension.getWidth());
    textStamp.setHeight(stampDimension.getHeight());
    textStamp.setRotateAngle(60);

    page.addStamp(textStamp);
}


private static Dimension getDimensionToFit(TextStamp textStamp, Dimension pageSize) {

    double textWidth = textStamp.getTextState().measureString(textStamp.getValue());
    double ratio = pageSize.getWidth() / textWidth;

    return new Dimension(textWidth * ratio,
            textStamp.getTextState().getFontSize());
}

@ladavilada

Would you please share a working sample project reproducing this issue because shared code snippet is not compiling fine in its present form, owning to a few anomalies in the code. We will then proceed further to help you out.

Hello Farthan,

I’ve added a working snippet. Just execute the main method and point the right path to the input pdf document, please. You will see that even pages marked with Regular font style and odd pages with Italic. And Italic text stamp is cut off.

package your.package.name;

import com.aspose.pdf.Document;
import com.aspose.pdf.FontRepository;
import com.aspose.pdf.FontStyles;
import com.aspose.pdf.Page;
import com.aspose.pdf.PageInfo;
import com.aspose.pdf.TextStamp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * Place text stamp for the pdf document.
 */
public class TextStampGenerator {
    private static final String PATH_TO_DIR = "src/main/resources/";
    private static final String IN_FILE_PDF = "inFile.pdf";
    private static final String OUT_FILE_PDF = "outFile.pdf";

    public static void main(String[] args) throws FileNotFoundException {
        Document doc = new Document(new FileInputStream(
                new File(PATH_TO_DIR + IN_FILE_PDF)));

        for (Page page : doc.getPages()) {
            addStamp(page, page.getNumber());
        }

        doc.save(PATH_TO_DIR + OUT_FILE_PDF);
    }

    /**
     * Add italic font style stamp for odd pages
     * and regular font style stamp for even pages.
     *
     * @param page       the pdf document page
     * @param pageNumber number of the page
     */
    public static void addStamp(Page page, int pageNumber) {

        TextStamp textStamp = new TextStamp("Preview");

        textStamp.getTextState().setFont(FontRepository.findFont("Courier", true));
        textStamp.getTextState().setFontSize(190);

        if (pageNumber % 2 == 0) {
            textStamp.getTextState().setFontStyle(FontStyles.Regular);
        } else {
            textStamp.getTextState().setFontStyle(FontStyles.Italic);
        }

        // based on the page size and stamp width
        Dimension stampDimension = getDimensionToFit(textStamp, page.getPageInfo());

        // compute the width to be set for the text stamp
        textStamp.setWidth(stampDimension.getWidth());
        textStamp.setHeight(stampDimension.getHeight());
        textStamp.setRotateAngle(60);

        page.addStamp(textStamp);

    }


    private static Dimension getDimensionToFit(TextStamp textStamp, PageInfo pageInfo) {

        double textWidth = textStamp.getTextState().measureString(textStamp.getValue());
        double ratio = pageInfo.getWidth() / textWidth;

        return new Dimension(textWidth * ratio,
                textStamp.getTextState().getFontSize());
    }

    private static class Dimension {

        private double width;
        private double height;

        public Dimension(double width, double height) {
            this.width = width;
            this.height = height;
        }

        public double getWidth() {
            return width;
        }

        public void setWidth(double width) {
            this.width = width;
        }

        public double getHeight() {
            return height;
        }

        public void setHeight(double height) {
            this.height = height;
        }
    }

}

@ladavilada

We have worked with the data shared by you and have been able to reproduce the issue in our environment. A ticket with ID PDFJAVA-37778 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

Hello Farthan,

Am I right that a ticket registered with ID PDFJAVA-37778 has been resolved?
In this case which aspose release/version should I use now to check whether it works?

Regards,
Vlada

@ladavilada

We would like to share with you that the ticket PDFJAVA-37778 has been resolved for Aspose.PDF for Java 18.6; tentative date for the release of it is end of June or first week of July 2018. We will notify you as soon as the product will be released. We appreciate your patience and comprehension in this regard.

The issues you have found earlier (filed as PDFJAVA-37778) have been fixed in Aspose.PDF for Java 18.6.