Does the setMargins method in iText works?
Asked Answered
D

4

11

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.

Dysgenics answered 10/12, 2011 at 0:31 Comment(0)
M
19

setMargins method works only before:

document.open();
Meatus answered 15/6, 2012 at 14:34 Comment(0)
C
1

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));
Chlorpromazine answered 8/10, 2012 at 12:37 Comment(0)
L
1

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.

Langobard answered 19/6, 2014 at 18:16 Comment(0)
S
-1

http://itext.2136553.n4.nabble.com/Adding-extra-margins-to-existing-PDF-Using-5-0-4-td3305175.html

using the above sniplet of code worked for me.

here's my version.

import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Utilities;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class PngToPdf2 {

    private static float points15mm = Utilities.millimetersToPoints(15);

    public static void main(String arg[]) throws Exception {
        File folder = new File("C:\\Users\\mamabile\\Desktop\\print-bulk-5");
        String outputFile = "output.pdf";
        List<String> files = new ArrayList<String>();
        files.add("newimage.png");
        //files.add("page2.jpg");

        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document, new FileOutputStream(new File(folder, outputFile)));

        float maxwidth = document.getPageSize().getWidth() - points15mm * 2;

        document.open();
        for (String f : files) {

            document.newPage();
            Image image = Image.getInstance(new File(folder, f).getAbsolutePath());
            image.scaleToFit(maxwidth, PageSize.A4.getHeight());
            image.setAbsolutePosition(points15mm, points15mm);
            //image.setRotationDegrees(reader.getPageRotation(i)==270?90:0);
            image.setCompressionLevel(9);
            image.simplifyColorspace();
            document.add(image);
        }
        document.close();
   }
}
Someday answered 27/9, 2019 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.