我正在尝试这样做:
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
BufferedReader br = new BufferedReader(new FileReader(SRC));
String line;
for(int i = 1; i<5;i++)
{
if(n%2==0)
{
pdfDoc.addNewPage();
doc.setLeftMargin(100);
}
else if(!(n%2==0))
{
pdfDoc.addNewPage();
doc.setLeftMargin(50);
}
}
while ((line = br.readLine()) != null) {
document.add(new Paragraph(line));
}
document.close();
但是它不起作用! 每个页边距都需要一个不同的页面。 同时,我用.txt
阅读了非常大的文字答案 0 :(得分:2)
在itext 7中尝试以下操作:
PdfWriter writer = new PdfWriter("111");
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc);
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
pdfDoc.addNewPage();
document.setLeftMargin(100);
document.add(new Paragraph("AAA"));
document.add(new AreaBreak());
} else {
pdfDoc.addNewPage();
document.setLeftMargin(50);
document.add(new Paragraph("SSS"));
document.add(new AreaBreak());
}
}
document.close();
这样,所有偶数页(2、4、6等)的左边距为100,而所有不均匀页的左边界为50(1、3、5等)。
答案 1 :(得分:0)
我这样做了:
PdfDocument srcDoc = new PdfDocument(new PdfReader(DEST1));
Rectangle rect = srcDoc.getFirstPage().getPageSize();
Rectangle pageSize = new Rectangle(rect.getWidth(), rect.getHeight());
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.setDefaultPageSize(new PageSize(pageSize));
PdfCanvas content = new PdfCanvas(pdfDoc.addNewPage());
int n = 0;
for (int i = 1; i <= srcDoc.getNumberOfPages(); i++) {
PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(pdfDoc);
content.clip();
content.newPath();
x++;
if(n%2==0)
content.addXObject(page,0,0);//here margin which is installed in DEST1
else if(!(n%2==0))
content.addXObject(page,150,0);
content = new PdfCanvas(pdfDoc.addNewPage());
n++;
}
srcDoc.close();
pdfDoc.close();
}