Reputation: 66
When I create a new pdf document and add paragraph, it is OK. When I copy sourcePDF to a new PDF document and after that add a new paragraph, it is flipped. I know that if I create a new pdf doc with text and merge with dest documents, it will be ok, but I hope there is simple solution. So here is my code.
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.BaseDirection;
import com.itextpdf.text.DocumentException;
public void copyPdfToPdf(String text, String inputPdf, String outputPdf) throws IOException, DocumentException {
PdfDocument pdfDocInput = new PdfDocument(new PdfReader(inputPdf));
Document docInput = new Document(pdfDocInput);
PdfDocument pdfDocOutput = new PdfDocument(new PdfWriter(outputPdf));
Document docOutput = new Document(pdfDocOutput);
pdfDocInput.copyPagesTo(1, pdfDocInput.getNumberOfPages(), pdfDocOutput);
Paragraph p = new Paragraph().add(text);
docOutput.add(p);
docOutput.close();
docInput.close();
}
How to change document transformation to write a new paragraf in right direction not flipped
I find out this
Adding page number text to pdf copy gets flipped/mirrored with itext 7
but I did not resolve a problem.
Upvotes: 0
Views: 117
Reputation: 66
Instead pdfDocInput.copyPagesTo... I simple create a new PdfDocument(reader, writer) After that I add paragraph at fixed position.
PdfReader reader = new PdfReader(inputPdf);
PdfWriter writer = new PdfWriter(outputPdf);
PdfDocument pdfDocument = new PdfDocument(reader, writer);
Document docOutput = new Document(pdfDocument);
File file = ResourceUtils.getFile("classpath:fonts/FreeSans.ttf");
String FONT = file.getAbsolutePath();
PdfFont freeUnicode = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
freeUnicode.getFontProgram();
PdfPage newPage = pdfDocument.getFirstPage();
int numberOfPages = pdfDocument.getNumberOfPages();
Rectangle pageSize = newPage.getCropBox();
Paragraph p = new Paragraph()
.setFixedPosition(numberOfPages, pageSize.getWidth()/2, 50, pageSize.getWidth()/2)
.setFont(freeUnicode).setFontSize(9)
.add(tekst);
docOutput.add(p);
pdfDocument.close();
docOutput.close();
Upvotes: 0
Reputation: 807
It seems you are creating the document randomly without establishing specific settings, characteristics, or properties. This usually results in random results instead of consistent ones.
You have two options.
Please note the assumptions.
Sources:
Upvotes: 0