0

When I print my pdf made with iText the resolution is so low. I tried set compress to increment quality with: pdfWritter.compressionLevel = 0 but no lucky. I look for better quality of the pdf to print. Actually only it has letters and it won't change.

CreatePDF.kt

val folder = File(context.externalCacheDir?.absolutePath.toString())
if (!folder.exists())   folder.mkdirs()
pdfFile = File(folder, "$fileName.pdf")
document = Document(PageSize.LETTER, 0F, 0F, 0F, 0F)
pdfWritter.compressionLevel = 0
pdfWritter = PdfWriter.getInstance(document,FileOutputStream(pdfFile))
document.open()
document.add(Paragraph("Dra. Mariana Castro", DefaultFont)
document.close()
val intent = Intent(context, ViewPDFActivity::class.java)
intent.putExtra("path",pdfFile.absolutePath)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)

ViewPDFActivity.kt

class ViewPDFActivity : AppCompatActivity() {

private lateinit var file:File

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_view_pdf)
    val bundle: Bundle? = intent.extras
    if(bundle != null){
        file = File(bundle.getString("path",""))
    }
    pdfViewAct.fromFile(file)
        .enableSwipe(true)
        .swipeHorizontal(false)
        .enableDoubletap(true)
        .enableAntialiasing(true)
        .load()
    printPDF.setOnClickListener {
        val v: View = pdfViewAct
        val bmp =
            Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888)
        val c = Canvas(bmp)
        v.draw(c)
        val photoPrinter = PrintHelper(this)
        photoPrinter.scaleMode = PrintHelper.SCALE_MODE_FIT
        if (bundle != null) {
            bundle.getString("path")?.let { it1 -> photoPrinter.printBitmap(it1, bmp) }
        }
    }
    backButton.setOnClickListener {
        finish()
    }
}

activity_view_pdf.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.subactivities.ViewPDFActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/printPDF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Imprimir" />

        <Space
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/backButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cm_button_back" />
    </LinearLayout>

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfViewAct"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.github.barteksc.pdfviewer.PDFView>
</LinearLayout>

The pdf created is this:PDF Result. The quality is poor even if I increase the page size with pageSize.A0.

Some idea of fix this? I've tried everything. Thanks in advance.

felipeac
  • 1
  • 1
  • this is very weird, as you are creating the Paragraph as text, but the document you have attached it has an image, so something is making that conversion (and the PDF document was apparently created by Microsoft Word 2013?) – André Lemos Aug 17 '20 at 09:12
  • Is very weird, in my project only use iTextpdf library to create the PDF document and my actual version is Microsoft Word 2013. Maybe there is a conflict with those processes. Thanks for the idea, I'll review this and try to solve this way. – felipeac Aug 18 '20 at 05:29
  • @felipeac Welcome to Stackoverflow. If you did solve your problem it is ok to write this as an answer (e.g. "The problem was a mixup between word and itext...) This will help others in future... – Lonzak Aug 20 '20 at 09:50

1 Answers1

0

Thanks to @André Lemos answer and me I found the answer. The problem ocurrs when I try to print the document like a BitMap Image like this:

        val v: View = pdfViewAct
        val bmp =
            Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888)
        val c = Canvas(bmp)
        v.draw(c)
        val photoPrinter = PrintHelper(this)
        photoPrinter.scaleMode = PrintHelper.SCALE_MODE_FIT
        if (bundle != null) {
            bundle.getString("path")?.let { it1 -> photoPrinter.printBitmap(it1, bmp) }
        }

When I used this function to print the file, the Paragraphs was converting into Images and the resolution was changed (according to the Image conversion inside pdf).

Only change the code above for 2 & 3 answers helps me.

felipeac
  • 1
  • 1