android制作pdf

ldxq2e6h  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(369)

我有一个android应用程序,它从具有各种视图的界面获取用户输入。我使用这些输入作为字符串,并根据我准备的模板,借助canvas object的draw方法将它们打印到pdf上。这些工作很好,但我遇到了一些问题。字符串中的一些字母要么在其他字母之间有太多的空间,要么空间非常小,从而导致字母之间的冲突。它使pdf文件的视觉效果很差。我使用了paint object的“setletterspacing”方法来设置字母之间的间距长度,但它不起作用。我怎样才能使所有的字母有相同的间距,而整个文本看起来不错?

private void makePDF(){
    PdfDocument document=new PdfDocument();
    Paint paint=new Paint();

    PdfDocument.PageInfo myPageInfo=new PdfDocument.PageInfo.Builder(250,400,1).create();
    PdfDocument.Page myPage=document.startPage(myPageInfo);

    Canvas canvas=myPage.getCanvas();
    Rect textBounds = new Rect();

    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(10);
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    canvas.drawText("İSG RİSK TESPİT VE DÖF FORMU",myPageInfo.getPageWidth()/2,8,paint);
    paint.setTypeface(Typeface.create("Arial", Typeface.NORMAL));

    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(1);
    canvas.drawRect(1,20,(myPageInfo.getPageWidth())-1,(myPageInfo.getPageHeight()/2)-50,paint);
    canvas.drawLine(myPageInfo.getPageWidth()/5,20,myPageInfo.getPageWidth()/5,44,paint);
    canvas.drawLine(7*myPageInfo.getPageWidth()/10,20,7*myPageInfo.getPageWidth()/10,44,paint);

    paint.setStrokeWidth(0);
    paint.setStyle(Paint.Style.FILL);

    String[] blockKısım={"Tespit Tarihi","Uygunsuz lukYeri","Etkilenen Kişi/Grup","Alınmış Önlem"};
    String[] blockCevap={btnCallendar.getText().toString(),edUygunsuz.getText().toString(),edEtkilenen.getText().toString(),edAlinmisOnlem.getText().toString()};
    paint.setTextAlign(Paint.Align.LEFT);
    paint.setTextSize(3f);
    //paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL));

    int startX=2;
    int startY=22;
    int endX=7*myPageInfo.getPageWidth()/10;
    for (int i=0;i<4;i++){

       // paint.setColor(ResourcesCompat.getColor(getResources(), R.color.gray, null));

        canvas.drawText(blockKısım[i],startX,startY+3,paint);
        //paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
        canvas.drawText(blockCevap[i],(myPageInfo.getPageWidth()/5)+2,startY+3,paint);
        //paint.setColor(ResourcesCompat.getColor(getResources(), R.color.black, null));
        canvas.drawLine(startX,startY+4,endX,startY+4,paint);
        startY+=5;
    }

    canvas.drawText("Risk Derecesi",(myPageInfo.getPageWidth()-(2*myPageInfo.getPageWidth()/10)),25,paint);
    canvas.drawLine(7*myPageInfo.getPageWidth()/10,26,myPageInfo.getPageWidth()-1,26,paint);

    String[] strRısk={"Olasılık","Frekans","Şiddet","Risk"};
    String[] strRıskUserr={spOlasilik.getSelectedItem().toString(),spFrekans.getSelectedItem().toString(),spSiddet.getSelectedItem().toString(),
            (Double.parseDouble(spFrekans.getSelectedItem().toString())*Double.parseDouble(spOlasilik.getSelectedItem().toString())
                    *Double.parseDouble(spSiddet.getSelectedItem().toString()))+""};

    int startLeft=(7*myPageInfo.getPageWidth()/10)+2;

    for (int i=0;i<4;i++){

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(0);
        canvas.drawRect(startLeft,28,startLeft+15,43,paint);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(0);
        paint.setTextSize(2f);
        canvas.drawText(strRısk[i],startLeft ,31,paint);
        paint.setTextSize(5);
        canvas.drawText(strRıskUserr[i],startLeft+5 ,38,paint);
        paint.setTextSize(3f);

        canvas.drawLine(startLeft,32,startLeft+15,32,paint);

        startLeft+=18;

    }

    canvas.drawText("X",(7*myPageInfo.getPageWidth()/10)+18 ,38,paint);
    canvas.drawText("X",(7*myPageInfo.getPageWidth()/10)+36 ,38,paint);
    canvas.drawText("=",(7*myPageInfo.getPageWidth()/10)+54 ,38,paint);
    //paint.settext
    canvas.drawLine(1,44,myPageInfo.getPageWidth()-1,44,paint);
    canvas.drawText("Tehlike Açıklaması",(myPageInfo.getPageWidth()/6)+5 ,49,paint);
    TextPaint mTextPaint=new TextPaint();
    mTextPaint.setTextSize(3);
    mTextPaint.setLetterSpacing(0.15f);
    StaticLayout mTextLayout = new StaticLayout(edAciklamaTehlike.getText().toString(),
            mTextPaint, (canvas.getWidth()/2)-10, Layout.Alignment.ALIGN_NORMAL, 3f, 0.0f, false);
    canvas.save();
    startX = 2;
    startY = 56;
    canvas.translate(startX, startY);
    mTextLayout.draw(canvas);
    canvas.restore();

    canvas.drawText("Fotoğraf",(4*myPageInfo.getPageWidth()/6)+15 ,49,paint);
    canvas.drawLine(1,52,myPageInfo.getPageWidth()-1,52,paint);
    canvas.drawLine(3*myPageInfo.getPageWidth()/6 ,44,3*myPageInfo.getPageWidth()/6,(myPageInfo.getPageHeight()/2)-50,paint);

    if (bitmap!=null){

        Bitmap scaledbitmap3=getResizedBitmap(bitmap,100,100);
        canvas.drawBitmap(scaledbitmap3,0,0,paint);

    }

    canvas.drawLine(1,(((myPageInfo.getPageHeight()/2)-44)/2)+19,myPageInfo.getPageWidth()/2,(((myPageInfo.getPageHeight()/2)-44)/2)+19,paint);
    canvas.drawText("Risk Açıklaması",(myPageInfo.getPageWidth()/6)+5 ,(((myPageInfo.getPageHeight()/2)-44)/2)+24,paint);
    mTextLayout = new StaticLayout(edAciklamaRisk.getText().toString(),
            mTextPaint, (canvas.getWidth()/2)-10, Layout.Alignment.ALIGN_NORMAL, 3f, 0.0f, false);
    canvas.save();
    startX = 2;
    startY = (((myPageInfo.getPageHeight()/2)-44)/2)+30;
    canvas.translate(startX, startY);
    mTextLayout.draw(canvas);
    canvas.restore();
    canvas.drawLine(1,(((myPageInfo.getPageHeight()/2)-44)/2)+27,myPageInfo.getPageWidth()/2,(((myPageInfo.getPageHeight()/2)-44)/2)+27,paint);

    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(8);
    paint.setLetterSpacing(0.1f);
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    canvas.drawText("DÜZELTİCİ ÖNLEYİCİ FALİYET",myPageInfo.getPageWidth()/2,(myPageInfo.getPageHeight()/2)-34,paint);
    paint.setTypeface(Typeface.create("Arial", Typeface.NORMAL));
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(1);
    canvas.drawRect(1,(myPageInfo.getPageHeight()/2)-30,(myPageInfo.getPageWidth())-1,myPageInfo.getPageHeight()-30,paint);
    paint.setStrokeWidth(0);
    canvas.drawRect(5,(myPageInfo.getPageHeight()/2)-25,12,(myPageInfo.getPageHeight()/2)-18,paint);
    canvas.drawRect((myPageInfo.getPageWidth()/2)+5,(myPageInfo.getPageHeight()/2)-25,(myPageInfo.getPageWidth()/2)+12,(myPageInfo.getPageHeight()/2)-18,paint);
    paint.setStyle(Paint.Style.FILL);
    paint.setTextSize(3f);
    paint.setTextAlign(Paint.Align.LEFT);
    //canvas.save();
    //canvas.scale(1f, 1f);
    canvas.drawText("Düzeltici Faaliyet/Meydana gelmiş bir uygunsuzluk için",13,(myPageInfo.getPageHeight()/2)-20,paint);
    canvas.drawText("Önleyici Faaliyet/Meydana gelebilecek bir uygunsuzluk için",(myPageInfo.getPageWidth()/2)+13,(myPageInfo.getPageHeight()/2)-20,paint);
    paint.setTextSize(4);
    if (chDuzeltici.isChecked())
        canvas.drawText("X",6,(myPageInfo.getPageHeight()/2)-20,paint);
    if (chOnleyici.isChecked())
        canvas.drawText("X",(myPageInfo.getPageWidth()/2)+6,(myPageInfo.getPageHeight()/2)-20,paint);

    canvas.drawLine(1,(myPageInfo.getPageHeight()/2)-17,myPageInfo.getPageWidth()-1,(myPageInfo.getPageHeight()/2)-17,paint);
    canvas.drawLine(1,(myPageInfo.getPageHeight()/2)-8,myPageInfo.getPageWidth()-1,(myPageInfo.getPageHeight()/2)-8,paint);

    canvas.drawText("Alınması Gereken Önlemler",(6*myPageInfo.getPageWidth()/20)+10,(myPageInfo.getPageHeight()/2)-10,paint);
    mTextLayout = new StaticLayout(edAlinmasiGerekenOnlem.getText().toString(),
            mTextPaint, (17*myPageInfo.getPageWidth()/20), Layout.Alignment.ALIGN_NORMAL, 3f, 0.0f, false);
    canvas.save();
    startX = 2;
    startY =(myPageInfo.getPageHeight()/2)-7;
    canvas.translate(startX, startY);
    mTextLayout.draw(canvas);
    canvas.restore();

    canvas.drawLine((17*myPageInfo.getPageWidth()/20)+4,(myPageInfo.getPageHeight()/2)-8,(17*myPageInfo.getPageWidth()/20)+4,
            (myPageInfo.getPageHeight()/2)-8+(myPageInfo.getPageHeight()/8),paint);
    canvas.drawLine((17*myPageInfo.getPageWidth()/20)+10,(myPageInfo.getPageHeight()/2)-8,(17*myPageInfo.getPageWidth()/20)+10,
            (myPageInfo.getPageHeight()/2)-8+(myPageInfo.getPageHeight()/8),paint);

    canvas.drawLine(1,(myPageInfo.getPageHeight()/2)-8+(myPageInfo.getPageHeight()/8),myPageInfo.getPageWidth()-1,
            (myPageInfo.getPageHeight()/2)-8+(myPageInfo.getPageHeight()/8),paint);
    canvas.drawLine(1,(myPageInfo.getPageHeight()/2)+1+(myPageInfo.getPageHeight()/8),myPageInfo.getPageWidth()-1,
            (myPageInfo.getPageHeight()/2)+1+(myPageInfo.getPageHeight()/8),paint);
    canvas.drawText("Uygunsuzluğun Giderilmesi İçin Yapılan Çalışmalar",5*myPageInfo.getPageWidth()/20,(myPageInfo.getPageHeight()/2)-2+(myPageInfo.getPageHeight()/8),paint);

    mTextLayout = new StaticLayout(edUygunsuzlugunGiderilmesi.getText().toString(),
            mTextPaint, (17*myPageInfo.getPageWidth()/20), Layout.Alignment.ALIGN_NORMAL, 3f, 0.0f, false);
    canvas.save();
    startX = 2;
    startY =(myPageInfo.getPageHeight()/2)+1+(myPageInfo.getPageHeight()/8)+1;
    canvas.translate(startX, startY);
    mTextLayout.draw(canvas);
    canvas.restore();

    canvas.drawLine((17*myPageInfo.getPageWidth()/20)+4,(myPageInfo.getPageHeight()/2)+1+(myPageInfo.getPageHeight()/8),(17*myPageInfo.getPageWidth()/20)+4,
            (myPageInfo.getPageHeight()/2)+1+(2*myPageInfo.getPageHeight()/8),paint);
    canvas.drawLine((17*myPageInfo.getPageWidth()/20)+10,(myPageInfo.getPageHeight()/2)+1+(myPageInfo.getPageHeight()/8),(17*myPageInfo.getPageWidth()/20)+10,
            (myPageInfo.getPageHeight()/2)+1+(2*myPageInfo.getPageHeight()/8),paint);
    canvas.drawLine(1,(myPageInfo.getPageHeight()/2)+1+(2*myPageInfo.getPageHeight()/8),myPageInfo.getPageWidth()-1,
            (myPageInfo.getPageHeight()/2)+1+(2*myPageInfo.getPageHeight()/8),paint);

    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(0);
    canvas.drawRect(5,(myPageInfo.getPageHeight()/2)+6+(2*myPageInfo.getPageHeight()/8),12,(myPageInfo.getPageHeight()/2)+13+(2*myPageInfo.getPageHeight()/8),paint);
    canvas.drawRect((myPageInfo.getPageWidth()/2)+5,(myPageInfo.getPageHeight()/2)+6+(2*myPageInfo.getPageHeight()/8),(myPageInfo.getPageWidth()/2)+12,
            (myPageInfo.getPageHeight()/2)+13+(2*myPageInfo.getPageHeight()/8),paint);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawText("Uygunsuzluk Giderildi",13,(myPageInfo.getPageHeight()/2)+10+(2*myPageInfo.getPageHeight()/8),paint);
    canvas.drawText("Uygunsuzluk Giderilmedi",(myPageInfo.getPageWidth()/2)+13,(myPageInfo.getPageHeight()/2)+10+(2*myPageInfo.getPageHeight()/8),paint);
    paint.setTextSize(4);
    if (chGiderildi.isChecked())
        canvas.drawText("X",6,(myPageInfo.getPageHeight()/2)+10+(2*myPageInfo.getPageHeight()/8),paint);
    else
        canvas.drawText("X",(myPageInfo.getPageWidth()/2)+6,(myPageInfo.getPageHeight()/2)+10+(2*myPageInfo.getPageHeight()/8),paint);
    paint.setTextSize(3f);

    canvas.drawLine(1,(myPageInfo.getPageHeight()/2)+18+(2*myPageInfo.getPageHeight()/8),myPageInfo.getPageWidth()-1,
            (myPageInfo.getPageHeight()/2)+18+(2*myPageInfo.getPageHeight()/8),paint);
    canvas.drawText("            Çalışmayı Yapan                                                      İş Güvenliği Uzmanı                " +
                    "                            Kontrol Eden Ve Onaylayan",13,
            (myPageInfo.getPageHeight()/2)+25+(2*myPageInfo.getPageHeight()/8),paint);
    canvas.drawLine(myPageInfo.getPageWidth()/3,(myPageInfo.getPageHeight()/2)+18+(2*myPageInfo.getPageHeight()/8),myPageInfo.getPageWidth()/3,
            myPageInfo.getPageHeight()-30,paint);
    canvas.drawLine(2*myPageInfo.getPageWidth()/3,(myPageInfo.getPageHeight()/2)+18+(2*myPageInfo.getPageHeight()/8),2*myPageInfo.getPageWidth()/3,
            myPageInfo.getPageHeight()-30,paint);
    canvas.drawLine(1,(myPageInfo.getPageHeight()/2)+30+(2*myPageInfo.getPageHeight()/8),myPageInfo.getPageWidth()-1,
            (myPageInfo.getPageHeight()/2)+30+(2*myPageInfo.getPageHeight()/8),paint);
        paint.setTextSize(2f);
    canvas.drawText("             Ad, Soyad, imza                                                                        Ad, Soyad, imza    " +
            "                                                                    Ad, Soyad, imza",20,(myPageInfo.getPageHeight()/2)+35+(2*myPageInfo.getPageHeight()/8),paint);
    paint.setTextSize(5f);
    canvas.drawText("YS-ISG-017",1,myPageInfo.getPageHeight()-4,paint);

    Calendar calendar=Calendar.getInstance();
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    String date = dateFormat.format(calendar.getTime());
    canvas.drawText("YT-"+date,16*myPageInfo.getPageWidth()/20,myPageInfo.getPageHeight()-4,paint);

    paint.setTextSize(3f);
    canvas.save();
    canvas.rotate(-90);
    canvas.drawText("Termin Tarihi",-228,220,paint);
    canvas.drawText("Tamamlama",-284,220,paint);
    canvas.restore();

    document.finishPage(myPage);

    File file=new File (Environment.getExternalStorageDirectory(),"/Document.pdf");

    try {
        document.writeTo(new FileOutputStream(file));
    }catch (IOException e) {
        e.printStackTrace();
    }

    document.close();
    Toast.makeText(getApplicationContext(),"PDF OLUŞTURULDU",Toast.LENGTH_LONG).show();
}

在此处输入图像描述

pw136qt2

pw136qt21#

打开要另存为pdf的文件,然后轻按平板电脑上的文件或轻按文件图标。在你的手机上。在“文件”菜单上,轻按“打印”。如果尚未选中,请在平板电脑上轻按microsoft print to pdf或在手机上轻按pdf。点击打印。在平板电脑上,将显示“输出另存为”页面。

相关问题