使用Java itextPdf pdf文件未在android

时间:2016-05-04 10:01:23

标签: java android printing itext inputstream

我正在编写一个包含动态报告部分的mobil应用程序。我正在使用Java ItextPdf库,但是当我将它发送到打印机打印机时,不接受我的文件并发出通知“此设备不支持文件类型”。但是当我发送由pc创建的普通pdf文件时,它正在打印。

印刷类

PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);

            // Set job name, which will be displayed in the print queue
            String jobName = " Document";

            // Start a print job, passing in a PrintDocumentAdapter implementation
            // to handle the generation of a print document
            File path = context.getFilesDir();
            File file = new File(path,"ss.pdf");
            FileOutputStream os = null;
            FileInputStream is = null;
            try {
                os = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            Adisyon a = new Adisyon(os);
            a.createFile();
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }


            printManager.print(jobName, new printAdapter(Siparis.this,is),

PrintAdapter Class

@TargetApi(Build.VERSION_CODES.KITKAT)
public class printAdapter extends PrintDocumentAdapter
{
    Context context;
    InputStream input;

    public printAdapter(Context context,FileInputStream input)
    {
        this.context = context;
        this.input = input;
    }

    @Override
    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback){

        OutputStream output = null;

        try {


            output = new FileOutputStream(destination.getFileDescriptor());

            byte[] buf = new byte[1024];
            int bytesRead;

            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }

            callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});

        } catch (FileNotFoundException ee){
            //Catch exception
        } catch (Exception e) {
            //Catch exception
        } finally {

            try {
                input.close();
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras){

        if (cancellationSignal.isCanceled()) {
            callback.onLayoutCancelled();
            return;
        }

        PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("deneme.txt").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

        callback.onLayoutFinished(pdi, true);

    }

}

Itext Class

public class Adisyon {
FileOutputStream stream;
Context appContext = LoginActivity.getContextOfApplication();


public Adisyon(FileOutputStream stream) {
    this.stream = stream;

}

public void createFile() {
    try {

        Document document = new Document();
        PdfWriter.getInstance(document, stream);
        document.open();
        //addMetaData(document);
        //addTitlePage(document);
        //addContent(document);
        createAdisyon(document);
        document.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
...

0 个答案:

没有答案