コード例 #1
0
ファイル: Sample.cs プロジェクト: SautinSoft/Document-Net
        /// <summary>
        /// Creates a new document with shape containing a text and picture.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/pictures-and-shapes.php
        /// </remarks>
        static void PictureAndShape()
        {
            string filePath  = @"Shape.docx";
            string imagePath = @"..\..\image.jpg";

            DocumentCore dc = new DocumentCore();

            // 1. Shape with text.
            Shape shapeWithText = new Shape(dc, Layout.Floating(new HorizontalPosition(1, LengthUnit.Inch, HorizontalPositionAnchor.Page),
                                                                new VerticalPosition(2, LengthUnit.Inch, VerticalPositionAnchor.Page),
                                                                new Size(LengthUnitConverter.Convert(6, LengthUnit.Inch, LengthUnit.Point), LengthUnitConverter.Convert(1.5d, LengthUnit.Centimeter, LengthUnit.Point))));

            (shapeWithText.Layout as FloatingLayout).WrappingStyle = WrappingStyle.InFrontOfText;
            shapeWithText.Text.Blocks.Add(new Paragraph(dc, new Run(dc, "This is the text in shape.", new CharacterFormat()
            {
                Size = 30
            })));
            shapeWithText.Outline.Fill.SetEmpty();
            shapeWithText.Fill.SetSolid(Color.Orange);
            dc.Content.End.Insert(shapeWithText.Content);

            // 2. Picture with FloatingLayout:
            // Floating layout means that the Picture (or Shape) is positioned by coordinates.
            Picture pic = new Picture(dc, imagePath);

            pic.Layout = FloatingLayout.Floating(
                new HorizontalPosition(50, LengthUnit.Millimeter, HorizontalPositionAnchor.Page),
                new VerticalPosition(20, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin),
                new Size(LengthUnitConverter.Convert(10, LengthUnit.Centimeter, LengthUnit.Point),
                         LengthUnitConverter.Convert(10, LengthUnit.Centimeter, LengthUnit.Point))
                );

            // Set the wrapping style.
            (pic.Layout as FloatingLayout).WrappingStyle = WrappingStyle.BehindText;

            // Add our picture into the section.
            dc.Content.End.Insert(pic.Content);

            dc.Save(filePath);

            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath)
            {
                UseShellExecute = true
            });
        }
コード例 #2
0
        /// <summary>
        /// How to add pictures into a document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/add-pictures.php
        /// </remarks>
        public static void AddPictures()
        {
            string documentPath = @"Pictures.docx";
            string pictPath     = @"..\..\image1.jpg";

            // Let's create a simple document.
            DocumentCore dc = new DocumentCore();

            // Add a new section.
            Section s = new Section(dc);

            dc.Sections.Add(s);

            // 1. Picture with InlineLayout:

            // Create a new paragraph with picture.
            Paragraph par = new Paragraph(dc);

            s.Blocks.Add(par);
            par.ParagraphFormat.Alignment = HorizontalAlignment.Left;

            // Add some text content.
            par.Content.End.Insert("Shrek and Donkey ", new CharacterFormat()
            {
                FontName = "Calibri", Size = 16.0, FontColor = Color.Black
            });

            // Our picture has InlineLayout - it doesn't have positioning by coordinates
            // and located as flowing content together with text (Run and other Inline elements).
            Picture pict1 = new Picture(dc, InlineLayout.Inline(new Size(100, 100)), pictPath);

            // Add picture to the paragraph.
            par.Inlines.Add(pict1);

            // Add some text content.
            par.Content.End.Insert(" arrive at Farquaad's palace in Duloc, where they end up in a tournament.", new CharacterFormat()
            {
                FontName = "Calibri", Size = 16.0, FontColor = Color.Black
            });

            // 2. Picture with FloatingLayout:
            // Floating layout means that the Picture (or Shape) is positioned by coordinates.
            Picture pict2 = new Picture(dc, pictPath);

            pict2.Layout = FloatingLayout.Floating(
                new HorizontalPosition(50, LengthUnit.Millimeter, HorizontalPositionAnchor.Page),
                new VerticalPosition(70, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin),
                new Size(LengthUnitConverter.Convert(10, LengthUnit.Centimeter, LengthUnit.Point),
                         LengthUnitConverter.Convert(10, LengthUnit.Centimeter, LengthUnit.Point))
                );

            // Set the wrapping style.
            (pict2.Layout as FloatingLayout).WrappingStyle = WrappingStyle.Square;

            // Add our picture into the section.
            s.Content.End.Insert(pict2.Content);

            // Save our document into DOCX format.
            dc.Save(documentPath, new DocxSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath)
            {
                UseShellExecute = true
            });
        }
コード例 #3
0
        /// <summary>
        /// Insert images on each page of the PDF file.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/from-customers-insert-images-on-each-document-page-csharp-vb-net.php
        /// </remarks>
        static void InsertImagesOnEachPage()
        {
            string inpfFile = @"..\..\example.pdf";
            string pctFile  = @"..\..\signature.png";
            string outFile  = @"Result.pdf";

            // This example is acceptable for PDF documents.
            // Because when we're loading PDF documents using DocumentCore the each PDF-page
            // we'll be stored in own Section object.
            // In other words, the each Section represents the separate PDF-page.
            DocumentCore dc = DocumentCore.Load(inpfFile);

            // Load the Picture from a file.
            Picture pict = new Picture(dc, pctFile);

            // In this example we'll place the image in three (3)
            // different places to the each document page.

            // Let's create three layouts
            List <Layout> layouts = new List <Layout>()
            {
                // Layout 1.
                // Horizontal: 10mm from page left.
                // Vertical: 260mm from top margin.
                // Size: 2cm * 1 cm.
                FloatingLayout.Floating(
                    new HorizontalPosition(10, LengthUnit.Millimeter, HorizontalPositionAnchor.Page),
                    new VerticalPosition(260, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin),
                    new Size(LengthUnitConverter.Convert(2, LengthUnit.Centimeter, LengthUnit.Point),
                             LengthUnitConverter.Convert(1, LengthUnit.Centimeter, LengthUnit.Point))
                    ),
                // Layout 2.
                // Horizontal: 180mm from page left.
                // Vertical: 10mm from top margin.
                // Size: 3cm * 2cm.
                FloatingLayout.Floating(
                    new HorizontalPosition(180, LengthUnit.Millimeter, HorizontalPositionAnchor.Page),
                    new VerticalPosition(10, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin),
                    new Size(LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.Point),
                             LengthUnitConverter.Convert(2, LengthUnit.Centimeter, LengthUnit.Point))
                    ),

                // Layout 3.
                // Horizontal: 150mm from page left.
                // Vertical: 150mm from page top.
                // Size: 3cm * 3cm.
                FloatingLayout.Floating(
                    new HorizontalPosition(150, LengthUnit.Millimeter, HorizontalPositionAnchor.Page),
                    new VerticalPosition(150, LengthUnit.Millimeter, VerticalPositionAnchor.Page),
                    new Size(LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.Point),
                             LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.Point))
                    )
            };

            // Iterate by Sections (PDF pages in our case).
            foreach (Section s in dc.Sections)
            {
                // Insert our pictures in different places.
                foreach (FloatingLayout fl in layouts)
                {
                    pict.Layout = new FloatingLayout(fl.HorizontalPosition, fl.VerticalPosition, fl.Size);
                    // Place the picture behind the text.
                    (pict.Layout as FloatingLayout).WrappingStyle = WrappingStyle.BehindText;

                    // Here we insert the Picture content at the 1st Block element (Paragraph or Table).
                    s.Blocks[0].Content.Start.Insert(pict.Content);
                }
            }

            dc.Save(outFile, new PdfSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
            {
                UseShellExecute = true
            });
        }
コード例 #4
0
ファイル: Sample.cs プロジェクト: Underboth/Junior-test-task
        /// <summary>
        /// Insert a picture to custom pages into existing DOCX document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-picture-jpg-image-to-custom-docx-page-net-csharp-vb.php
        /// </remarks>
        static void InsertPictureToCustomPages()
        {
            // In this example we'll insert the picture to 1st and 3rd pages
            // of DOCX document into specific positions.

            string inpFile  = @"..\..\example.docx";
            string outFile  = @"Result.docx";
            string pictFile = @"..\..\picture.jpg";

            DocumentCore      dc = DocumentCore.Load(inpFile);
            DocumentPaginator dp = dc.GetPaginator();


            // Step 1: Put the picture to 1st page.

            // Create the Picture object from Jpeg file.
            Picture pict = new Picture(dc, pictFile);

            // Specify the picture size and position.
            pict.Layout = FloatingLayout.Floating(
                new HorizontalPosition(70, LengthUnit.Millimeter, HorizontalPositionAnchor.Margin),
                new VerticalPosition(23, LengthUnit.Millimeter, VerticalPositionAnchor.Margin),
                new Size(LengthUnitConverter.Convert(1, LengthUnit.Inch, LengthUnit.Point),
                         pict.Layout.Size.Height * LengthUnitConverter.Convert(1, LengthUnit.Inch, LengthUnit.Point) / pict.Layout.Size.Width));

            // Put the picture behind the text
            (pict.Layout as FloatingLayout).WrappingStyle = WrappingStyle.BehindText;

            // Find the 1st Element in the 1st page.
            Element e1 = dp.Pages[0].ElementsOnPage.FirstOrDefault(e => e is Run);

            // Insert the picture at this Element.
            e1.Content.End.Insert(pict.Content);


            // Step 2: Put the picture to 3rd page.
            if (dp.Pages.Count >= 3)
            {
                // Find the 1st Element on the 3rd page.
                Element e2 = dp.Pages[2].ElementsOnPage.FirstOrDefault(e => e is Run);

                // Create another picture
                Picture pict2 = new Picture(dc, pictFile);
                pict2.Layout = FloatingLayout.Floating(
                    new HorizontalPosition(10, LengthUnit.Millimeter, HorizontalPositionAnchor.Margin),
                    new VerticalPosition(20, LengthUnit.Millimeter, VerticalPositionAnchor.Margin),
                    new Size(LengthUnitConverter.Convert(1, LengthUnit.Inch, LengthUnit.Point),
                             pict2.Layout.Size.Height * LengthUnitConverter.Convert(1, LengthUnit.Inch, LengthUnit.Point) / pict2.Layout.Size.Width)
                    );
                (pict2.Layout as FloatingLayout).WrappingStyle = WrappingStyle.BehindText;

                // Insert the picture at this Element.
                e2.Content.End.Insert(pict2.Content);
            }
            // Save the document as new DOCX and open it.
            dc.Save(outFile);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
            {
                UseShellExecute = true
            });
        }