Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StackOverflowError by calling PdfGraphics2D.translate(int,int) #313

Closed
V-F opened this issue Dec 16, 2019 · 1 comment
Closed

StackOverflowError by calling PdfGraphics2D.translate(int,int) #313

V-F opened this issue Dec 16, 2019 · 1 comment

Comments

@V-F
Copy link
Contributor

V-F commented Dec 16, 2019

After f73467c commit, calling the method translate(int,int) of the class PdfGraphics2D results in an StackOverflowError. Bugfix is proposed in the 306 PR.
The following code example may reproduce this error when uncommenting a line:

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.DefaultFontMapper.BaseFontParameters;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class Test {

	private static final float 	PAGE_A4_WIDTH_PX		= 793.7007f;
	private static final float 	PAGE_A4_HEIGHT_PX		= 1122.5196f;
	private static final float	TEMPLATE_WIDTH_HEIGHT	= 400;
	private static final String	TESTED_TEXT				= "Hello world";

	public static void main(String[] args) {
		try (Document document = new Document()) {
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test_pdf.pdf"));
			document.open();

			PdfContentByte pdfContentByte = writer.getDirectContent();
			document.setPageSize(new Rectangle(0, 0, PAGE_A4_WIDTH_PX, PAGE_A4_HEIGHT_PX));
			document.newPage();

			PdfTemplate pdfTemplate = pdfContentByte.createTemplate(PAGE_A4_WIDTH_PX, PAGE_A4_HEIGHT_PX);

			Graphics2D g2D = pdfTemplate.createGraphics(TEMPLATE_WIDTH_HEIGHT, TEMPLATE_WIDTH_HEIGHT);			
			AffineTransform transform = g2D.getTransform();
			g2D.setColor(Color.black);
			try {
				// Uncomment to get an error
				//g2D.translate(50, 50);

				g2D.drawString(TESTED_TEXT, 15, 20);
			} finally {
				g2D.setTransform(transform);
				g2D.dispose();
			}
			pdfContentByte.addTemplate(pdfTemplate, 0, PAGE_A4_HEIGHT_PX - TEMPLATE_WIDTH_HEIGHT);
		} catch (DocumentException | IOException de) {
			System.err.println(de.getMessage());
		}
	}
}
@asturio
Copy link
Member

asturio commented Jan 15, 2020

PR integrated

@asturio asturio closed this as completed Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants