Does the setMargins
method in iText work in a conventional manner?
I have tried the following:
System.out.println(f.exists());
Document document = new Document(PageSize.A4,36,36,36,36);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f.getAbsolutePath()+"0.pdf"));
document.open();
//reader
PdfReader reader = new PdfReader(new FileInputStream(f));
PdfContentByte cb = writer.getDirectContent();
for(int i = 1 ; i < reader.getNumberOfPages()+1 ; i++)
{
System.out.println(f.getName()+" "+i);
PdfImportedPage page = writer.getImportedPage(reader, i);
document.newPage();
cb.addTemplate(page,0,0);
}
// Add your new data / text here
// for example...
//document.add(new Paragraph("my timestamp"));
document.close();
However, I get the same output as my input. I'm wondering whether there is a special trick to getting the setMargins
method to work normally.
setMargins
method works only before:
document.open();
Margins can be changed after open()
, however, the changes only take effect after newPage()
.
Adding a PdfImportedPage
as an "image" honours the margins of the document.
Example:
PdfImportedPage page = writer.getImportedPage(reader, i);
document.add(Image.getInstance(page));
You can change left and top margins in the new pdf document by adding offset by calling
cb.addTemplate(page, leftMarginOffset,topMarginOffset);
Positive numbers will reduce margins and negative numbers increase them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With