Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text is reverse in generated pdf

Tags:

pdf

grails

pdfbox

I am using pdfbox to add a line to pdf file. but the text i am adding is reversed.

File file = new File(filePath);
PDDocument document = PDDocument.load(file);

PDPage page = document.getPage(0);
PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true);

int stampFontSize = grailsApplication.config.pdfStamp.stampFontSize ? grailsApplication.config.pdfStamp.stampFontSize : 20
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, stampFontSize);

int leftOffset = grailsApplication.config.pdfStamp.leftOffset ? grailsApplication.config.pdfStamp.leftOffset : 10
int bottomOffset = grailsApplication.config.pdfStamp.bottomOffset ? grailsApplication.config.pdfStamp.bottomOffset : 20
contentStream.moveTextPositionByAmount(grailsApplication.config.xMove,grailsApplication.config.yMove)
contentStream.newLineAtOffset(leftOffset, bottomOffset)

String text = "i have added this line...!!!!";
contentStream.showText(text);
contentStream.endText();

contentStream.close();

document.save(new File(filePath));
document.close();

byte[] pdfData;
pdfData = Files.readAllBytes(file.toPath());
return pdfData;

i tried using moveTextPositionByAmount method but this does not seem to have any effect on text. why is my text reversed and how can i set it to correct orientation.

Please see the image of pdf output

like image 380
GJAIN Avatar asked Oct 25 '17 04:10

GJAIN


People also ask

Why is my PDF file backwards?

Under "Print Range," check the box that says "Reverse Pages." Power PDF automatically reverses all pages for printing without changing the document itself. Set your other printer settings, then print as usual.

How do I change text direction in PDF?

Open a PDF and then choose Tools > Edit PDF > Add text. Drag to define the width of the text block you want to add. For vertical text, right-click the text box, and choose Make Text Direction Vertical.

Why does PDF text look weird?

If you save a report in the PDF format, and the fonts look weird in the saved file - this may be caused by rare peculiarity local to your system. Basically, it's triggered by the absence (or manual removal) of the most widely-used Arial Font Family (causing the weird fonts to substitute it upon generating PDFs).


1 Answers

Your code is not causing the mirrored output by itself, so the cause must be inside the PDF you are stamping. Unfortunately you did not provide the PDF in question, so we have to guess here.

Most likely the issue is caused by the pre-existing page content having set the current transformation matrix to a mirroring affine transformation without resetting it at the end.

If that indeed is the case, PDFBox provides an easy work-around:

You construct your PDPageContentStream like this:

PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true);

There is another constructor accepting an additional boolean argument. If you use that constructor setting the additional argument to true, PDFBox attempts to reset the graphics state of the content:

PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true,true);

Beware: If this indeed fixes the issue, the coordinates and offsets you currently use rely on the transformation matrix being changed as it is. In that case you will have to update them accordingly.


Alternatively introducing a counter-mirroring may help, e.g. by setting the text matrix like this at the start of each of your text objects:

contentStream.beginText();
contentStream.setTextMatrix(new Matrix(1f, 0f, 0f, -1f, 0f, 0f));

Thereafter all y coordinate changes need to be negated, in particular the second argument of contentStream.moveTextPositionByAmount and contentStream.newLineAtOffset.

(By the way, moveTextPositionByAmount and newLineAtOffset do the same, the former merely is the deprecated variant, so you might want to use the latter in both cases.)

like image 107
mkl Avatar answered Sep 24 '22 14:09

mkl