【发布时间】:2022-01-14 01:04:39
【问题描述】:
我正在我的 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 英寸的纸张尺寸。请建议我怎样才能做到这一点。
【问题讨论】: