Example #1
0
        public static void Run()
        {
            // ExStart:AddTOC
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

            // Load an existing PDF files
            Document doc = new Document(dataDir + "AddTOC.pdf");

            // Get access to first page of PDF file
            Page tocPage = doc.Pages.Insert(1);

            // Create object to represent TOC information
            TocInfo      tocInfo = new TocInfo();
            TextFragment title   = new TextFragment("Table Of Contents");

            title.TextState.FontSize  = 20;
            title.TextState.FontStyle = FontStyles.Bold;

            // Set the title for TOC
            tocInfo.Title   = title;
            tocPage.TocInfo = tocInfo;

            // Create string objects which will be used as TOC elements
            string[] titles = new string[4];
            titles[0] = "First page";
            titles[1] = "Second page";
            titles[2] = "Third page";
            titles[3] = "Fourth page";
            for (int i = 0; i < 2; i++)
            {
                // Create Heading object
                Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
                TextSegment        segment2 = new TextSegment();
                heading2.TocPage = tocPage;
                heading2.Segments.Add(segment2);

                // Specify the destination page for heading object
                heading2.DestinationPage = doc.Pages[i + 2];

                // Destination page
                heading2.Top = doc.Pages[i + 2].Rect.Height;

                // Destination coordinate
                segment2.Text = titles[i];

                // Add heading to page containing TOC
                tocPage.Paragraphs.Add(heading2);
            }
            dataDir = dataDir + "TOC_out.pdf";
            // Save the updated document
            doc.Save(dataDir);
            // ExEnd:AddTOC
            Console.WriteLine("\nTOC added successfully to an existing PDF.\nFile saved at " + dataDir);
        }
Example #2
0
        public static void Run()
        {
            // ExStart:AddTOC
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

            // Load an existing PDF files
            Document doc = new Document(dataDir + "AddTOC.pdf");

            // Get access to first page of PDF file
            Page tocPage = doc.Pages.Insert(1);

            // Create object to represent TOC information
            TocInfo tocInfo = new TocInfo();
            TextFragment title = new TextFragment("Table Of Contents");
            title.TextState.FontSize = 20;
            title.TextState.FontStyle = FontStyles.Bold;

            // Set the title for TOC
            tocInfo.Title = title;
            tocPage.TocInfo = tocInfo;

            // Create string objects which will be used as TOC elements
            string[] titles = new string[4];
            titles[0] = "First page";
            titles[1] = "Second page";
            titles[2] = "Third page";
            titles[3] = "Fourth page";
            for (int i = 0; i < 2; i++)
            {
                // Create Heading object
                Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
                TextSegment segment2 = new TextSegment();
                heading2.TocPage = tocPage;
                heading2.Segments.Add(segment2);

                // Specify the destination page for heading object
                heading2.DestinationPage = doc.Pages[i + 2];

                // Destination page
                heading2.Top = doc.Pages[i + 2].Rect.Height;

                // Destination coordinate
                segment2.Text = titles[i];

                // Add heading to page containing TOC
                tocPage.Paragraphs.Add(heading2);
            }
            dataDir = dataDir + "TOC_out.pdf";
            // Save the updated document
            doc.Save(dataDir);
            // ExEnd:AddTOC
            Console.WriteLine("\nTOC added successfully to an existing PDF.\nFile saved at " + dataDir);
        }
        public static void Main(string[] args)
        {
            // The path to the documents directory.
            string dataDir = Path.GetFullPath("../../../Data/");

            // Load an existing PDF files
            Document doc = new Document(dataDir + "input.pdf");

            // Get access to first page of PDF file
            Page tocPage = doc.Pages.Insert(1);

            // Create object to represent TOC information
            TocInfo      tocInfo = new TocInfo();
            TextFragment title   = new TextFragment("Table Of Contents");

            title.TextState.FontSize  = 20;
            title.TextState.FontStyle = FontStyles.Bold;

            // Set the title for TOC
            tocInfo.Title   = title;
            tocPage.TocInfo = tocInfo;

            // Create string objects which will be used as TOC elements
            string[] titles = new string[4];
            titles[0] = "First page";
            titles[1] = "Second page";
            titles[2] = "Third page";
            titles[3] = "Fourth page";
            for (int i = 0; i < 2; i++)
            {
                // Create Heading object
                Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
                TextSegment        segment2 = new TextSegment();
                heading2.TocPage = tocPage;
                heading2.Segments.Add(segment2);

                // Specify the destination page for heading object
                heading2.DestinationPage = doc.Pages[i + 2];

                // Destination page
                heading2.Top = doc.Pages[i + 2].Rect.Height;

                // Destination coordinate
                segment2.Text = titles[i];

                // Add heading to page containing TOC
                tocPage.Paragraphs.Add(heading2);
            }
            // Save the updated document
            doc.Save(dataDir + "TOC_Output.pdf");
        }
        public static void Main(string[] args)
        {
            // The path to the documents directory.
            string dataDir = Path.GetFullPath("../../../Data/");

            // Load an existing PDF files
            Document doc = new Document(dataDir + "input.pdf");

            // Get access to first page of PDF file
            Page tocPage = doc.Pages.Insert(1);

            // Create object to represent TOC information
            TocInfo tocInfo = new TocInfo();
            TextFragment title = new TextFragment("Table Of Contents");
            title.TextState.FontSize = 20;
            title.TextState.FontStyle = FontStyles.Bold;

            // Set the title for TOC
            tocInfo.Title = title;
            tocPage.TocInfo = tocInfo;

            // Create string objects which will be used as TOC elements
            string[] titles = new string[4];
            titles[0] = "First page";
            titles[1] = "Second page";
            titles[2] = "Third page";
            titles[3] = "Fourth page";
            for (int i = 0; i < 2; i++)
            {
                // Create Heading object
                Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
                TextSegment segment2 = new TextSegment();
                heading2.TocPage = tocPage;
                heading2.Segments.Add(segment2);

                // Specify the destination page for heading object
                heading2.DestinationPage = doc.Pages[i + 2];

                // Destination page
                heading2.Top = doc.Pages[i + 2].Rect.Height;

                // Destination coordinate
                segment2.Text = titles[i];

                // Add heading to page containing TOC
                tocPage.Paragraphs.Add(heading2);
            }
            // Save the updated document
            doc.Save(dataDir + "TOC_Output.pdf");
        }
        private void btnCreateTOC_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstTitles.Items.Count > 0)
                {
                    Document doc     = new Document(txtPDF.Text);
                    Page     tocPage = doc.Pages.Insert(1);

                    // Create object to represent TOC information
                    TocInfo      tocInfo = new TocInfo();
                    TextFragment title   = new TextFragment("Table Of Contents");
                    title.TextState.FontSize  = 20;
                    title.TextState.FontStyle = FontStyles.Bold;

                    // Set the title for TOC
                    tocInfo.Title   = title;
                    tocPage.TocInfo = tocInfo;
                    foreach (var item in lstTitles.Items)
                    {
                        TextFragmentAbsorber absorber = new TextFragmentAbsorber(item.ToString());
                        doc.Pages.Accept(absorber);
                        Page targetPage = doc.Pages[absorber.TextFragments[1].Page.Number];
                        // Create Heading object
                        Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
                        TextSegment        segment2 = new TextSegment();
                        heading2.TocPage = tocPage;
                        heading2.Segments.Add(segment2);
                        // Specify the destination page for heading object
                        heading2.DestinationPage = targetPage;
                        // Destination page
                        heading2.Top = targetPage.Rect.Height;
                        // Destination coordinate
                        segment2.Text = item.ToString();
                        // Add heading to page containing TOC
                        tocPage.Paragraphs.Add(heading2);
                    }
                    doc.Save("Toc_out.pdf");
                    MessageBox.Show("PDF with TOC has been created.", "Success");
                }
                else
                {
                    MessageBox.Show("Please extract titles from PDF document first.", "Titles are not extracted");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
        public static void Run()
        {
            // ExStart:ApplyNumberStyle
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_Headings();

            Document pdfDoc = new Document();
            pdfDoc.PageInfo.Width = 612.0;
            pdfDoc.PageInfo.Height = 792.0;
            pdfDoc.PageInfo.Margin = new Aspose.Pdf.MarginInfo();
            pdfDoc.PageInfo.Margin.Left = 72;
            pdfDoc.PageInfo.Margin.Right = 72;
            pdfDoc.PageInfo.Margin.Top = 72;
            pdfDoc.PageInfo.Margin.Bottom = 72;

            Aspose.Pdf.Page pdfPage = pdfDoc.Pages.Add();
            pdfPage.PageInfo.Width = 612.0;
            pdfPage.PageInfo.Height = 792.0;
            pdfPage.PageInfo.Margin = new Aspose.Pdf.MarginInfo();
            pdfPage.PageInfo.Margin.Left = 72;
            pdfPage.PageInfo.Margin.Right = 72;
            pdfPage.PageInfo.Margin.Top = 72;
            pdfPage.PageInfo.Margin.Bottom = 72;

            Aspose.Pdf.FloatingBox floatBox = new Aspose.Pdf.FloatingBox();
            floatBox.Margin = pdfPage.PageInfo.Margin;

            pdfPage.Paragraphs.Add(floatBox);

            TextFragment textFragment = new TextFragment();
            TextSegment segment = new TextSegment();

            Aspose.Pdf.Heading heading = new Aspose.Pdf.Heading(1);
            heading.IsInList = true;
            heading.StartNumber = 1;
            heading.Text = "List 1";
            heading.Style = NumberingStyle.NumeralsRomanLowercase;
            heading.IsAutoSequence = true;

            floatBox.Paragraphs.Add(heading);

            Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
            heading2.IsInList = true;
            heading2.StartNumber = 13;
            heading2.Text = "List 2";
            heading2.Style = NumberingStyle.NumeralsRomanLowercase;
            heading2.IsAutoSequence = true;

            floatBox.Paragraphs.Add(heading2);

            Aspose.Pdf.Heading heading3 = new Aspose.Pdf.Heading(2);
            heading3.IsInList = true;
            heading3.StartNumber = 1;
            heading3.Text = "the value, as of the effective date of the plan, of property to be distributed under the plan onaccount of each allowed";
            heading3.Style = NumberingStyle.LettersLowercase;
            heading3.IsAutoSequence = true;

            floatBox.Paragraphs.Add(heading3);
            dataDir = dataDir + "ApplyNumberStyle_out.pdf";
            pdfDoc.Save(dataDir);
            // ExEnd:ApplyNumberStyle
            Console.WriteLine("\nNumber style applied successfully in headings.\nFile saved at " + dataDir);            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            CreatePDFWithTable();

            return;

            // ExStart:ApplyNumberStyle
            // The path to the documents directory.
            string dataDir = @"..\pdfpages\sample.pdf";

            Document pdfDoc = new Document(dataDir);

            pdfDoc.PageInfo.Width         = 612.0;
            pdfDoc.PageInfo.Height        = 792.0;
            pdfDoc.PageInfo.Margin        = new Aspose.Pdf.MarginInfo();
            pdfDoc.PageInfo.Margin.Left   = 72;
            pdfDoc.PageInfo.Margin.Right  = 72;
            pdfDoc.PageInfo.Margin.Top    = 72;
            pdfDoc.PageInfo.Margin.Bottom = 72;

            Aspose.Pdf.Page pdfPage = pdfDoc.Pages.Add();
            pdfPage.PageInfo.Width         = 612.0;
            pdfPage.PageInfo.Height        = 792.0;
            pdfPage.PageInfo.Margin        = new Aspose.Pdf.MarginInfo();
            pdfPage.PageInfo.Margin.Left   = 72;
            pdfPage.PageInfo.Margin.Right  = 72;
            pdfPage.PageInfo.Margin.Top    = 2;
            pdfPage.PageInfo.Margin.Bottom = 72;

            Aspose.Pdf.FloatingBox floatBox = new Aspose.Pdf.FloatingBox();
            floatBox.Margin = pdfPage.PageInfo.Margin;

            pdfPage.Paragraphs.Add(floatBox);

            TextFragment textFragment = new TextFragment();
            TextSegment  segment      = new TextSegment();

            Aspose.Pdf.Heading heading = new Aspose.Pdf.Heading(1);
            heading.IsInList       = true;
            heading.StartNumber    = 1;
            heading.Text           = "List 1";
            heading.Style          = NumberingStyle.NumeralsRomanLowercase;
            heading.IsAutoSequence = true;

            floatBox.Paragraphs.Add(heading);

            Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
            heading2.IsInList       = true;
            heading2.StartNumber    = 13;
            heading2.Text           = "List 2";
            heading2.Style          = NumberingStyle.NumeralsRomanLowercase;
            heading2.IsAutoSequence = true;

            floatBox.Paragraphs.Add(heading2);

            Aspose.Pdf.Heading heading3 = new Aspose.Pdf.Heading(2);
            heading3.IsInList       = true;
            heading3.StartNumber    = 1;
            heading3.Text           = "the value, as of the effective date of the plan, of property to be distributed under the plan onaccount of each allowed";
            heading3.Style          = NumberingStyle.LettersLowercase;
            heading3.IsAutoSequence = true;

            floatBox.Paragraphs.Add(heading3);
            dataDir = dataDir + "9992.pdf";
            pdfDoc.Save(dataDir);
            // ExEnd:ApplyNumberStyle
            Console.WriteLine("\nNumber style applied successfully in headings.\nFile saved at " + dataDir);
        }
        protected void btnMergeAllFiles_Click(object sender, EventArgs e)
        {
            try
            {
                //delete file if already exists...
                if (File.Exists(Server.MapPath("~/Uploads/Catalog.pdf")))
                {
                    File.Delete(Server.MapPath("~/Uploads/Catalog.pdf"));
                }
                // create new document to save the catalog...
                Document doc = new Document();

                //get all the applications in the directory
                string[] pdfFiles = Directory.GetFiles(Server.MapPath("~/Uploads"), "*.pdf")
                       .Select(path => Path.GetFileName(path))
                       .ToArray();
                if (pdfFiles.Count() == 0)
                {
                    msg.Text = "<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>There is currently no application available.</div>";
                    return;
                }
                else
                {
                    foreach (string val in pdfFiles)
                    {
                        //get the pdf document..
                        Document application = new Document(Server.MapPath("~/Uploads/" + val));
                        //merge the pages in catalog document...
                        doc.Pages.Add(application.Pages);
                    }
                    //====================================== Adding Table of Content Starts =========================================
                    //add TOC page...
                    Aspose.Pdf.Page tocPage = doc.Pages.Insert(1);
                    //add watermark on table of content
                    AddWaterMark(tocPage);
                    // Create object to represent TOC information
                    TocInfo tocInfo = new TocInfo();
                    TextFragment title = new TextFragment("Table Of Contents");
                    title.TextState.FontSize = 20;
                    title.TextState.FontStyle = FontStyles.Bold;

                    // Set the title for TOC
                    tocInfo.Title = title;
                    tocPage.TocInfo = tocInfo;

                    for (int i = 0; i < pdfFiles.Count(); i++)
                    {
                        // Create Heading object
                        Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
                        TextSegment segment2 = new TextSegment();
                        heading2.TocPage = tocPage;
                        heading2.Segments.Add(segment2);

                        // Specify the destination page for heading object
                        heading2.DestinationPage = doc.Pages[i + 2];

                        // Destination page
                        heading2.Top = doc.Pages[i + 2].Rect.Height;

                        // Destination coordinate
                        segment2.Text = "Application Form No. " + (i + 1).ToString();

                        // Add heading to page containing TOC
                        tocPage.Paragraphs.Add(heading2);
                    }
                    //===================================== TOC Ends ==========================================

                    //save the final catalog file...
                    string catalogpath = Server.MapPath("~/Catalogs/Catalog.pdf");
                    doc.Save(catalogpath);
                    msg.Text = "<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button>Applications have been saved in catalog.</div>";
                    //show success message
                }
            }
            catch (Exception exp)
            {
                msg.Text = "<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>Exception Occured:" + exp.Message + "</div>";
            }
        }
        private void btnCreatePDF_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstTitles.Items.Count > 0)
                {
                    Dictionary <string, List <int> > titles = new Dictionary <string, List <int> >();
                    List <string> ts  = new List <string>();
                    Document      doc = new Document(txtPDF.Text);
                    foreach (Page page in doc.Pages)
                    {
                        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
                        page.Accept(absorber);
                        if (titles.Where(x => x.Key == absorber.TextFragments[1].Text).ToList().Count > 0)
                        {
                            titles[absorber.TextFragments[1].Text].Add(absorber.TextFragments[1].Page.Number);
                            ts.Remove(absorber.TextFragments[1].Text);
                        }
                        else
                        {
                            titles.Add(absorber.TextFragments[1].Text, new List <int>()
                            {
                                absorber.TextFragments[1].Page.Number
                            });
                            ts.Add(absorber.TextFragments[1].Text);
                        }
                    }
                    // Get chapters which are more than 1 page
                    var        pages = titles.Where(x => x.Value.Count > 1).Select(x => x.Value).ToList();
                    List <int> ps    = new List <int>();
                    if (pages.Count > 0)
                    {
                        foreach (var page in pages)
                        {
                            Document newDoc = new Document();
                            foreach (var p in page)
                            {
                                newDoc.Pages.Add(doc.Pages[p]);
                                ps.Add(p);
                            }
                            newDoc.Save("newPDF_" + DateTime.Now.Millisecond + ".pdf");
                        }
                        // Delete extracted pages from existing PDF
                        doc.Pages.Delete(ps.ToArray());
                        doc.Save("tmp.pdf");
                    }
                    // Create TOC for remaining pages
                    doc = new Document("tmp.pdf");
                    Page tocPage = doc.Pages.Insert(1);

                    // Create object to represent TOC information
                    TocInfo      tocInfo = new TocInfo();
                    TextFragment title   = new TextFragment("Table Of Contents");
                    title.TextState.FontSize  = 20;
                    title.TextState.FontStyle = FontStyles.Bold;

                    // Set the title for TOC
                    tocInfo.Title   = title;
                    tocPage.TocInfo = tocInfo;
                    foreach (var s in ts)
                    {
                        TextFragmentAbsorber absorber = new TextFragmentAbsorber(s);
                        doc.Pages.Accept(absorber);
                        Page targetPage = doc.Pages[absorber.TextFragments[1].Page.Number];
                        // Create Heading object
                        Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
                        TextSegment        segment2 = new TextSegment();
                        heading2.TocPage = tocPage;
                        heading2.Segments.Add(segment2);
                        // Specify the destination page for heading object
                        heading2.DestinationPage = targetPage;
                        // Destination page
                        heading2.Top = targetPage.Rect.Height;
                        // Destination coordinate
                        segment2.Text = s;
                        // Add heading to page containing TOC
                        tocPage.Paragraphs.Add(heading2);
                    }
                    doc.Save("Toc_" + DateTime.Now.Millisecond + ".pdf");
                    File.Delete("tmp.pdf");
                    MessageBox.Show("PDF with TOC has been created.", "Success");
                }
                else
                {
                    MessageBox.Show("Please extract titles from PDF document first.", "Titles are not extracted");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }