Exemplo n.º 1
0
        // Loads an existing PDF and renders it to a Bitmap
        public void renderFile(object sender, EventArgs e)
        {
            // Render the page and save it to an image file
            try
            {
                // Load in an already created PDF
                using (var document = PDDocument.Load(assetManager.Open("Created.pdf")))
                    // Create a renderer for the document
                    using (var renderer = new PDFRenderer(document))
                    {
                        // Render the image to an RGB Bitmap
                        pageImage = renderer.RenderImage(0, 1, Bitmap.Config.Rgb565);

                        // Save the render result to an image
                        string filePath = root.AbsolutePath + "/render.jpg";
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            pageImage.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
                        }

                        tv.Text = "Successfully rendered image to " + filePath;
                        // Optional: display the render result on screen
                        displayRenderedImage();
                    }
            }
            catch (IOException ex)
            {
                Log.Error("PdfBox-Android-Sample", "Exception thrown while rendering file", ex);
            }
        }
Exemplo n.º 2
0
        // Fills in a PDF form and saves the result
        public void fillForm(object sender, EventArgs e)
        {
            try
            {
                // Load the document and get the AcroForm
                using (var document = PDDocument.Load(assetManager.Open("FormTest.pdf")))
                    using (var docCatalog = document.DocumentCatalog)
                        using (var acroForm = docCatalog.AcroForm)
                        {
                            // Fill the text field
                            using (var field = (PDTextField)acroForm.GetField("TextField"))
                            {
                                field.Value = "Filled Text Field";
                                // Optional: don't allow this field to be edited
                                field.ReadOnly = true;
                            }

                            using (var checkbox = acroForm.GetField("Checkbox"))
                            {
                                ((PDCheckbox)checkbox).Check();
                            }

                            using (var radio = acroForm.GetField("Radio"))
                            {
                                ((PDRadioButton)radio).Value = "Second";
                            }

                            // TODO: Use List<int>
                            using (var listbox = acroForm.GetField("ListBox"))
                            {
                                List <Integer> listValues = new List <Integer>();
                                listValues.Add(Integer.ValueOf(1.ToString()));
                                listValues.Add(Integer.ValueOf(2.ToString()));
                                ((PDListBox)listbox).SelectedOptionsIndex = listValues;
                            }

                            /*
                             * // TODO: Fix Exception
                             * using (var dropdown = acroForm.GetField("Dropdown"))
                             * {
                             *      IList<string> list = new List<string>();
                             *      list.Add("Hello");
                             *      ((PDComboBox)dropdown).Value = list;
                             * }
                             */

                            string path = root.AbsolutePath + "/FilledForm.pdf";
                            tv.Text = "Saved filled form to " + path;
                            document.Save(path);
                            document.Close();
                        }
            }
            catch (IOException ex)
            {
                Log.Error("PdfBox-Android-Sample", "Exception thrown while filling form fields", ex);
            }
        }
Exemplo n.º 3
0
        // Strips the text from a PDF and displays the text on screen
        public void stripText(object sender, EventArgs e)
        {
            string     parsedText = null;
            PDDocument document   = null;

            try
            {
                document = PDDocument.Load(assetManager.Open("Hello.pdf"));
            }
            catch (IOException ex)
            {
                Log.Error("PdfBox-Android-Sample", "Exception thrown while loading document to strip", ex);
            }

            try
            {
                using (var pdfStripper = new PDFTextStripper())
                {
                    pdfStripper.StartPage = 0;
                    pdfStripper.EndPage   = 1;
                    parsedText            = "Parsed text: " + pdfStripper.GetText(document);
                }
            }
            catch (IOException ex)
            {
                Log.Error("PdfBox-Android-Sample", "Exception thrown while stripping text", ex);
            }
            finally
            {
                try
                {
                    if (document != null)
                    {
                        document.Close();
                        document.Dispose();
                    }
                }
                catch (IOException ex)
                {
                    Log.Error("PdfBox-Android-Sample", "Exception thrown while closing document", ex);
                }
            }
            tv.Text = parsedText;
        }
Exemplo n.º 4
0
        // Creates a new PDF from scratch and saves it to a file
        public void createPdf(object sender, EventArgs e)
        {
            using (var document = new PDDocument())
                using (var page = new PDPage())
                {
                    document.AddPage(page);

                    // Create a new font object selecting one of the PDF base fonts
                    using (PDFont font = PDType1Font.Helvetica)
                    {
                        // Or a custom font
                        //try
                        //{
                        //	// Replace MyFontFile with the path to the asset font you'd like to use.
                        //	// Or use LiberationSans "com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf"
                        //	font = PDType0Font.Load(document, assetManager.Open("MyFontFile.ttf"));
                        //}
                        //catch (IOException ex)
                        //{
                        //	Log.Error("PdfBox-Android-Sample", "Could not load font", ex);
                        //}

                        try
                        {
                            // Define a content stream for adding to the PDF
                            using (var contentStream = new PDPageContentStream(document, page))
                            {
                                // Write Hello World in blue text
                                contentStream.BeginText();
                                contentStream.SetNonStrokingColor(15, 38, 192);
                                contentStream.SetFont(font, 12);
                                contentStream.NewLineAtOffset(100, 700);
                                contentStream.ShowText("Hello World");
                                contentStream.EndText();

                                // Load in the images
                                var inStream    = assetManager.Open("falcon.jpg");
                                var alphaStream = assetManager.Open("trans.png");

                                // Draw a green rectangle
                                contentStream.AddRect(5, 500, 100, 100);
                                contentStream.SetNonStrokingColor(0, 255, 125);
                                contentStream.Fill();

                                // Draw the falcon base image
                                using (var ximage = JPEGFactory.CreateFromStream(document, inStream))
                                {
                                    contentStream.DrawImage(ximage, 20, 20);
                                }

                                // Draw the red overlay image
                                using (var alphaImage = BitmapFactory.DecodeStream(alphaStream))
                                    using (var alphaXimage = LosslessFactory.CreateFromImage(document, alphaImage))
                                    {
                                        contentStream.DrawImage(alphaXimage, 20, 20);
                                    }

                                // Make sure that the content stream is closed:
                                contentStream.Close();

                                // Save the final pdf document to a file
                                string path = root.AbsolutePath + "/Created.pdf";
                                document.Save(path);
                                document.Close();
                                tv.Text = "Successfully wrote PDF to " + path;
                            }
                        }
                        catch (IOException ex)
                        {
                            Log.Error("PdfBox-Android-Sample", "Exception thrown while creating PDF", ex);
                        }
                    }
                }
        }