0

sorry,you may not understand my problems,because i am not good at english. i want to add some labels at the top and bottom of pdf,but the label position can set minus.if i set a minus,i should make heigh larger to set label. i got help from How to resize existing pdf page size to change my pdf pagesize.then i encountered another problem,when i set lly as a
minus, my text was truncated,then i want to add bottom length to top, but i do not know how to move the text up to make the text in center.

key codes

float newTop = rectangle.getTop();
                    if (printSet.getHeaderMargins() < 0) {
                        newTop += height2Offset(PrintSet.defaultMargins - printSet.getHeaderMargins());
                        headMargins = height2Offset(PrintSet.defaultMargins);
                    }
                    if (printSet.getFooterMargins() < 0) {
                        newTop += height2Offset(PrintSet.defaultMargins - printSet.getFooterMargins());
                        footMargins = height2Offset(PrintSet.defaultMargins);
                    }
                    float[] newBoxValues = new float[] {
                            rectangle.getLeft(),
                            rectangle.getBottom(),
                            rectangle.getRight(),
                            newTop
                    };
                    PdfArray newBox = new PdfArray(newBoxValues);
                    PdfDictionary pageDict = reader.getPageN(page + 1);
                    pageDict.put(PdfName.CROPBOX, newBox);
                    pageDict.put(PdfName.MEDIABOX, newBox);

enter image description here

Community
  • 1
  • 1
王佳明
  • 3
  • 2
  • i spend much time drawing a beautiful picture to describe my quesiton!oh,so powerful – 王佳明 Nov 18 '16 at 08:45
  • *"then i encountered another problem,when i set lly as a minus, my text was truncated"* - Thus, this is your actual problem and should be the focus of your question because *"to add bottom length to top"* and *"to move the text up to make the text in center"* is an unnecessarily complicated way to solve that issue. – mkl Nov 18 '16 at 10:10

1 Answers1

0

Currently, you are defining a new page size like this:

float[] newBoxValues = new float[] {
    rectangle.getLeft(),
    rectangle.getBottom(),
    rectangle.getRight(),
    newTop
};

This creates a bigger rectangle, but that rectangle only expands the page towards the top.

I think you should create the new rectangle like this:

float[] newBoxValues = new float[] {
    rectangle.getLeft(),
    rectangle.getBottom() - extramarginBottom,
    rectangle.getRight(),
    rectangle.getTop() + extramarginTop
};

I can't help you define the value of extramarginBottom and extramarginTop because I'm not sure what your height2Offset() method is supposed to do, nor what PrintSet.defaultMargins, printSet.getHeaderMargins(), and printSet.getFooterMargins() are about.

Basically, extramarginTop is the height to be added at the top, whereas extramarginBottom is the height to be added at the bottom:

enter image description here

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I have resolved the question by your method.don't care about the height2Offset() method,it just change the page real length to pixels,like the A4 page real length 210mm×297mm to 841×594.thanks for you answer. – 王佳明 Nov 18 '16 at 12:26