예제 #1
0
        static void WriteTest()
        {
            using (PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument())
            {
                document.Info.Title    = "Family Tree";
                document.Info.Author   = "FamilyTree Ltd. - Stefan Steiger";
                document.Info.Subject  = "Family Tree";
                document.Info.Keywords = "Family Tree, Genealogical Tree, Genealogy, Bloodline, Pedigree";


                PdfSharp.Pdf.Security.PdfSecuritySettings securitySettings = document.SecuritySettings;

                // Setting one of the passwords automatically sets the security level to
                // PdfDocumentSecurityLevel.Encrypted128Bit.
                securitySettings.UserPassword  = "******";
                securitySettings.OwnerPassword = "******";

                // Don't use 40 bit encryption unless needed for compatibility
                //securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;

                // Restrict some rights.
                securitySettings.PermitAccessibilityExtractContent = false;
                securitySettings.PermitAnnotations      = false;
                securitySettings.PermitAssembleDocument = false;
                securitySettings.PermitExtractContent   = false;
                securitySettings.PermitFormsFill        = true;
                securitySettings.PermitFullQualityPrint = false;
                securitySettings.PermitModifyDocument   = true;
                securitySettings.PermitPrint            = false;



                document.ViewerPreferences.Direction = PdfSharp.Pdf.PdfReadingDirection.LeftToRight;
                PdfSharp.Pdf.PdfPage page = document.AddPage();

                // page.Width = PdfSettings.PaperFormatSettings.Width
                // page.Height = PdfSettings.PaperFormatSettings.Height

                page.Orientation = PdfSharp.PageOrientation.Landscape;
                double dblLineWidth = 1.0;
                string strHtmlColor = "#FF00FF";
                PdfSharp.Drawing.XColor lineColor = XColorHelper.FromHtml(strHtmlColor);
                PdfSharp.Drawing.XPen   pen       = new PdfSharp.Drawing.XPen(lineColor, dblLineWidth);

                PdfSharp.Drawing.XFont font = new PdfSharp.Drawing.XFont("Arial"
                                                                         , 12.0, PdfSharp.Drawing.XFontStyle.Bold
                                                                         );



                using (PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page))
                {
                    gfx.MUH = PdfSharp.Pdf.PdfFontEncoding.Unicode;

                    PdfSharp.Drawing.Layout.XTextFormatter tf = new PdfSharp.Drawing.Layout.XTextFormatter(gfx);
                    tf.Alignment = PdfSharp.Drawing.Layout.XParagraphAlignment.Left;

                    PdfSharp.Drawing.Layout.XTextFormatterEx2 etf = new PdfSharp.Drawing.Layout.XTextFormatterEx2(gfx);


                    gfx.DrawRectangle(pen, new PdfSharp.Drawing.XRect(100, 100, 100, 100));

                    using (PdfSharp.Drawing.XImage img = PdfSharp.Drawing.XImage.FromFile(@"D:\username\Documents\Visual Studio 2017\Projects\PdfSharp\TestApplication\Wikipedesketch1.png"))
                    {
                        gfx.DrawImage(img, 500, 500);
                    }

                    string text = "Lalala";

                    tf.DrawString(text
                                  , font
                                  , PdfSharp.Drawing.XBrushes.Black
                                  , new PdfSharp.Drawing.XRect(300, 300, 100, 100)
                                  , PdfSharp.Drawing.XStringFormats.TopLeft
                                  );
                } // End Using gfx

                // Save the document...
                string filename = "TestFilePwProtected.pdf";
                document.Save(filename);
                // ...and start a viewer.
                System.Diagnostics.Process.Start(filename);
            } // End Using document
        }     // End Sub WriteTest
        // StaticConvertFromString
        // https://github.com/mono/mono/blob/master/mcs/class/System.Drawing/System.Drawing/ColorConverter.cs
        public static PdfSharp.Drawing.XColor FromHtml(string htmlColor)
        {
            if (string.IsNullOrEmpty(htmlColor))
            {
                // return PdfSharp.Drawing.XColor.FromArgb(System.Drawing.Color.Empty);
                return(PdfSharp.Drawing.XColor.Empty);
            }

            if (htmlColor.StartsWith("#"))
            {
                htmlColor = htmlColor.Substring(1);

                if (htmlColor.Length == 3)
                {
                    string R = htmlColor.Substring(0, 1);
                    string G = htmlColor.Substring(1, 1);
                    string B = htmlColor.Substring(2, 1);
                    htmlColor = System.Convert.ToString(System.Convert.ToString(System.Convert.ToString(System.Convert.ToString(R + R) + G) + G) + B) + B;
                }
                // (htmlColor.Length == 3)

                if (htmlColor.Length == 6)
                {
                    string sR = htmlColor.Substring(0, 2);
                    string sG = htmlColor.Substring(2, 2);
                    string sB = htmlColor.Substring(4, 2);

                    byte R = System.Convert.ToByte(sR, 16);
                    byte G = System.Convert.ToByte(sG, 16);
                    byte B = System.Convert.ToByte(sB, 16);

                    PdfSharp.Drawing.XColor xc = new PdfSharp.Drawing.XColor();
                    xc.R = R;
                    xc.G = G;
                    xc.B = B;
                    xc.A = 1.0;
                    return(xc);
                }
                // (htmlColor.Length == 6)

                throw new ArgumentException("Invalid HTML color.");
            }
            // (htmlColor.StartsWith("#"))

            switch (htmlColor.ToLowerInvariant())
            {
            case "buttonface":
            case "threedface":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.SystemColors.Control));

            case "buttonhighlight":
            case "threedlightshadow":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.SystemColors.ControlLightLight));

            case "buttonshadow":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.SystemColors.ControlDark));

            case "captiontext":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.SystemColors.ActiveCaptionText));

            case "threeddarkshadow":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.SystemColors.ControlDarkDark));

            case "threedhighlight":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.SystemColors.ControlLight));

            case "background":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.SystemColors.Desktop));

            case "buttontext":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.SystemColors.ControlText));

            case "infobackground":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.SystemColors.Info));

            // special case for Color.LightGray versus html's LightGrey (#340917)
            case "lightgrey":
                return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.Color.LightGray));
            } // End Switch (htmlColor.ToLowerInvariant())


            PdfSharp.Drawing.XKnownColor kk = default(PdfSharp.Drawing.XKnownColor);
            if (TryEnumParse <PdfSharp.Drawing.XKnownColor>(htmlColor, true, ref kk))
            {
                return(PdfSharp.Drawing.XColor.FromKnownColor(kk));
            }

            return(PdfSharp.Drawing.XColor.FromArgb(System.Drawing.Color.Empty));
        } // FromHtml