javaopenpdf

OpenPDF margin mirroring set to true, but all pages have same margins


I am using OpenPDF 1.3.30 and trying to create a document with margin mirroring. I called document.setMarginMirroring(true) before opening the document, but I am still getting a document where all pages have the same margin.

Here is a screenshot of a test document showing the pages are all the same: output

Here is the code I used to generate it:

package openpdf;

import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;

public class MirrorMarginTest {
  public static void main(String[] args) 
  throws Exception {
    Rectangle pageSize = new Rectangle(400,650);
    Document doc = new Document(pageSize, 125, 25, 50, 50);
    doc.setMarginMirroring(true);
    FileOutputStream fos = new FileOutputStream(new File("C:\\Tmp\\test.pdf"));
    PdfWriter writer = PdfWriter.getInstance(doc, fos);
    doc.open();
    for( int i=0; i<4; i++) {
      doc.newPage();
      doc.add(new Paragraph("Test. Test. Test. Test. Test. Test. Test. Test. Test. Test. Test. Test. Test."));
    }    
    doc.close();
    fos.close();
    writer.close();
  }
}

What did I do wrong?


Solution

  • After playing with the code for a bit, I figured out a solution. The margins mirror as intended if I put the call to setMarginMirroring after the call to open:

        doc.open();
        doc.setMarginMirroring(true);
    
    

    I don't understand why it has to be this way, but it works.