Exemple #1
0
        public static void ReadPdf()
        {
            // Get a fresh copy of the sample PDF file
            string filename = @"C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\R_SERVICES\doc\manual\R-intro.pdf";

            // Create the output document
            PdfSharp.Pdf.PdfDocument outputDocument =
                new PdfSharp.Pdf.PdfDocument();

            // Show single pages
            // (Note: one page contains two pages from the source document)
            outputDocument.PageLayout = PdfSharp.Pdf.PdfPageLayout.SinglePage;

            /*
             * PdfSharp.Drawing.XFont font =
             *  new PdfSharp.Drawing.XFont("Verdana", 8, PdfSharp.Drawing.XFontStyle.Bold);
             * PdfSharp.Drawing.XStringFormat format = new PdfSharp.Drawing.XStringFormat();
             * format.Alignment = PdfSharp.Drawing.XStringAlignment.Center;
             * format.LineAlignment = PdfSharp.Drawing.XLineAlignment.Far;
             */
            PdfSharp.Drawing.XGraphics gfx;
            PdfSharp.Drawing.XRect     box;

            // Open the external document as XPdfForm object
            PdfSharp.Drawing.XPdfForm form =
                PdfSharp.Drawing.XPdfForm.FromFile(filename);

            for (int idx = 0; idx < form.PageCount; idx += 2)
            {
                // Add a new page to the output document
                PdfSharp.Pdf.PdfPage page = outputDocument.AddPage();
                page.Orientation = PdfSharp.PageOrientation.Landscape;
                double width  = page.Width;
                double height = page.Height;

                int rotate = page.Elements.GetInteger("/Rotate");

                gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);

                // Set page number (which is one-based)
                form.PageNumber = idx + 1;

                box = new PdfSharp.Drawing.XRect(0, 0, width / 2, height);
                // Draw the page identified by the page number like an image
                gfx.DrawImage(form, box);


                // Write document file name and page number on each page
                box.Inflate(0, -10);

                /*
                 * gfx.DrawString(string.Format("- {1} -", filename, idx + 1),
                 *   font, PdfSharp.Drawing.XBrushes.Red, box, format);
                 */
                if (idx + 1 < form.PageCount)
                {
                    // Set page number (which is one-based)
                    form.PageNumber = idx + 2;

                    box = new PdfSharp.Drawing.XRect(width / 2, 0, width / 2, height);
                    // Draw the page identified by the page number like an image
                    gfx.DrawImage(form, box);

                    // Write document file name and page number on each page
                    box.Inflate(0, -10);

                    /*
                     * gfx.DrawString(string.Format("- {1} -", filename, idx + 2),
                     *  font, PdfSharp.Drawing.XBrushes.Red, box, format);
                     */
                }
            }

            // Save the document...
            filename = "TwoPagesOnOne_tempfile.pdf";
            outputDocument.Save(filename);
            // ...and start a viewer.
            System.Diagnostics.Process.Start(filename);
        }
        public static string makeIt(string out_name, string source, ID_MACROS.IDS.PAGE_MODE md)
        {
            if (md == ID_MACROS.IDS.PAGE_MODE.mode1)
            {
                string                         filename       = source;
                PdfDocument                    outputDocument = new PdfDocument();
                PdfSharp.Drawing.XFont         font           = new PdfSharp.Drawing.XFont("Times New Roman", 10, PdfSharp.Drawing.XFontStyle.Regular);
                PdfSharp.Drawing.XStringFormat format         = new PdfSharp.Drawing.XStringFormat();
                format.Alignment     = PdfSharp.Drawing.XStringAlignment.Near;
                format.LineAlignment = PdfSharp.Drawing.XLineAlignment.Far;
                PdfSharp.Drawing.XGraphics gfx;
                PdfSharp.Drawing.XRect     box;
                PdfSharp.Drawing.XPdfForm  form = PdfSharp.Drawing.XPdfForm.FromFile(filename);
                for (int idx = 0; idx < form.PageCount; idx++)
                {
                    PdfPage page   = outputDocument.AddPage();
                    double  width  = page.Width;
                    double  height = page.Height;
                    gfx             = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
                    form.PageNumber = idx + 1;
                    box             = new PdfSharp.Drawing.XRect(0, 0, width, height);
                    gfx.DrawImage(form, box);
                    box.Inflate(0, -30);
                }
                filename = out_name;
                outputDocument.Save(filename);
                return(filename);
            }
            else
            {
                int[]                          lst            = PDF_MAKER.PDF_MAKER.Page_Number_List;
                string                         filename       = source;
                PdfDocument                    outputDocument = new PdfDocument();
                PdfSharp.Drawing.XFont         font           = new PdfSharp.Drawing.XFont("Times New Roman", 10, PdfSharp.Drawing.XFontStyle.Regular);
                PdfSharp.Drawing.XStringFormat format         = new PdfSharp.Drawing.XStringFormat();
                format.Alignment     = PdfSharp.Drawing.XStringAlignment.Center;
                format.LineAlignment = PdfSharp.Drawing.XLineAlignment.Far;
                PdfSharp.Drawing.XGraphics gfx;
                PdfSharp.Drawing.XRect     box;
                PdfSharp.Drawing.XPdfForm  form = PdfSharp.Drawing.XPdfForm.FromFile(filename);
                for (int idx = 0; idx < form.PageCount; idx++)
                {
                    PdfPage page   = outputDocument.AddPage();
                    double  width  = page.Width;
                    double  height = page.Height;
                    gfx             = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
                    form.PageNumber = idx + 1;
                    box             = new PdfSharp.Drawing.XRect(0, 0, width, height);
                    gfx.DrawImage(form, box);
                    box.Inflate(0, -30);

                    if (lst[idx] == 2)
                    {
                        gfx.DrawString(String.Format("{1}", filename, idx + 1), font, PdfSharp.Drawing.XBrushes.Black, box, format);
                    }
                    else if (lst[idx] == 1)
                    {
                        gfx.DrawString(String.Format("{1}", filename, PAGE_MODE_II_NUMBERS.RomanNumbers.convert(idx + 1)), font, PdfSharp.Drawing.XBrushes.Black, box, format);
                    }
                    else
                    {
                    }
                }
                filename = out_name;
                outputDocument.Save(filename);
                return(filename);
            }
        }