繁体   English   中英

如何删除 pdf 中的顶部填充

[英]How to remove top padding in pdf

我正在我的 android 应用程序中创建 PDF 文件,并使用我的 Wi-Fi 打印机打印该 PDF 文件。 下面是我创建 pdf 文件的代码。

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();
        }
    }

我的纸张尺寸是 11.2 * 9.2 厘米。 现在我想将我的内容稍微移到上面。 我在打印时选择了 4*6 英寸的纸张尺寸。 请建议我怎样才能做到这一点。

我曾经这样做,除了我会缩放图像,以便如果图像很小,它总是居中,如果图像较大,我会水平或垂直缩放它,然后将它放在页面的中心。

这就是你需要的,你需要找到你想要的 X 和 Y。 X 和 Y 将是 position 将开始在页面上绘制

image.setAbsolutePosition(x, y)

您可以参考代码并对其进行自定义以满足您的意愿。

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)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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