// POST: api/Annotations
        public void Post([FromBody]Annotations value)
        {
            DocumentRepository documentData = new DocumentRepository();
            CurrentDocuments currentDocuments = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName());
            string [] pathParts = currentDocuments.PathToCurrentPDFDocument.Split('\\');
            string tempPDFPath = string.Empty;

            for (int i = 0; i < pathParts.Length - 1 ; i++)
            {
                tempPDFPath = tempPDFPath + pathParts[i] + "\\";
            }

            tempPDFPath = tempPDFPath + "temp" + pathParts[pathParts.Length -1];

            System.IO.File.Copy(currentDocuments.PathToCurrentPDFDocument, tempPDFPath,true);
            try
            {

                PDFDoc in_doc = new PDFDoc(tempPDFPath);
                {
                    in_doc.InitSecurityHandler();

                    //string str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><xfdf xmlns=\"http://ns.adobe.com/xfdf\" xml:space=\"preserve\"><square subject=\"Rectangle\" page=\"0\" name=\"cf4d2e58-e9c5-2a58-5b4d-9b4b1a330e45\" title=\"user\" creationdate=\"D:20120827112326-07'00'\" date=\"D:20120827112326-07'00'\" rect=\"227.7814207650273,597.6174863387978,437.07103825136608,705.0491803278688\" color=\"#000000\" interior-color=\"#FFFF00\" flags=\"print\" width=\"1\"><popup flags=\"print,nozoom,norotate\" open=\"no\" page=\"0\" rect=\"0,792,0,792\" /></square></xfdf>";
                    string str = value.Data;

                    using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str)))
                    {
                        in_doc.FDFMerge(fdoc);
                        in_doc.Save(currentDocuments.PathToCurrentPDFDocument, SDFDoc.SaveOptions.e_linearized);
                    }
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        // POST: api/Annotations
        public void Post([FromBody]Annotations value)
        {
            DocumentRepository documentData = new DocumentRepository();
            CurrentDocuments currentDocuments = documentData.GetCurrentDocumentPathPdf(Utility.GetUserName());
            string [] pathParts = currentDocuments.PathToCurrentPdfDocument.Split('\\');
            string tempPdfPath = string.Empty;

            for (int i = 0; i < pathParts.Length - 1 ; i++)
            {
                tempPdfPath = tempPdfPath + pathParts[i] + "\\";
            }

            tempPdfPath = tempPdfPath + "temp" + pathParts[pathParts.Length -1];

            System.IO.File.Copy(currentDocuments.PathToCurrentPdfDocument, tempPdfPath,true);
            try
            {
                PDFDoc inDoc = new PDFDoc(tempPdfPath);
                {
                   inDoc.InitSecurityHandler();

                    string str = value.Data;

                    using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str)))
                    {
                        inDoc.FDFMerge(fdoc);
                        inDoc.FlattenAnnotations();
                        inDoc.Save(currentDocuments.PathToCurrentPdfDocument, SDFDoc.SaveOptions.e_incremental);
                        //inDoc.Save(currentDocuments.PathToCurrentPdfDocument, SDFDoc.SaveOptions.e_linearized);
                    }
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        /// <summary>
        /// Genera la factura de la reserva id pasada como parametro, si la factura pertenece al socio y si ha pagado la reserva, y la devuelve al socio
        /// </summary>
        /// <param name="id">id de la reserva para la cual mostrar la factura</param>
        /// <returns>pdf con la factura si socio, sino redirige a /Home/index o /Admin/index dependiendo si es admin o no logueado</returns>
        public ActionResult VerFacturaReserva(int? id)
        {
            if (isSocio())
            {
                reservas reserva = db.reservas.Find(id);
                if (reserva == null)
                {
                    addError("No tiene permisos para visualizar esta reserva");
                    saveErrors();
                    return RedirectToAction("MisReservas", "Socio");
                }
                string socio_id = (string)Session["UserID"];
                if (reserva.socios.id != socio_id)
                {
                    addError("No tiene permisos para visualizar esta reserva");
                    saveErrors();
                    return RedirectToAction("MisReservas", "Socio");
                }
                if (reserva.pagado == false)
                {
                    addError("La factura no puede ser visualizada hasta que la reserva sea abonada");
                    saveErrors();
                    return RedirectToAction("MisReservas", "Socio");
                }

                // Creacion del pdf a partir de los datos xml en la BBDD
                PDFNet.Initialize();

                // Ruta relavita a las carpetas que contienen los archivos
                string input_path = "c:/Google Drive/PFC/pdf/";
                string output_path = "c:/Google Drive/PFC/pdf/Output/";
                try
                {
                    // Juntar XFDF desde el xml string
                    PDFDoc in_doc = new PDFDoc(input_path + "factura.pdf");
                    {
                        in_doc.InitSecurityHandler();

                        //Debug.WriteLine("Juntamos XFDF string con el PDF...");

                        //reservas reserva = db.reservas.Find(id);
                        string str = reserva.facturas.xml_factura;
                        //Debug.WriteLine(str);

                        using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str)))
                        {
                            in_doc.FDFMerge(fdoc);
                            // Iniciamos los permisos del pdf, para que no se pueda editar
                            StdSecurityHandler newHandler = new StdSecurityHandler();
                            newHandler.SetPermission(SecurityHandler.Permission.e_doc_modify, false);
                            newHandler.SetPermission(SecurityHandler.Permission.e_fill_forms, false);
                            newHandler.SetPermission(SecurityHandler.Permission.e_extract_content, false);
                            newHandler.SetPermission(SecurityHandler.Permission.e_mod_annot, false);

                            in_doc.SetSecurityHandler(newHandler);
                            in_doc.Save(output_path + "factura_modificada.pdf", SDFDoc.SaveOptions.e_linearized);
                            Debug.WriteLine("Juntado completado.");
                        }
                    }
                }
                catch (PDFNetException e)
                {
                    Debug.WriteLine(e.Message);
                }

                return File("c:/Google drive/PFC/pdf/output/factura_modificada.pdf", "application/pdf");
            }
            else
                return RedirectToAction("Index", isAdmin() ? "Admin" : "Home");
        }
示例#4
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            PDFNet.Initialize();

            // Relative path to the folder containing test files.
            string input_path  = "../../TestFiles/";
            string output_path = "../../TestFiles/Output/";

            // Example 1)
            // Iterate over all form fields in the document. Display all field names.
            try
            {
                using (PDFDoc doc = new PDFDoc(input_path + "form1.pdf"))
                {
                    doc.InitSecurityHandler();

                    FieldIterator itr;
                    for (itr = doc.GetFieldIterator(); itr.HasNext(); itr.Next())
                    {
                        Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
                        Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());

                        Console.Write("Field type: ");
                        Field.Type type = itr.Current().GetType();
                        switch (type)
                        {
                        case Field.Type.e_button:
                            Console.WriteLine("Button"); break;

                        case Field.Type.e_text:
                            Console.WriteLine("Text"); break;

                        case Field.Type.e_choice:
                            Console.WriteLine("Choice"); break;

                        case Field.Type.e_signature:
                            Console.WriteLine("Signature"); break;
                        }

                        Console.WriteLine("------------------------------");
                    }

                    Console.WriteLine("Done.");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
            try
            {
                // XFDF to FDF
                // form fields
                Console.WriteLine("Import form field data from XFDF to FDF.");

                FDFDoc fdf_doc1 = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf"));
                fdf_doc1.Save(output_path + "form1_data.fdf");

                // annotations
                Console.WriteLine("Import annotations from XFDF to FDF.");

                FDFDoc fdf_doc2 = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf"));
                fdf_doc2.Save(output_path + "form1_annots.fdf");

                // FDF to PDF
                // form fields
                Console.WriteLine("Merge form field data from FDF.");

                using (PDFDoc doc = new PDFDoc(input_path + "form1.pdf"))
                {
                    doc.InitSecurityHandler();
                    doc.FDFMerge(fdf_doc1);

                    // To use PDFNet form field appearance generation instead of relying on
                    // Acrobat, uncomment the following two lines:
                    // doc.RefreshFieldAppearances();
                    // doc.GetAcroForm().Put("NeedAppearances", Obj.CreateBool(false));

                    doc.Save(output_path + "form1_filled.pdf", SDFDoc.SaveOptions.e_linearized);

                    // annotations
                    Console.WriteLine("Merge annotations from FDF.");

                    doc.FDFMerge(fdf_doc2);
                    doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc.SaveOptions.e_linearized);

                    Console.WriteLine("Done.");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
            try
            {
                // PDF to FDF
                using (PDFDoc in_doc = new PDFDoc(output_path + "form1_filled_with_annots.pdf"))
                {
                    in_doc.InitSecurityHandler();

                    // form fields only
                    Console.WriteLine("Extract form fields data to FDF.");

                    FDFDoc doc_fields = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_forms_only);
                    doc_fields.SetPdfFileName("../form1_filled_with_annots.pdf");
                    doc_fields.Save(output_path + "form1_filled_data.fdf");

                    // annotations only
                    Console.WriteLine("Extract annotations to FDF.");

                    FDFDoc doc_annots = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only);
                    doc_annots.SetPdfFileName("../form1_filled_with_annots.pdf");
                    doc_annots.Save(output_path + "form1_filled_annot.fdf");

                    // both form fields and annotations
                    Console.WriteLine("Extract both form fields and annotations to FDF.");

                    FDFDoc doc_both = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
                    doc_both.SetPdfFileName("../form1_filled_with_annots.pdf");
                    doc_both.Save(output_path + "form1_filled_both.fdf");

                    // FDF to XFDF
                    // form fields
                    Console.WriteLine("Export form field data from FDF to XFDF.");

                    doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf");

                    // annotations
                    Console.WriteLine("Export annotations from FDF to XFDF.");

                    doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf");

                    // both form fields and annotations
                    Console.WriteLine("Export both form fields and annotations from FDF to XFDF.");

                    doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf");

                    Console.WriteLine("Done.");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            // Example 4) Merge/Extract XFDF into/from PDF
            try
            {
                // Merge XFDF from string
                PDFDoc in_doc = new PDFDoc(input_path + "numbered.pdf");
                {
                    in_doc.InitSecurityHandler();

                    Console.WriteLine("Merge XFDF string into PDF.");

                    string str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><xfdf xmlns=\"http://ns.adobe.com/xfdf\" xml:space=\"preserve\"><square subject=\"Rectangle\" page=\"0\" name=\"cf4d2e58-e9c5-2a58-5b4d-9b4b1a330e45\" title=\"user\" creationdate=\"D:20120827112326-07'00'\" date=\"D:20120827112326-07'00'\" rect=\"227.7814207650273,597.6174863387978,437.07103825136608,705.0491803278688\" color=\"#000000\" interior-color=\"#FFFF00\" flags=\"print\" width=\"1\"><popup flags=\"print,nozoom,norotate\" open=\"no\" page=\"0\" rect=\"0,792,0,792\" /></square></xfdf>";

                    using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str)))
                    {
                        in_doc.FDFMerge(fdoc);
                        in_doc.Save(output_path + "numbered_modified.pdf", SDFDoc.SaveOptions.e_linearized);
                        Console.WriteLine("Merge complete.");
                    }

                    // Extract XFDF as string
                    Console.WriteLine("Extract XFDF as a string.");
                    FDFDoc fdoc_new = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
                    string XFDF_str = fdoc_new.SaveAsXFDF();
                    Console.WriteLine("Extracted XFDF: ");
                    Console.WriteLine(XFDF_str);
                    Console.WriteLine("Extract complete.");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            // Example 5) Read FDF files directly
            try
            {
                FDFDoc           doc = new FDFDoc(output_path + "form1_filled_data.fdf");
                FDFFieldIterator itr = doc.GetFieldIterator();
                for (; itr.HasNext(); itr.Next())
                {
                    Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
                    Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());
                    Console.WriteLine("------------------------------");
                }

                Console.WriteLine("Done.");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            // Example 6) Direct generation of FDF.
            try
            {
                FDFDoc doc = new FDFDoc();

                // Create new fields (i.e. key/value pairs).
                doc.FieldCreate("Company", (int)Field.Type.e_text, "PDFTron Systems");
                doc.FieldCreate("First Name", (int)Field.Type.e_text, "John");
                doc.FieldCreate("Last Name", (int)Field.Type.e_text, "Doe");
                // ...

                // doc.SetPdfFileName("mydoc.pdf");
                doc.Save(output_path + "sample_output.fdf");
                Console.WriteLine("Done. Results saved in sample_output.fdf");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }