Example #1
0
        public void PageMargins()
        {
            //ExStart
            //ExFor:ConvertUtil
            //ExFor:ConvertUtil.InchToPoint
            //ExFor:PaperSize
            //ExFor:PageSetup.PaperSize
            //ExFor:PageSetup.Orientation
            //ExFor:PageSetup.TopMargin
            //ExFor:PageSetup.BottomMargin
            //ExFor:PageSetup.LeftMargin
            //ExFor:PageSetup.RightMargin
            //ExFor:PageSetup.HeaderDistance
            //ExFor:PageSetup.FooterDistance
            //ExSummary:Specifies paper size, orientation, margins and other settings for a section.
            DocumentBuilder builder = new DocumentBuilder();

            Aspose.Words.PageSetup ps = builder.PageSetup;
            ps.PaperSize      = PaperSize.Legal;
            ps.Orientation    = Orientation.Landscape;
            ps.TopMargin      = Aspose.Words.ConvertUtil.InchToPoint(1.0);
            ps.BottomMargin   = Aspose.Words.ConvertUtil.InchToPoint(1.0);
            ps.LeftMargin     = Aspose.Words.ConvertUtil.InchToPoint(1.5);
            ps.RightMargin    = Aspose.Words.ConvertUtil.InchToPoint(1.5);
            ps.HeaderDistance = Aspose.Words.ConvertUtil.InchToPoint(0.2);
            ps.FooterDistance = Aspose.Words.ConvertUtil.InchToPoint(0.2);

            builder.Writeln("Hello world.");

            builder.Document.Save(MyDir + "PageSetup.PageMargins Out.doc");
            //ExEnd
        }
Example #2
0
        public void DifferentHeaders()
        {
            //ExStart
            //ExFor:PageSetup.DifferentFirstPageHeaderFooter
            //ExFor:PageSetup.OddAndEvenPagesHeaderFooter
            //ExSummary:Creates headers and footers different for first, even and odd pages using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder();

            Aspose.Words.PageSetup ps = builder.PageSetup;
            ps.DifferentFirstPageHeaderFooter = true;
            ps.OddAndEvenPagesHeaderFooter    = true;

            builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
            builder.Writeln("First page header.");

            builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
            builder.Writeln("Even pages header.");

            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
            builder.Writeln("Odd pages header.");

            // Move back to the main story of the first section.
            builder.MoveToSection(0);
            builder.Writeln("Text page 1.");
            builder.InsertBreak(BreakType.PageBreak);
            builder.Writeln("Text page 2.");
            builder.InsertBreak(BreakType.PageBreak);
            builder.Writeln("Text page 3.");

            builder.Document.Save(MyDir + "PageSetup.DifferentHeaders Out.doc");
            //ExEnd
        }
Example #3
0
        public void PageBorderProperties()
        {
            //ExStart
            //ExFor:Section.PageSetup
            //ExFor:PageSetup.BorderAlwaysInFront
            //ExFor:PageSetup.BorderDistanceFrom
            //ExFor:PageSetup.BorderAppliesTo
            //ExFor:PageBorderDistanceFrom
            //ExFor:PageBorderAppliesTo
            //ExFor:Border.DistanceFromText
            //ExSummary:Creates a page border that looks like a wide blue band at the top of the first page only.
            Aspose.Words.Document doc = new Aspose.Words.Document();

            Aspose.Words.PageSetup ps = doc.Sections[0].PageSetup;
            ps.BorderAlwaysInFront = false;
            ps.BorderDistanceFrom  = PageBorderDistanceFrom.PageEdge;
            ps.BorderAppliesTo     = PageBorderAppliesTo.FirstPage;

            Aspose.Words.Border border = ps.Borders[BorderType.Top];
            border.LineStyle        = LineStyle.Single;
            border.LineWidth        = 30;
            border.Color            = System.Drawing.Color.Blue;
            border.DistanceFromText = 0;

            doc.Save(MyDir + "PageSetup.PageBorderTop Out.doc");
            //ExEnd
        }
Example #4
0
        private static void ConvertImageToPdf(string inputFileName, string outputFileName)
        {
            // Create Aspose.Words.Document and DocumentBuilder.
            // The builder makes it simple to add content to the document.
            Aspose.Words.Document doc     = new Aspose.Words.Document();
            DocumentBuilder       builder = new DocumentBuilder(doc);

            // Read the image from file, ensure it is disposed.
            using (System.Drawing.Image image = System.Drawing.Image.FromFile(inputFileName))
            {
                // Find which dimension the frames in this image represent. For example
                // the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".
                FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);

                int framesCount = image.GetFrameCount(dimension);
                // Get the number of frames in the image.
                framesCount = image.GetFrameCount(FrameDimension.Page);


                // Loop through all frames.
                for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
                {
                    // Insert a section break before each new page, in case of a multi-frame TIFF.
                    if (frameIdx != 0)
                    {
                        builder.InsertBreak(BreakType.SectionBreakNewPage);
                    }

                    // Select active frame.
                    image.SelectActiveFrame(dimension, frameIdx);

                    // We want the size of the page to be the same as the size of the image.
                    // Convert pixels to points to size the page to the actual image size.
                    PageSetup ps = builder.PageSetup;
                    ps.PageWidth  = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
                    ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);

                    // Insert the image into the document and position it at the top left corner of the page.
                    builder.InsertImage(
                        image,
                        RelativeHorizontalPosition.Page,
                        0,
                        RelativeVerticalPosition.Page,
                        0,
                        ps.PageWidth > 950 ? 950 : ps.PageWidth,
                        ps.PageHeight > 950 ? 950 : ps.PageHeight,
                        WrapType.None);
                }
            }

            // Save the document to PDF.
            doc.Save(outputFileName);
        }
Example #5
0
        public void UtilityClassesConvertBetweenMeasurementUnits()
        {
            //ExStart
            //ExFor:ConvertUtil
            //ExId:UtilityClassesConvertBetweenMeasurementUnits
            //ExSummary:Shows how to specify page properties in inches.
            Aspose.Words.Document doc     = new Aspose.Words.Document();
            DocumentBuilder       builder = new DocumentBuilder(doc);

            Aspose.Words.PageSetup pageSetup = builder.PageSetup;
            pageSetup.TopMargin      = Aspose.Words.ConvertUtil.InchToPoint(1.0);
            pageSetup.BottomMargin   = Aspose.Words.ConvertUtil.InchToPoint(1.0);
            pageSetup.LeftMargin     = Aspose.Words.ConvertUtil.InchToPoint(1.5);
            pageSetup.RightMargin    = Aspose.Words.ConvertUtil.InchToPoint(1.5);
            pageSetup.HeaderDistance = Aspose.Words.ConvertUtil.InchToPoint(0.2);
            pageSetup.FooterDistance = Aspose.Words.ConvertUtil.InchToPoint(0.2);
            //ExEnd
        }
Example #6
0
        public void ColumnsCustomWidth()
        {
            //ExStart
            //ExFor:TextColumnCollection.LineBetween
            //ExFor:TextColumnCollection.EvenlySpaced
            //ExFor:TextColumnCollection.Item
            //ExFor:TextColumn
            //ExFor:TextColumn.Width
            //ExFor:TextColumn.SpaceAfter
            //ExSummary:Creates multiple columns of different widths in a section using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder();

            TextColumnCollection columns = builder.PageSetup.TextColumns;

            // Show vertical line between columns.
            columns.LineBetween = true;
            // Indicate we want to create column with different widths.
            columns.EvenlySpaced = false;
            // Create two columns, note they will be created with zero widths, need to set them.
            columns.SetCount(2);

            // Set the first column to be narrow.
            TextColumn c1 = columns[0];

            c1.Width      = 100;
            c1.SpaceAfter = 20;

            // Set the second column to take the rest of the space available on the page.
            TextColumn c2 = columns[1];

            Aspose.Words.PageSetup ps = builder.PageSetup;
            double contentWidth       = ps.PageWidth - ps.LeftMargin - ps.RightMargin;

            c2.Width = contentWidth - c1.Width - c1.SpaceAfter;

            builder.Writeln("Narrow column 1.");
            builder.InsertBreak(BreakType.ColumnBreak);
            builder.Writeln("Wide column 2.");

            builder.Document.Save(MyDir + "PageSetup.ColumnsCustomWidth Out.doc");
            //ExEnd
        }
Example #7
0
        public void PageBorders()
        {
            //ExStart
            //ExFor:PageSetup.Borders
            //ExFor:Border.Shadow
            //ExFor:BorderCollection.LineStyle
            //ExFor:BorderCollection.LineWidth
            //ExFor:BorderCollection.Color
            //ExFor:BorderCollection.DistanceFromText
            //ExFor:BorderCollection.Shadow
            //ExSummary:Creates a fancy looking green wavy page border with a shadow.
            Aspose.Words.Document  doc = new Aspose.Words.Document();
            Aspose.Words.PageSetup ps  = doc.Sections[0].PageSetup;

            ps.Borders.LineStyle        = LineStyle.DoubleWave;
            ps.Borders.LineWidth        = 2;
            ps.Borders.Color            = System.Drawing.Color.Green;
            ps.Borders.DistanceFromText = 24;
            ps.Borders.Shadow           = true;

            doc.Save(MyDir + "PageSetup.PageBorders Out.doc");
            //ExEnd
        }
Example #8
0
        public void LineNumbers()
        {
            //ExStart
            //ExFor:PageSetup.LineStartingNumber
            //ExFor:PageSetup.LineNumberCountBy
            //ExFor:PageSetup.LineNumberRestartMode
            //ExFor:LineNumberRestartMode
            //ExSummary:Turns on Microsoft Word line numbering for a section.
            DocumentBuilder builder = new DocumentBuilder();

            Aspose.Words.PageSetup ps = builder.PageSetup;
            ps.LineStartingNumber    = 1;
            ps.LineNumberCountBy     = 5;
            ps.LineNumberRestartMode = LineNumberRestartMode.RestartPage;

            for (int i = 1; i <= 20; i++)
            {
                builder.Writeln(string.Format("Line {0}.", i));
            }

            builder.Document.Save(MyDir + "PageSetup.LineNumbers Out.doc");
            //ExEnd
        }
Example #9
0
        //ExStart
        //ExId:HeaderFooterPrimer
        //ExSummary:Maybe a bit complicated example, but demonstrates many things that can be done with headers/footers.
        public void Primer()
        {
            Aspose.Words.Document doc     = new Aspose.Words.Document();
            DocumentBuilder       builder = new DocumentBuilder(doc);

            Aspose.Words.Section   currentSection = builder.CurrentSection;
            Aspose.Words.PageSetup pageSetup      = currentSection.PageSetup;

            // Specify if we want headers/footers of the first page to be different from other pages.
            // You can also use PageSetup.OddAndEvenPagesHeaderFooter property to specify
            // different headers/footers for odd and even pages.
            pageSetup.DifferentFirstPageHeaderFooter = true;

            // --- Create header for the first page. ---
            pageSetup.HeaderDistance = 20;
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

            // Set font properties for header text.
            builder.Font.Name = "Arial";
            builder.Font.Bold = true;
            builder.Font.Size = 14;
            // Specify header title for the first page.
            builder.Write("Aspose.Words Header/Footer Creation Primer - Title Page.");

            // --- Create header for pages other than first. ---
            pageSetup.HeaderDistance = 20;
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

            // Insert absolutely positioned image into the top/left corner of the header.
            // Distance from the top/left edges of the page is set to 10 points.
            string imageFileName = MyDir + "Aspose.Words.gif";

            builder.InsertImage(imageFileName, RelativeHorizontalPosition.Page, 10, RelativeVerticalPosition.Page, 10, 50, 50, WrapType.Through);

            builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
            // Specify another header title for other pages.
            builder.Write("Aspose.Words Header/Footer Creation Primer.");

            // --- Create footer for pages other than first. ---
            builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

            // We use table with two cells to make one part of the text on the line (with page numbering)
            // to be aligned left, and the other part of the text (with copyright) to be aligned right.
            builder.StartTable();

            // Clear table borders.
            builder.CellFormat.ClearFormatting();

            builder.InsertCell();

            // Set first cell to 1/3 of the page width.
            builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 / 3);

            // Insert page numbering text here.
            // It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of pages.
            builder.Write("Page ");
            builder.InsertField("PAGE", "");
            builder.Write(" of ");
            builder.InsertField("NUMPAGES", "");

            // Align this text to the left.
            builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;

            builder.InsertCell();
            // Set the second cell to 2/3 of the page width.
            builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 * 2 / 3);

            builder.Write("(C) 2001 Aspose Pty Ltd. All rights reserved.");

            // Align this text to the right.
            builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Right;

            builder.EndRow();
            builder.EndTable();

            builder.MoveToDocumentEnd();
            // Make page break to create a second page on which the primary headers/footers will be seen.
            builder.InsertBreak(BreakType.PageBreak);

            // Make section break to create a third page with different page orientation.
            builder.InsertBreak(BreakType.SectionBreakNewPage);

            // Get the new section and its page setup.
            currentSection = builder.CurrentSection;
            pageSetup      = currentSection.PageSetup;

            // Set page orientation of the new section to landscape.
            pageSetup.Orientation = Orientation.Landscape;

            // This section does not need different first page header/footer.
            // We need only one title page in the document and the header/footer for this page
            // has already been defined in the previous section
            pageSetup.DifferentFirstPageHeaderFooter = false;

            // This section displays headers/footers from the previous section by default.
            // Call currentSection.HeadersFooters.LinkToPrevious(false) to cancel this.
            // Page width is different for the new section and therefore we need to set
            // a different cell widths for a footer table.
            currentSection.HeadersFooters.LinkToPrevious(false);

            // If we want to use the already existing header/footer set for this section
            // but with some minor modifications then it may be expedient to copy headers/footers
            // from the previous section and apply the necessary modifications where we want them.
            CopyHeadersFootersFromPreviousSection(currentSection);

            // Find the footer that we want to change.
            Aspose.Words.HeaderFooter primaryFooter = currentSection.HeadersFooters[HeaderFooterType.FooterPrimary];

            Row row = primaryFooter.Tables[0].FirstRow;

            row.FirstCell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 / 3);
            row.LastCell.CellFormat.PreferredWidth  = PreferredWidth.FromPercent(100 * 2 / 3);

            // Save the resulting document.
            doc.Save(MyDir + "HeaderFooter.Primer Out.doc");
        }