I have pages in PDF document which when you look at in Acrobat reader are images (but when you look at them in the Adobe editor they are text with a background image).
These input PDF pages are getting processed using addTemplate method of PdfContentByte class of itext API.
The call cb.addTemplate(page, 1, 0, 0, 1, 0, 0) is not suppose to rotate the read page, but it does.
When you look at the resultant page, the pages with background image get rotated.
And it is only happening to the image pages, not to others. Do you know why?
Another thing observed is that irrespective of the orientation of the image in the "input.pdf", the resultant "output.pdf" document always is in one particular orientation. That means it is read that way by this line --->
PdfImportedPage page = writer.getImportedPage(reader, currentPageNumber++);
Here is the complete code which I have put so that it is easy to run(I have removed many details, to focus on the issue)
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class ItextPOC {
public static void createNewPdf(byte[] inputFile, OutputStream outputStream)
throws IOException, DocumentException {
PdfReader reader = new PdfReader(inputFile);
Document document = new Document(PageSize.A4);
HeaderFooter footer = new HeaderFooter(new Phrase(""), false);
footer.setBorder(Rectangle.NO_BORDER);
document.setFooter(footer);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
int currentPageNumber = 1;
int totalNumberOfPages = reader.getNumberOfPages();
while (currentPageNumber <= totalNumberOfPages) {
PdfImportedPage page = writer.getImportedPage(reader, currentPageNumber++);
cb.addTemplate(page, 1, 0, 0, 1, 0, 0);
document.newPage();
}
document.close();
}
public static void main(String[] args) throws Exception {
Path path = Paths.get("C:/temp/input.pdf");
byte[] inputFile = Files.readAllBytes(path);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
createNewPdf(inputFile, byteArrayOutputStream);
writeToFile(byteArrayOutputStream);
}
private static void writeToFile(ByteArrayOutputStream byteArrayOutputStream) {
FileOutputStream fos = null;
String filePath = "C:/temp/output.pdf";
try {
Path pathToFile = Paths.get(filePath);
Files.createDirectories(pathToFile.getParent());
fos = FileUtils.openOutputStream(new File(filePath));
byteArrayOutputStream.writeTo(fos);
fos.flush();
fos.close();
} catch (Exception ex) {
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException ex) {
}
}
}
}