如何在Android Studio中使用itext在现有的pdf文件中添加视频?

时间:2018-06-29 04:38:28

标签: android itext

我现在正在使用itext库生成一个PDF文件,我正在尝试附加该PDF文件的视频,但该视频未显示在生成的PDF中。我引用了以下链接来执行此操作,但我的PDF文件中没有任何视频。请给我一些建议。

IText PDF Library : Embedded video, how to show thumbnail and playback controls

http://what-when-how.com/itext-5/integrating-rich-media-part-1-itext-5/

    public class MainActivity extends AppCompatActivity {

    /**
     * The resulting PDF file.
     */
    public static final String RESULT = "samplefile.pdf";
    /**
     * The path to a Flash application.
     */
    public static final String RESOURCE = "res/raw/samplefile.swf";


    private Button mButton;
    private EditText mEditusertext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditusertext = (EditText) findViewById(R.id.editText);
        mButton = (Button) findViewById(R.id.button);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createpdf(mEditusertext.getText().toString());
            }
        });

    }

    private void createpdf(String s) {
        File docsFolder = new File(Environment.getExternalStorageDirectory() + "/MyDocuments");
        if (!docsFolder.exists()) {
            docsFolder.mkdir();
            Log.i(TAG, "Created a new directory for PDF");
        }


        Document document = new Document();
        //for richmedia...
        PdfWriter writer = null;
        try {
            writer = PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory() + File.separator + RESULT));
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        document.open();
        // step 4
        // we prepare a RichMediaAnnotation
        RichMediaAnnotation richMedia = new RichMediaAnnotation(writer, new Rectangle(36, 400, 559, 806));
        // we embed the swf file
        PdfFileSpecification fs
                = null;
        try {
            fs = PdfFileSpecification.fileEmbedded(writer, RESOURCE, "samplefile.swf", null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // we declare the swf file as an asset
        PdfIndirectReference asset = null;
        try {
            asset = richMedia.addAsset("samplefile.swf", fs);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // we create a configuration
        RichMediaConfiguration configuration = new RichMediaConfiguration(PdfName.FLASH);
        RichMediaInstance instance = new RichMediaInstance(PdfName.FLASH);
        RichMediaParams flashVars = new RichMediaParams();
        String vars = new String("&day=2011-10-13");
        flashVars.setFlashVars(vars);
        instance.setParams(flashVars);
        instance.setAsset(asset);
        configuration.addInstance(instance);
        // we add the configuration to the annotation
        PdfIndirectReference configurationRef = null;
        try {
            configurationRef = richMedia.addConfiguration(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // activation of the rich media
        RichMediaActivation activation = new RichMediaActivation();
        activation.setConfiguration(configurationRef);
        richMedia.setActivation(activation);
        // we add the annotation
        PdfAnnotation richMediaAnnotation = null;
        try {
            richMediaAnnotation = richMedia.createAnnotation();
        } catch (IOException e) {
            e.printStackTrace();
        }
        richMediaAnnotation.setFlags(PdfAnnotation.FLAGS_PRINT);
        writer.addAnnotation(richMediaAnnotation);
        // step 5
        document.close();

    }

}

0 个答案:

没有答案