3

I want to have a pdf file that contains no difference between actual size vs fit to page at printing. I tried with the following example, but not works, only the width will be locked, not the height. Is there any solution?

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();
  }
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Cristi B.
  • 651
  • 7
  • 17
  • (1.) Look at the imports and compare the second part (lowagie) with my name (Bruno Lowagie). The fact that my name is in your code means that you're using software I once wrote and that is now considered obsolete. See http://itextpdf.com/salesfaq Please upgrade. (2) You are creating a page with size A4 (8.267 x 11.692 inch). You are creating a table with of size 400 by 490 (5.55 x 6.81 inch). You have a top border of 50 (0.69 inch), left and right borders of 97.5 (1.35 inch) and a bottom border of 352 (4.88 inch). What is your question? Are you looking to set the `PrintScaling` to `None`? – Bruno Lowagie Jun 06 '14 at 15:03
  • 1
    I want to have the same output if I choose to print at Actual size or at Fit to page. – Cristi B. Jun 06 '14 at 15:05
  • You first want to upgrade your iText version! – Bruno Lowagie Jun 06 '14 at 15:08

1 Answers1

2

Please take a look at the PrintPreferences example. In this example, several print preferences are explained. The line that is important to you, will make sure that the page isn't scaled when printed:

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

This is the only parameter you can set to influence the print setting. You can't tell a viewer that it needs to fit the page to the size of the printer page. You can only instruct the viewer that the actual size needs to be taken, by setting the PRINTSCALING to NONE.

If you want some other behavior, you're asking for something that is impossible in PDF.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Unfortunately, `writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);` not works. There are some margins at Fit to page comparing with Actual size and the position of the content differs from one to each other. – Cristi B. Jun 07 '14 at 08:19
  • @CristiB. - from my short experience with setting the above property Adobe Reader successfully reads this property and sets "Actual Page" in the print dialog. Chrome's PDF viewer on the other hand seems to ignore it and set its "Fit To Page" setting to whatever it was set on the previous print job. – jaredbaszler Mar 23 '17 at 17:45