示例#1
0
        static void Main()
        {
            #region Create PDF

            Console.WriteLine("Creating a PDF from scratch...");
            PdfPage page;
            XFont   font;
            using (var document = new PdfDocument())
            {
                document.Info.Title = "Created with PDFsharp";
                // Create an empty page
                page = document.AddPage();

                // Get an XGraphics object for drawing
                var gfx = XGraphics.FromPdfPage(page);

                // Create a font
                //font = new XFont("Courier", 20, XFontStyle.BoldItalic);
                font = XFont.Default(20); // this should short-cut all the loading and caching, just let the PDF renderer guess

                // Draw the text
                gfx.DrawString("Hello, World! ©®£¥§", font, XBrushes.Black, // the end characters aren't in the fall-back font
                               new XRect(0, 0, page.Width, page.Height),
                               XStringFormats.Center);

                Console.WriteLine("Saving...");
                // Save the document...
                const string filename = "HelloWorld.pdf";
                document.Save(filename);
            }

            #endregion

            #region Open, interpret, and edit an existing PDF

            Console.WriteLine("Trying to open an existing PDF");

            var sw = new Stopwatch();
            sw.Start();
            var targetFile = "finland";
            using (var existing = PdfReader.Open($"{targetFile}.pdf"))
            {
                var form = existing.AcroForm;
                if (form == null)
                {
                    Console.WriteLine("No form found");
                }
                else
                {
                    Console.WriteLine($"Found {form.Fields.Count} form fields");
                    foreach (var afName in form.Fields.Names)
                    {
                        var field     = form.Fields[afName];
                        var pageNum   = 0;
                        var maybePage = field?.GetPage(out pageNum);
                        Console.WriteLine($"Field def for page {pageNum} / {(maybePage==null?"not found":"found")}:");
                        LogPdfItem(field);
                    }
                }

                Console.WriteLine($"Found {existing.FullPath}: {existing.Pages.Count} pages");
                int pageNumber = 0;
                foreach (var ePage in existing.Pages)
                {
                    pageNumber++;

                    ExtractImages(ePage);

                    var bits = ePage !.Contents.ToList();
                    foreach (var item in bits)
                    {
                        Console.WriteLine("------------");

                        LogPdfItem(item);
                    }

                    Console.WriteLine("==================================================");

                    // Try recovering some of the page to an image (proper PDF rendering)
                    AttemptToRenderPage(ePage, pageNumber); // TODO: implement properly. This is *REALLY* not working yet

                    using (var g = XGraphics.FromPdfPage(ePage))
                    {
                        g.DrawString("Hello, World!", font, XBrushes.Black,
                                     new XRect(0, 0, page.Width, page.Height),
                                     XStringFormats.Center);
                    }
                }

                existing.Save($"{targetFile}-edited.pdf");
            }

            sw.Stop();
            Console.WriteLine($"Opening editing and writing PDF took {sw.Elapsed}");

            #endregion

            #region Concatenate two existing PDFs into a new document

            Console.WriteLine("Trying to concat documents");

            for (int i = 1; i <= 3; i++)
            {
                MakeSmallDoc(font, $"_concat_src_{i}.pdf");
            }

            using (var concatOut = new PdfDocument())
            {
                concatOut.Info.Title = "Joined with PdfSharpStd";

                for (int i = 1; i <= 3; i++)
                {
                    Console.WriteLine($"    Reading document {i}");
                    using (var srcDoc = PdfReader.Open($"_concat_src_{i}.pdf", PdfDocumentOpenMode.Import)) // Must use import mode to copy pages across
                    {
                        var p = 0;
                        foreach (var pdfPage in srcDoc.Pages)
                        {
                            p++;
                            if (pdfPage is null)
                            {
                                continue;
                            }
                            concatOut.AddPage(pdfPage);
                            Console.WriteLine($"        copied page {p}");
                        }
                    }
                }

                Console.WriteLine("    Saving result");
                concatOut.Save("Concatenated.pdf");
            }

            #endregion

            #region Read-back a file made from a concatenated set of sources

            Console.WriteLine("Trying to read previously merged pdf");
            using (var concatSrc = PdfReader.Open("Concatenated.pdf"))
            {
                Console.WriteLine($"Concat doc has {concatSrc.Pages.Count} pages");
            }

            #endregion

            Console.WriteLine("Done.");
        }