JAVA: after adding text in pdf, text is getting partially hidden/cropped from top side on zoom out and zoom in

Text getting cropped/hidden from top side.
I have attached files for more information.

Also, im using default mode of pdf writting which is top-left to bottom-right, and i dont want to place text based on fixed position. i want to add text in new page based on top-left to top-right based.

I tried 2 solutions but its not working

#solution 1

TextSegment ts = new TextSegment(text);
ts.getTextState().setFontSize(fontsize);
ts.getTextState().setFont(textFont);
ts.getTextState().setForegroundColor(textColor);

		TextFragment mainHeading = new TextFragment();
		mainHeading.getSegments().add(ts);
		mainHeading.setHorizontalAlignment(horizontalAlignment);
	
		mainHeading.setMargin(margin);
		
		page.getParagraphs().add(mainHeading);

#solution 2

TextSegment ts = new TextSegment(text);
ts.getTextState().setFontSize(fontsize);
ts.getTextState().setFont(textFont);
ts.getTextState().setForegroundColor(textColor);

		TextFragment mainHeading = new TextFragment();
		mainHeading.getSegments().add(ts);
		mainHeading.setHorizontalAlignment(horizontalAlignment);
		if (!StringUtils.isEmpty(margin))
			mainHeading.setMargin(margin);
		
		page.getParagraphs().add(mainHeading);

Images and pdf:
Text Pdf issue.pdf (12.3 KB)

cocissue.PNG (33.1 KB)

@rabinintig

The issue may be occurring due to particular font because I can not reproduce it on my end. Can you please share the font file, font size and margin values. heading.pdf (2.0 KB)

I’m using open-sans fonts, I am sharing the openSans-semiBold.ttf and openSans-regular.ttf font files. Also, the problem is happening even if we don’t add margins.

Incase, if you found that issue is happening due to fonts, Kindly let me know the list of all supported fonts.

font size: 18 (I think, it’s independent of font size)

Font files:
fonts.zip (118.8 KB)

@rabinintig

You can add the required fonts and set path to font directory by the method:

FontRepository.addLocalFontPath("path_to_fonts"); 

and you can set any font with the following code:

FontRepository.addLocalFontPath("path_to_fonts");
Document document = new Document();
Page page = document.getPages().add();
TextSegment ts = new TextSegment("Certificate of Completion");
ts.getTextState().setFontSize(14);
ts.getTextState().setFont(new FontRepository().findFont("Open Sans"));
ts.getTextState().setForegroundColor(Color.getBlack());
TextFragment mainHeading = new TextFragment();
mainHeading.getSegments().add(ts);
mainHeading.setHorizontalAlignment(2);		
page.getParagraphs().add(mainHeading);
document.save("heading.pdf");

heading.pdf (14.1 KB)

FontRepository.addLocalFontPath(“path_to_fonts”);
This method is not supported in our current jar.

We are using Aspose pdf 16.10.0, and can’t update our jar as only Aspose pdf 16.10.0 is compatible with our current license.

The text is getting croped/hidden from top when using below code and open-sans .ttf font file in Aspose pdf 16.10.0.

Java Code:

com.aspose.pdf.Font font = FontRepository.openFont(new ClassPathResource("/OpenSans-SemiBold.ttf").getInputStream(),
FontTypes.TTF);
Document document = new Document();
Page page = document.getPages().add();
TextSegment ts = new TextSegment(“Certificate of Completion”);
ts.getTextState().setFontSize(18);
ts.getTextState().setFont(font);
ts.getTextState().setForegroundColor(Color.getBlack());
TextFragment mainHeading = new TextFragment();
mainHeading.getSegments().add(ts);
mainHeading.setHorizontalAlignment(2);
page.getParagraphs().add(mainHeading);
document.save(“heading.pdf”);

1. can you please confirm whether the text cropped/partially hidden from top is due to open-sans font in aspose pdf 16.10.0?

2. can you please provide the solution which is compatible with aspose 16.10.0 jar?

I have attached font file and pdf file.
fonts.zip (118.8 KB)
textCroppedOnZoomInZoomOut.pdf (12.6 KB)

@rabinintig

Please note that we can only consider this to be an issue only if that is reproduced using latest version too and we log that in our issue tracking system for investigation and resolution.

As per the PDF file we have shared with you, we can not reproduce it on our end. You can request a 30-day Temporary License. Please refer to How to get a Temporary License and share your feedback after testing addLocalFontPath method on your end.