static void Main(string[] args)
        {
            Console.WriteLine("Watermark Sample:");
            using (Library lib = new Library()) {
                Console.WriteLine("Initialized the library.");

                String sInput     = "../../Resources/Sample_Input/sample.pdf";
                String sWatermark = "../../Resources/Sample_Input/ducky.pdf";
                String sOutput    = "../Watermark-out.pdf";

                if (args.Length > 0)
                {
                    sInput = args[0];
                }

                if (args.Length > 1)
                {
                    sWatermark = args[1];
                }

                if (args.Length > 2)
                {
                    sOutput = args[2];
                }

                Console.WriteLine("Adding watermark from " + sWatermark + " to " + sInput + " and saving to " + sOutput);

                Document doc = new Document(sInput);

                Document watermarkDoc = new Document(sWatermark);

                WatermarkParams watermarkParams = new WatermarkParams();
                watermarkParams.Opacity              = 0.8f;
                watermarkParams.Rotation             = 45.3f;
                watermarkParams.Scale                = 0.5f;
                watermarkParams.TargetRange.PageSpec = PageSpec.EvenPagesOnly;

                doc.Watermark(watermarkDoc.GetPage(0), watermarkParams);

                watermarkParams.TargetRange.PageSpec = PageSpec.OddPagesOnly;

                WatermarkTextParams watermarkTextParams = new WatermarkTextParams();
                Color color = new Color(109.0f / 255.0f, 15.0f / 255.0f, 161.0f / 255.0f);
                watermarkTextParams.Color = color;

                watermarkTextParams.Text = "Multiline\nWatermark";

                Datalogics.PDFL.Font f = new Datalogics.PDFL.Font("Courier", FontCreateFlags.Embedded | FontCreateFlags.Subset);
                watermarkTextParams.Font      = f;
                watermarkTextParams.TextAlign = HorizontalAlignment.Center;

                doc.Watermark(watermarkTextParams, watermarkParams);

                doc.EmbedFonts();
                doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput);
            }
        }
示例#2
0
        private void SetFreeTextAnnotationProperties(Datalogics.PDFL.Content content)
        {
            bool         useDefaultFont = false;
            Path         path           = null;
            TextRun      currentTextRun = null;
            GraphicState gState         = null;

            Datalogics.PDFL.Font dlFont = null;
            for (int i = 0; i < content.NumElements; i++)
            {
                Element element = content.GetElement(i);
                if (element is Text)
                {
                    Text currentText = element as Text;
                    for (int j = 0; j < currentText.NumberOfRuns; j++)
                    {
                        currentTextRun = currentText.GetRun(j);
                        dlFont         = currentTextRun.Font;
                        if (dlFont != null)
                        {
                            break;
                        }
                    }
                }
                else if (element is Path)
                {
                    path   = element as Path;
                    gState = path.GraphicState;
                }
            }
            if (dlFont == null)
            {
                dlFont         = new Datalogics.PDFL.Font("Arial");
                useDefaultFont = true;
            }

            if (gState == null)
            {
                gState = new GraphicState();
            }

            if (currentTextRun == null)
            {
                currentTextRun = new TextRun("", dlFont, gState, new TextState(), new Datalogics.PDFL.Matrix());
            }

            if (path == null)
            {
                path = new Path(gState);
            }

            string fontName = "";

            if (families.IndexOf(dlFont.Name) == -1)
            {
                foreach (string name in families)
                {
                    string trimmedName = name.Replace(" ", "");
                    if (name.Contains(dlFont.Name) || trimmedName.Contains(dlFont.Name) || dlFont.Name.Contains(name) || dlFont.Name.Contains(trimmedName))
                    {
                        fontName = name;
                        break;
                    }
                }
            }
        }