简体   繁体   中英

How to remove top padding in pdf

I am creating PDF file in my android application and printing that PDF file with my Wi-Fi printer. Below is my code for creating pdf file.

private void createPDFFile(String path) {
        if (new File(path).exists())
            new File(path).delete();
        try {
            Document document = new Document();

            //save
            PdfWriter.getInstance(document, new FileOutputStream(path));

            //Open to write
            document.open();

            //setting
            Rectangle pagesize = new Rectangle(612, 864);
            document.setPageSize(pagesize);
            document.addCreationDate();
            document.addAuthor("Securevs");
            document.addCreator("Moin Khan");
            document.setMargins(0, 0, 0, 0);


            //Font Setting
            BaseColor coloAccent = new BaseColor(0, 0, 0, 68);
            float fontSize = 21.0f;
            float valueFontSize = 26.0f;

            // Custom Font
            //BaseFont fontName =  BaseFont.createFont("assets/fonts/brandon_medium.otf", "UTF-8", BaseFont.EMBEDDED);
            BaseFont fontName = BaseFont.createFont("assets/fonts/KohinoorDevanagari-Bold.ttf", "UTF-8", BaseFont.EMBEDDED);

            //Create Title Of Doc
            Font title = new Font(fontName, 40.0f, Font.BOLD, BaseColor.BLACK);
            addNewItem(document, txtName.getText().toString(), Element.ALIGN_CENTER, title);

            document.add(new Paragraph("\n"));
            //Add more
            Font orderNumberFont = new Font(fontName, fontSize, Font.BOLD, BaseColor.BLACK);
            //addNewItem(document, "Order Number:", Element.ALIGN_LEFT, orderNumberFont);

            BitmapDrawable drawable = (BitmapDrawable) imgUserImage.getDrawable();
            Bitmap bm = drawable.getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            Image myImg = Image.getInstance(stream.toByteArray());
            myImg.scaleAbsolute(125, 174);
            myImg.setAlignment(Image.LEFT);
            //document.add(myImg);


            BitmapDrawable drawableQR = (BitmapDrawable) imgQrCode.getDrawable();
            Bitmap bmQR = drawableQR.getBitmap();
            ByteArrayOutputStream streamQR = new ByteArrayOutputStream();
            bmQR.compress(Bitmap.CompressFormat.JPEG, 100, streamQR);
            Image myImgQR = Image.getInstance(streamQR.toByteArray());
            myImgQR.scaleAbsolute(200, 200);
            myImgQR.setAlignment(Image.RIGHT);
            //document.add(myImgQR);

            addNewImageWithLeftAndRight(document, myImg, myImgQR);

           /* PdfPTable table = new PdfPTable(2);
            table.setSpacingBefore(20);
            table.setWidthPercentage(100);
            table.setWidths(new int[]{1, 2});
            table.addCell(createImageCell(myImg));
            table.addCell(createImageCell(myImgQR));
            document.add(table);*/
            /*Font orderNumberValueFont = new Font(fontName, valueFontSize, Font.NORMAL, BaseColor.BLACK);
            addNewItem(document, "#717171", Element.ALIGN_LEFT, orderNumberValueFont);

            addLineSeperator(document);

            addNewItem(document, "Order Date", Element.ALIGN_LEFT, orderNumberFont);
            addNewItem(document, "3/8/2010", Element.ALIGN_LEFT, orderNumberValueFont);

            addLineSeperator(document);*/

            addNewItemCenter(document, "Visiting to : " + txtEmployeeName.getText().toString(), orderNumberFont);
            addNewItemCenter(document, "Date and time : " + txtDateAndTime.getText().toString(), orderNumberFont);
            addNewItemCenter(document, "Purpose of visit : " + txtPurpose.getText().toString(), orderNumberFont);
            addNewItemCenter(document, "Visiting from : " + txtCompany.getText().toString(), orderNumberFont);

            addNewItemWithLeftAndRight(document, "Out time:................", "................", orderNumberFont, orderNumberFont);

            addNewItemWithLeftAndRight(document, "", "Signature", orderNumberFont, orderNumberFont);

            //addNewItemRight(document, "Signature", orderNumberFont);

            document.close();

            Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();

            printPdf();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

My paper size is 11.2 * 9.2 CM. Now I want to shift my content little bit above. I am selecting paper size 4*6 in while printing. Please suggest how can I achieve this.

I used to do this except I would scale the image so that it was always centered if the image was small and if the image was larger I would scale it horizontally or vertically then place it in the center of the page.

This is what you need, You need to find the X and Y you want. X and Y will be the position that will start drawing on the page

image.setAbsolutePosition(x, y)

You can refer to the code and customize it to suit your wishes.

fun createPdf () {    
    val rectText = Rectangle()
    rectText.width = 9.5 * 72.0
    rectText.height = 12 * 72.0
    val doc = Document(com.itextpdf.text.Rectangle(rectText), 0f, 0f, 0f, 0f)

    val image: Image = Image.getInstance(bitmap)
    val scaleImage = scaleImage(doc, image)
    //Scale image fit height or width
    image.scalePercent(scaleImage.second)

    //Find a place to start drawing pictures on the page
    val absolutePosition = findPositionToPutImgIntoPage(scaleImage.first, doc, image)

    image.setAbsolutePosition(absolutePosition.first,absolutePosition.second)
    doc.add(image)
    doc.newPage()
}

//calculate to know whether to scale the image horizontally or vertically
private fun scaleImage(doc: Document, image: Image): Pair<Boolean, Float> {
    var isScaleWidth = false
    val scaleWidth: Float =
        (doc.pageSize.width - doc.leftMargin() - doc.rightMargin()) / image.width * 100
    val scaleHeight: Float =
        (doc.pageSize.height - doc.bottomMargin() - doc.topMargin()) / image.height * 100
    val scale = if (scaleHeight > scaleWidth) {
        isScaleWidth = true
        scaleWidth
    } else {
        scaleHeight
    }
    return Pair(isScaleWidth, scale)
}

private fun findPositionToPutImgIntoPage(
    isScaleWidth: Boolean,
    doc: Document,
    image: Image
): Pair<Float, Float> {
    return if (isScaleWidth) {
        val y = (doc.pageSize.height - image.scaledHeight) / 2
        Pair(0f, y)
    } else {
        val x = (doc.pageSize.width - image.scaledWidth) / 2
        Pair(x, 0f)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM