Beispiel #1
0
        static void ConvertFromStream()
        {
            // We need files only for demonstration purposes.
            // The conversion process will be done completely in memory.
            byte[] inpData = File.ReadAllBytes(@"..\..\..\..\..\..\Testing Files\example.pdf");
            byte[] outData = null;

            using (MemoryStream msInp = new MemoryStream(inpData))
            {
                // Load a document.
                DocumentCore dc = DocumentCore.Load(msInp, new PdfLoadOptions()
                {
                    PreserveGraphics = true,
                    DetectTables     = true
                });

                // Save the document to another format.
                using (MemoryStream outMs = new MemoryStream())
                {
                    dc.Save(outMs, new HtmlFixedSaveOptions()
                    {
                        CssExportMode = CssExportMode.Inline,
                        EmbedImages   = true
                    });
                    outData = outMs.ToArray();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Find and replace a text using ContentRange.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/find-replace-content-net-csharp-vb.php
        /// </remarks>
        public static void FindAndReplace()
        {
            // Path to a loadable document.
            string loadPath = @"..\..\critique.docx";

            // Load a document intoDocumentCore.
            DocumentCore dc = DocumentCore.Load(loadPath);

            Regex regex = new Regex(@"bean", RegexOptions.IgnoreCase);

            //Find "Bean" and Replace everywhere on "Joker :-)"
            // Please note, Reverse() makes sure that action replace not affects to Find().
            foreach (ContentRange item in dc.Content.Find(regex).Reverse())
            {
                item.Replace("Joker");
            }

            // Save our document into PDF format.
            string savePath = "Replaced.pdf";

            dc.Save(savePath, new PdfSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath)
            {
                UseShellExecute = true
            });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath)
            {
                UseShellExecute = true
            });
        }
Beispiel #3
0
        /// <summary>
        /// Convert RTF to DOCX (using Stream).
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-rtf-to-docx-in-csharp-vb.php
        /// </remarks>
        static void ConvertFromStream()
        {
            // We need files only for demonstration purposes.
            // The conversion process will be done completely in memory.
            string inpFile = @"..\..\example.rtf";
            string outFile = @"ResultStream.docx";

            byte[] inpData = File.ReadAllBytes(inpFile);
            byte[] outData = null;

            using (MemoryStream msInp = new MemoryStream(inpData))
            {
                // Load a document.
                DocumentCore dc = DocumentCore.Load(msInp, new RtfLoadOptions());

                // Save the document to DOCX format.
                using (MemoryStream outMs = new MemoryStream())
                {
                    dc.Save(outMs, new DocxSaveOptions());
                    outData = outMs.ToArray();
                }
                // Show the result for demonstration purposes.
                if (outData != null)
                {
                    File.WriteAllBytes(outFile, outData);
                    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
                    {
                        UseShellExecute = true
                    });
                }
            }
        }
Beispiel #4
0
        public void CreatePDFFIleReport()
        {
            CreateWordFileReport(Program.expencesList, Program.incomeList);
            var document = DocumentCore.Load(@"C:\Users\user\source\repos\ZenMoney\ZenMoney\bin\Debug\OutputDocument.docx");

            document.Save(@"C:\Users\user\source\repos\ZenMoney\ZenMoney\bin\Debug/report.pdf");
        }
Beispiel #5
0
        /// <summary>
        /// Merge all paragraphs into a single in an existing PDF document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/merge-paragraphs-in-pdf-document-net-csharp-vb.php
        /// </remarks>
        static void MergeParagraphs()
        {
            string       inpFile = @"..\..\example.pdf";
            string       outFile = @"Result.pdf";
            DocumentCore dc      = DocumentCore.Load(inpFile);

            Paragraph firstPar = dc.GetChildElements(true, ElementType.Paragraph).First() as Paragraph;

            int lastIndex = firstPar.Inlines.Count;

            foreach (Paragraph par in dc.GetChildElements(true, ElementType.Paragraph).Reverse().Where(p => p != firstPar))
            {
                int last = lastIndex;
                foreach (Inline inline in par.Inlines)
                {
                    firstPar.Inlines.Insert(last++, inline.Clone(true));
                }
                par.Content.Delete();
            }

            dc.Save(outFile);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(inpFile)
            {
                UseShellExecute = true
            });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
            {
                UseShellExecute = true
            });
        }
Beispiel #6
0
        public static void ExtractPictures()
        {
            // Path to a document where to extract pictures.
            string filePath = @"..\..\..\..\..\..\Testing Files\example.pdf";

            // Directory to store extracted pictures:
            DirectoryInfo imageDirectory = new DirectoryInfo(Path.GetDirectoryName(filePath));
            string        imageTemplate  = "Picture";

            // Here we store extracted images.
            List <ImageData> imageInventory = new List <ImageData>();

            // Load the document.
            DocumentCore dc = DocumentCore.Load(filePath);

            // Extract all images from document, skip duplicates.
            foreach (Picture pict in dc.GetChildElements(true, ElementType.Picture))
            {
                // Let's avoid the adding of duplicates.
                if (imageInventory.Exists((img => (img.GetStream().Length == pict.ImageData.GetStream().Length))) == false)
                {
                    imageInventory.Add(pict.ImageData);
                }
            }

            // Save and show all images.
            for (int i = 0; i < imageInventory.Count; i++)
            {
                string imagePath = Path.Combine(imageDirectory.FullName, String.Format("{0}{1}.{2}", imageTemplate, i + 1, imageInventory[i].Format.ToString().ToLower()));
                File.WriteAllBytes(imagePath, imageInventory[i].GetStream().ToArray());
                System.Diagnostics.Process.Start(imagePath);
            }
        }
Beispiel #7
0
        /// <summary>
        /// How to replace a hyperlink URL by a new address.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/hyperlinks-replace-url-csharp-vb-net.php
        /// </remarks>
        public static void ReplaceHyperlinksURL()
        {
            // Let us say, we've a DOCX document.
            // And we've to replace the all URLs by the custom.
            // Furthermore, let's save the result as PDF.

            string inpFile = @"..\..\Hyperlinks example.docx";
            string outFile = @"Result - URL.pdf";

            // Let's open our document.
            DocumentCore dc = DocumentCore.Load(inpFile);

            // Specify the custom URL.
            string customURL = "https://www.sautinsoft.com";

            // Loop by all hyperlinks and replace the URL (address).
            foreach (Hyperlink hpl in dc.GetChildElements(true, ElementType.Hyperlink))
            {
                hpl.Address = customURL;
            }

            // Save our document back, but in PDF format.
            dc.Save(outFile, new PdfSaveOptions()
            {
                Compliance = PdfCompliance.PDF_14
            });

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
            {
                UseShellExecute = true
            });
        }
Beispiel #8
0
        /// <summary>
        /// Iterates through a document and count the amount of Paragraphs and Runs.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-iteration.php
        /// </remarks>
        static void ShowInlines()
        {
            string       filePath = @"..\..\example.docx";
            DocumentCore dc       = DocumentCore.Load(filePath);

            Console.WriteLine("This document contains from:");
            for (int sect = 0; sect < dc.Sections.Count; sect++)
            {
                Console.WriteLine("Section {0} contains from:", sect);
                int     totalParagraphs = 0;
                Section section         = dc.Sections[sect];
                for (int blocks = 0; blocks < section.Blocks.Count; blocks++)
                {
                    if (section.Blocks[blocks] is Paragraph)
                    {
                        totalParagraphs++;
                        Paragraph paragraph = section.Blocks[blocks] as Paragraph;
                        Console.Write("\t\t Paragraph {0} contains from: ", totalParagraphs);
                        int totalRuns = 0;
                        for (int i = 0; i < paragraph.Inlines.Count; i++)
                        {
                            if (paragraph.Inlines[i] is Run)
                            {
                                totalRuns++;
                            }
                        }
                        Console.WriteLine("{0} Run(s).", totalRuns);
                    }
                }
            }
            Console.ReadKey();
        }
        private void Button_Click1(object sender, RoutedEventArgs e) // docx
        {
            //SautinSoft.Document library:
            //Create: DOCX, PDF, RTF, HTML
            //Load: DOCX, PDF, RTF, HTML
            //Save as: DOCX, PDF, RTF, HTML, Text
            //string inpFile = @"C:\Users\user\Desktop\kkkkkkkk\d\d\dokum\test.docx..docx";
            //string outFile = @"C:\Users\user\Desktop\kkkkkkkk\d\d\dokum\111.pdf";
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.CheckFileExists  = true;
            openFileDialog.Multiselect      = true;
            openFileDialog.Filter           = "Doc files (*.docx)|*.docx|All files (*.*)|*.*";
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
            Nullable <bool> result = openFileDialog.ShowDialog();

            if (result == true)
            {
                string       inpFile = openFileDialog.FileName; // Open document
                string       outFile = @"C:\Users\Mariia\Desktop\Convertor\PDFfromDOC.pdf";
                DocumentCore dc      = DocumentCore.Load(inpFile);
                dc.Save(outFile);
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
                {
                    UseShellExecute = true
                });
                //File.OpenRead(filename);
            }
            //DocumentCore dc = DocumentCore.Load(inpFile);
            //dc.Save(outFile);
            // Open the result for demonstation purposes.
            //System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
Beispiel #10
0
        /// <summary>
        /// Inserts a new Run (Text element) at the start of each paragraph.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-insert.php
        /// </remarks>
        static void InsertParagraphCount()
        {
            string       filePath     = @"..\..\example.docx";
            DocumentCore dc           = DocumentCore.Load(filePath);
            int          paragraphNum = 1;

            foreach (Element el in dc.Sections[0].GetChildElements(false))
            {
                if (el is Paragraph)
                {
                    // Insert a new Run into Paragraph.InlineCollection 'Inlines'.
                    // InlineCollection is descendant of the base abstract class ElementCollection.
                    (el as Paragraph).Inlines.Insert(0, new Run(dc, "Paragraph " + paragraphNum.ToString() + " - ", new CharacterFormat()
                    {
                        BackgroundColor = Color.Orange, FontColor = Color.White
                    }));
                    paragraphNum++;
                }
            }
            dc.Save("Result.docx");

            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Result.docx")
            {
                UseShellExecute = true
            });
        }
Beispiel #11
0
        /// <summary>
        /// Generate reports using a custom data source (collection of custom classes Actor and Order).
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/mail-merge-custom-data-source-net-csharp-vb.php
        /// </remarks>
        static void CustomDataSource()
        {
            // Populate some data that we will use in the mail merge.
            List <Actor> actors = new List <Actor>();

            actors.Add(new Actor("Arnold Schwarzenegger", "12989 Chalon Road, Los Angeles, CA 90049"));
            actors.Add(new Actor("Sylvester Stallone", "30 Beverly Park Terrace, Beverly Hills, CA 90210"));

            // Populate some data for nesting in the mail merge.
            actors[0].Orders.Add(new Order("Bowflex SelectTech 1090 Adjustable Dumbbell", 2));
            actors[0].Orders.Add(new Order("Gold's Gym Kettlebell Kit, 5-15 Lbs.", 1));
            actors[1].Orders.Add(new Order("Weider Cast Iron Olympic Hammertone Weight Set, 300 Lb.", 1));

            // Load the template document.
            DocumentCore dc = DocumentCore.Load(@"..\..\OrdersTemplate.docx");

            // To be able to mail merge from your own data source, it must be wrapped into an object that implements the IMailMergeDataSource interface.
            CustomMailMergeDataSource customDataSource = new CustomMailMergeDataSource(actors);

            // Execute the mail merge.
            dc.MailMerge.Execute(customDataSource);

            string resultPath = "Orders.docx";

            // Save the output to file.
            dc.Save(resultPath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath)
            {
                UseShellExecute = true
            });
        }
Beispiel #12
0
        /// <summary>
        /// Loads a document into DocumentCore (dc) from an array of bytes.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/load-document.php
        /// </remarks>
        static void LoadFromBytes()
        {
            // Get document bytes from a file.
            byte[] fileBytes = File.ReadAllBytes(@"..\..\example.pdf");

            DocumentCore dc = null;

            using (MemoryStream ms = new MemoryStream(fileBytes))
            {
                // With PdfLoadOptions we explicitly set that a loadable document is PDF.
                PdfLoadOptions pdfLO = new PdfLoadOptions()
                {
                    // 'false' - means to load vector graphics as is. Don't transform it to raster images.
                    RasterizeVectorGraphics = false,

                    // The PDF format doesn't have real tables, in fact it's a set of orthogonal graphic lines.
                    // In case of 'true' the component will detect and recreate tables from graphic lines.
                    DetectTables = false,

                    // 'true' - Load embedded fonts from PDF document, even if the font with the same name is installed in your System.
                    PreserveEmbeddedFonts = false,

                    // Load only first 2 pages from the document.
                    PageIndex = 0,
                    PageCount = 2
                };
                dc = DocumentCore.Load(ms, pdfLO);
            }
            if (dc != null)
            {
                Console.WriteLine("Loaded successfully!");
            }

            Console.ReadKey();
        }
Beispiel #13
0
        /// <summary>
        /// How to clone an element in DOCX document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/cloning-element-net-csharp-vb.php
        /// </remarks>
        static void CloningElement()
        {
            string       filePath    = @"..\..\Parsing.docx";
            string       cloningFile = "Cloning.docx";
            DocumentCore dc          = DocumentCore.Load(filePath);

            // Clone section.
            dc.Sections.Add(dc.Sections[0].Clone(true));

            // Clone paragraphs.
            foreach (Block item in dc.Sections[0].Blocks)
            {
                dc.Sections.Last().Blocks.Add(item.Clone(true));
            }

            // Save the result.
            dc.Save(cloningFile);

            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath)
            {
                UseShellExecute = true
            });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(cloningFile)
            {
                UseShellExecute = true
            });
        }
Beispiel #14
0
        /// <summary>
        /// Generates a table report with regions using XML document as a data source.
        /// </summary>
        /// <remarks>
        /// See details at: https://www.sautinsoft.com/products/document/help/net/developer-guide/mail-merge-table-report-with-regions-net-csharp-vb.php
        /// </remarks>
        public static void TableReportWithRegions()
        {
            // Create the Dataset and read the XML.
            DataSet ds = new DataSet();

            ds.ReadXml(@"..\..\Orders.xml");

            // Load the template document.
            string templatePath = @"..\..\InvoiceTemplate.docx";

            DocumentCore dc = DocumentCore.Load(templatePath);

            // Execute the mail merge.
            dc.MailMerge.Execute(ds.Tables["Order"]);

            string resultPath = "Invoices.pdf";

            // Save the output to file
            dc.Save(resultPath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath)
            {
                UseShellExecute = true
            });
        }
Beispiel #15
0
        /// <summary>
        /// Convert PDF to RTF (file to file).
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/convert-pdf-to-rtf-in-csharp-vb.php
        /// </remarks>
        static void ConvertFromFile()
        {
            string inpFile = @"..\..\example.pdf";
            string outFile = @"Result.rtf";

            // Specifying PdfLoadOptions we explicitly set that a loadable document is PDF.
            PdfLoadOptions pdfLO = new PdfLoadOptions()
            {
                // 'false' - means to load vector graphics as is. Don't transform it to raster images.
                RasterizeVectorGraphics = false,

                // The PDF format doesn't have real tables, in fact it's a set of orthogonal graphic lines.
                // In case of 'true' the component will detect and recreate tables from graphic lines.
                DetectTables = false,

                // 'true' - Load embedded fonts from PDF document, even if the font with the same name is installed in your System.
                PreserveEmbeddedFonts = false
            };

            DocumentCore dc = DocumentCore.Load(inpFile, pdfLO);

            dc.Save(outFile);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
            {
                UseShellExecute = true
            });
        }
Beispiel #16
0
        /// <summary>
        /// Find Text and replace it with a Picture using ContentRange.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/find-replace-content-net-csharp-vb.php
        /// </remarks>
        public static void FindTextAndReplaceImage()
        {
            // Path to a loadable document.
            string loadPath = @"..\..\Critique_signature.docx";
            string pictPath = @"..\..\Smile.png";

            // Load a document intoDocumentCore.
            DocumentCore dc = DocumentCore.Load(loadPath);

            //Find "<signature>" Text and Replace everywhere with the "Smile.png"
            // Please note, Reverse() makes sure that action replace not affects to Find().
            Regex   regex   = new Regex(@"<signature>", RegexOptions.IgnoreCase);
            Picture picture = new Picture(dc, InlineLayout.Inline(new Size(50, 50)), pictPath);

            foreach (ContentRange item in dc.Content.Find(regex).Reverse())
            {
                item.Replace(picture.Content);
            }

            // Save our document into PDF format.
            string savePath = @"..\..\Replaced_signature.pdf";

            dc.Save(savePath, new PdfSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath)
            {
                UseShellExecute = true
            });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath)
            {
                UseShellExecute = true
            });
        }
Beispiel #17
0
        /// <summary>
        /// Delete a specific text from DOCX document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/delete-text-from-docx-document-net-csharp-vb.php
        /// </remarks>
        static void DeleteText()
        {
            string       filePath     = @"..\..\example.docx";
            string       fileResult   = @"Result.pdf";
            string       textToDelete = "document";
            DocumentCore dc           = DocumentCore.Load(filePath);

            int countDel = 0;

            foreach (ContentRange cr in dc.Content.Find(textToDelete).Reverse())
            {
                cr.Delete();
                countDel++;
            }
            Console.WriteLine("The text: \"" + textToDelete + "\" - was deleted " + countDel.ToString() + " time(s).");
            Console.ReadKey();

            dc.Save(fileResult);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath)
            {
                UseShellExecute = true
            });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(fileResult)
            {
                UseShellExecute = true
            });
        }
Beispiel #18
0
        private void BtnConverter_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtFileName.Text))
                {
                    return;
                }

                var docPath = txtFileName.Text;
                var pdfPath = docPath.Replace(".docx", ".pdf");

                var dc = DocumentCore.Load(docPath);
                dc.Save(pdfPath);

                txtFileName.Text      = string.Empty;
                btnSelecionar.Enabled = true;
                btnConverter.Enabled  = false;

                MessageBox.Show(this, "Arquivo convertido com sucesso!", @"Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Erro:", ex.Message);

                txtFileName.Text      = string.Empty;
                btnSelecionar.Enabled = true;
                btnConverter.Enabled  = false;
            }
        }
Beispiel #19
0
        public string Read(string path)
        {
            var dc      = DocumentCore.Load(path);
            var runList = dc.GetChildElements(true, ElementType.Run).Select(x => x.Content.ToString());

            return(string.Concat(runList));
        }
Beispiel #20
0
        /// <summary>
        /// Copy a document to other document. Supported any formats (PDF, DOCX, RTF, HTML).
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/importing-element-net-csharp-vb.php
        /// </remarks>
        static void ImportingElement()
        {
            string file1      = @"..\..\digitalsignature.docx";
            string file2      = @"..\..\Parsing.docx";
            string resultFile = "Importing.docx";

            // Load files.
            DocumentCore dc  = DocumentCore.Load(file1);
            DocumentCore dc1 = DocumentCore.Load(file2);

            // New Import Session to improve performance.
            var session = new ImportSession(dc1, dc);

            // Import all sections from source document.
            foreach (Section sourceSection in dc1.Sections)
            {
                Section destinationSection = dc.Import(sourceSection, true, session);
                dc.Sections.Add(destinationSection);
            }

            // Save the result.
            dc.Save(resultFile);

            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultFile)
            {
                UseShellExecute = true
            });
        }
Beispiel #21
0
        /// <summary>
        /// Open a document and delete some content.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/delete-content-net-csharp-vb.php
        /// </remarks>
        public static void DeleteContent()
        {
            string loadPath = @"..\..\example.docx";
            string savePath = "Result.docx";

            DocumentCore dc = DocumentCore.Load(loadPath);

            // Remove the text "This" from all paragraphs in 1st section.
            foreach (Paragraph par in dc.Sections[0].GetChildElements(true, ElementType.Paragraph))
            {
                var findText = par.Content.Find("This");

                if (findText != null)
                {
                    foreach (ContentRange cr in findText)
                    {
                        cr.Delete();
                    }
                }
            }
            dc.Save(savePath);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath)
            {
                UseShellExecute = true
            });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath)
            {
                UseShellExecute = true
            });
        }
Beispiel #22
0
        /// <summary>
        /// Insert a picture to custom page and position into existing DOCX document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-picture-jpg-image-to-custom-docx-page-and-position-net-csharp-vb.php
        /// </remarks>
        static void InsertPictureToCustomPageAndPosition()
        {
            // In this example we'll insert the picture to 1st after the word "Sign:".

            string inpFile  = @"..\..\example.docx";
            string outFile  = @"Result.docx";
            string pictFile = @"..\..\picture.jpg";

            DocumentCore      dc = DocumentCore.Load(inpFile);
            DocumentPaginator dp = dc.GetPaginator();

            // Find the text "Sign:" on the 1st page.
            ContentRange cr = dp.Pages[0].Content.Find("Sign:").LastOrDefault();

            if (cr != null)
            {
                Picture pic = new Picture(dc, pictFile);
                cr.End.Insert(pic.Content);
            }
            // Save the document as new DOCX and open it.
            dc.Save(outFile);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
            {
                UseShellExecute = true
            });
        }
Beispiel #23
0
        /// <summary>
        /// Inserts a new paragraph into an existing PDF document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-paragraphs-to-pdf-document-net-csharp-vb.php
        /// </remarks>
        static void InsertParagraph()
        {
            string inpFile = @"..\..\example.pdf";
            string outFile = @"Result.pdf";

            DocumentCore dc = DocumentCore.Load(inpFile);
            Paragraph    p  = new Paragraph(dc);

            p.Content.Start.Insert("Alexander Pushkin was a great russian romantic poet " +
                                   "and writer who is considered by a lot of people as the best russian poet and the founder " +
                                   "of contemporary russian literature.",
                                   new CharacterFormat()
            {
                Size = 20, FontName = "Verdana", FontColor = new Color("#358CCB")
            });
            p.ParagraphFormat.Alignment = HorizontalAlignment.Justify;

            // Insert the paragraph as 1st element in the 1st section.
            dc.Sections[0].Blocks.Insert(0, p);

            dc.Save(outFile);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
            {
                UseShellExecute = true
            });
        }
 public void LoadPages(int pagesCount)
 {
     for (int i = 0; i < pagesCount; i++)
     {
         List <SautinSoft.Document.Tables.TableRow> rowContent = new List <SautinSoft.Document.Tables.TableRow>();
         DocumentCore dc = DocumentCore.Load(folderPath + @"\Page - " + (i + 1).ToString() + ".pdf");
         foreach (SautinSoft.Document.Tables.TableRow run in dc.GetChildElements(true, ElementType.TableRow))
         {
             rowContent.Add(run);
         }
         ;
         foreach (SautinSoft.Document.Section run in dc.GetChildElements(true, ElementType.Section))
         {
             date = run.Blocks[run.Blocks.Count - 2].Content.ToString().Replace("\r\n", "").Substring(4, 10);
             break;
         }
         ;
         foreach (SautinSoft.Document.Paragraph run in dc.GetChildElements(true, ElementType.Paragraph))
         {
             if (run.Content.ToString().Contains("Дата принятия уполномоченным банком"))
             {
                 adoptionDate = run.Inlines[7].Content.ToString();
                 break;
             }
         }
         ;
         ParseAndGetInfo(rowContent, i + 1);
     }
 }
Beispiel #25
0
        /// <summary>
        /// Deletes a specific paragraphs in an existing DOCX and save it as new PDF.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/delete-paragraphs-in-docx-document-net-csharp-vb.php
        /// </remarks>
        static void DeleteParagraphs()
        {
            string filePath   = @"..\..\example.docx";
            string fileResult = @"Result.pdf";

            DocumentCore dc = DocumentCore.Load(filePath);

            // Note, remove paragraphs only inside the first section.
            Section section = dc.Sections[0];

            // Let's remove all paragraphs containing the text "Jack".
            for (int i = 0; i < section.Blocks.Count; i++)
            {
                if (section.Blocks[i].Content.Find("Jack").Count() > 0)
                {
                    section.Blocks.RemoveAt(i);
                    i--;
                }
            }
            dc.Save(fileResult);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath)
            {
                UseShellExecute = true
            });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(fileResult)
            {
                UseShellExecute = true
            });
        }
Beispiel #26
0
        /// <summary>
        /// How to delete all hyperlink objects.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/hyperlinks-delete-url-csharp-vb-net.php
        /// </remarks>
        public static void DeleteHyperlinksObjects()
        {
            // Let us say, we've a DOCX document.
            // And we've to remove the hyperlink objects.

            string inpFile = @"..\..\Hyperlinks example.docx";
            string outFile = @"Result - Delete Hyperlinks completely.pdf";

            // Let's open our document.
            DocumentCore dc = DocumentCore.Load(inpFile);

            // Loop by all hyperlinks and replace the URL (address).
            foreach (Hyperlink hpl in dc.GetChildElements(true, ElementType.Hyperlink).Reverse())
            {
                hpl.ParentCollection.Remove(hpl);
            }

            // Save our document back, but in PDF format.
            dc.Save(outFile, new PdfSaveOptions()
            {
                Compliance = PdfCompliance.PDF_14
            });

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
            {
                UseShellExecute = true
            });
        }
Beispiel #27
0
        /// <summary>
        /// How to get a content from a document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/get-content-net-csharp-vb.php
        /// </remarks>
        public static void GetContent()
        {
            // Path to an input document.
            string documentPath = @"..\..\example.docx";

            DocumentCore dc = DocumentCore.Load(documentPath);

            StringBuilder sb = new StringBuilder();

            // Get content of each paragraph in the document.
            foreach (Paragraph par in dc.GetChildElements(true, ElementType.Paragraph))
            {
                // The property 'Content' returns the content as ContentRange.
                // Get content and append it into StringBuilder.
                sb.AppendFormat("Paragraph: {0}", par.Content.ToString());
                sb.AppendLine();
            }

            // Get content of each Run where the text color is Red.
            foreach (Run run in dc.GetChildElements(true, ElementType.Run))
            {
                if (run.CharacterFormat.FontColor == Color.Red)
                {
                    // The property 'Content' returns the content as ContentRange.
                    // Get content and append it into StringBuilder.
                    sb.AppendFormat("Red color: {0}", run.Content.ToString());
                    sb.AppendLine();
                }
            }
            Console.WriteLine(sb.ToString());
            Console.ReadKey();
        }
        private void buttonGetDocument_Click(object sender, EventArgs e)
        {
            SetDefaultDataTable();
            List <string>  lS1 = new List <string>(); // Лист всех элементов из .docx
            OpenFileDialog opf = new OpenFileDialog();

            opf.Filter = "Word 2007 Documents (*.docx)|*.docx";
            if (opf.ShowDialog() == DialogResult.OK)
            {
                string        filename = opf.FileName; // Path to Docx file.
                DocumentCore  dc       = DocumentCore.Load(filename);
                StringBuilder sb       = new StringBuilder();
                // Get content of each Run where the text color is Red.
                foreach (Paragraph run in dc.GetChildElements(true, ElementType.Paragraph))
                {
                    string   str     = run.Content.ToString();
                    string[] strpath = str.Split('\r'); // Не удавалось расплитить по '\r\n'. Расплититл по '\r'.
                    str = strpath[0];                   // Хвост отбросил.
                    if (str != "")                      // Проверка на какой-либо элемент. В том числе и филды.
                    {
                        lS1.Add(str);                   // Список всех элементов документа, где каждый первый - Id, а каждый второй - дата.
                    }
                }
                ToTable TTable    = new ToTable();
                string  FieldId   = lS1[0];
                string  FieldData = lS1[1];
                lS1.Remove(FieldId);
                lS1.Remove(FieldData);
                DataTable DT = TTable.ConvertToTable(lS1, FieldId, FieldData, GetPathAndName(opf.FileName)[1]);
                dataGridView1.DataSource = DT;
            }
        }
Beispiel #29
0
        /// <summary>
        /// Replace all Run elements with Bold formatting to Italic and mark them by yellow.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/manipulation.php
        /// </remarks>
        static void Manipulation()
        {
            string       filePath       = @"..\..\example.docx";
            DocumentCore dc             = DocumentCore.Load(filePath);
            string       filePathResult = @"Result-file.pdf";

            foreach (Run run in dc.GetChildElements(true, ElementType.Run))
            {
                if (run.CharacterFormat.Bold == true)
                {
                    run.CharacterFormat.Bold            = false;
                    run.CharacterFormat.Italic          = true;
                    run.CharacterFormat.BackgroundColor = Color.Yellow;
                }
            }
            dc.Save(filePathResult);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath)
            {
                UseShellExecute = true
            });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePathResult)
            {
                UseShellExecute = true
            });
        }
Beispiel #30
0
        // How to modify an existing table in a document.
        public static void ModifyTable()
        {
            string sourcePath = @"..\..\..\..\..\..\Testing Files\table.docx";
            string destPath   = Path.ChangeExtension(sourcePath, ".modified.pdf");

            // Load a document with a table.
            DocumentCore dc = DocumentCore.Load(sourcePath);

            // Find a first table in the document.
            Table table = (Table)dc.GetChildElements(true, ElementType.Table).First();

            // Set dashed borders and yellow background for all cells.
            for (int r = 0; r < table.Rows.Count; r++)
            {
                for (int c = 0; c < table.Rows[r].Cells.Count; c++)
                {
                    TableCell cell = table.Rows[r].Cells[c];
                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dashed, Color.Black, 1);
                    cell.CellFormat.BackgroundColor = new Color("#FFCC00");
                }
            }

            // Save the document as PDF.
            dc.Save(destPath, new PdfSaveOptions());

            // Show the source and the dest documents.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(sourcePath)
            {
                UseShellExecute = true
            });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(destPath)
            {
                UseShellExecute = true
            });
        }