【发布时间】:2014-06-06 14:29:18
【问题描述】:
我想要一个 pdf 文件,该文件在打印时实际大小与适合页面之间没有差异。 我尝试了以下示例,但不起作用,只有宽度将被锁定,而不是高度。 有什么解决办法吗?
package etiq;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class TestHeight {
public static void main(String[] args) {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
String path = "D:\\test.pdf";
FileOutputStream output = new FileOutputStream(path);
PdfWriter writer = PdfWriter.getInstance(document, output);
document.open();
PdfPTable table = new PdfPTable(2);
table.setTotalWidth(400);
table.setLockedWidth(true);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
table.getDefaultCell().setFixedHeight(70);
table.addCell("!");
table.addCell("Text Text Text Text Text Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text Text Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text \nText \nText ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text ");
table.addCell(new Phrase(new Chunk("cell")));
document.add(table);
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
}
【问题讨论】:
-
(1.) 查看导入并将第二部分 (lowagie) 与我的名字 (Bruno Lowagie) 进行比较。我的名字出现在您的代码中这一事实意味着您正在使用我曾经编写的软件,现在该软件被认为已过时。请参阅itextpdf.com/salesfaq 请升级。 (2) 您正在创建尺寸为 A4(8.267 x 11.692 英寸)的页面。您正在创建一个大小为 400 x 490(5.55 x 6.81 英寸)的表格。您的顶部边框为 50(0.69 英寸),左右边框为 97.5(1.35 英寸),底部边框为 352(4.88 英寸)。你的问题是什么?您是否要将
PrintScaling设置为None? -
如果我选择以实际尺寸或适合页面打印,我希望得到相同的输出。
-
你首先要升级你的iText版本!