【问题标题】:How to prevent the resizing of pages in PDF?如何防止在 PDF 中调整页面大小?
【发布时间】: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版本!

标签: pdf itext


【解决方案1】:

请查看PrintPreferences 示例。在此示例中,解释了几个打印首选项。对您很重要的行将确保页面在打印时不会被缩放:

writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);

这是您可以设置以影响打印设置的唯一参数。您不能告诉查看者它需要使页面适合打印机页面的大小。您只能通过将PRINTSCALING 设置为NONE 来指示查看器需要采用实际尺寸。

如果你想要一些其他的行为,你要求的东西在 PDF 中是不可能的。

【讨论】:

  • 很遗憾,writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE); 不起作用。与实际大小相比,适合页面的页边距存在一些边距,并且内容的位置彼此不同。
  • @CristiB。 - 根据我设置上述属性的短暂经验,Adobe Reader 成功读取了此属性并在打印对话框中设置了“实际页面”。另一方面,Chrome 的 PDF 查看器似乎忽略了它,并将其“适合页面”设置设置为之前打印作业中设置的任何设置。
猜你喜欢
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 2016-05-02
  • 2010-12-08
  • 1970-01-01
相关资源
最近更新 更多