Way to Manage PDF Page Setup for Printing with C#

Before printing one document, we need to do some setting on page according to document contents and page size. When printing, we hope that there will be a good layout and all the contents in document can be displayed on papers. However, it is possible that some contents will disappear or we cannot get contents clearly if we don’t pay attention to page setup. In this post, I will talk something about PDF page setup with C#.

Page setup includes page size and direction setting. In my example, I will set page size as A3 and A4. For direction, besides portrait and landscape, the page will be rotated, containing 90 and 180 degree. The following steps and code show you details.

In order to realize this function more quickly and easily, I use a C# .NET PDF component, Spire.PDF for .NET. So,  I have added its dll file as reference in my project. 

Steps:

  1. Create a PDF document and then set margin, including bottom, top, left and right.
  2. Before setting pages, we need to add some contents. The contents I draw are from wiki. Therefore, after adding title, add reference below title and use brackets to mark it. Next, load icon and body from my computer. Set them format to make the appearance be more wonderful.
  3. Create sections in pages. Set margin, page size and background color for this section. Then, draw page.
  4. Set page to rotate 180 degree, page size as A4. Add background color and draw page.
  5. Set direction of page as landscape, page size as A4. Add background color and draw page.
  6. Set page to rotate 90 degree, page size as A4. Add background color and draw page.
  7. Set page to rotate 180 degree, page size as A3, page direction as landscape. Add background color and draw page.
  8. Save and launch file.

Main Coding:

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace PageSetup
{
    class Setup
    {
        static void Main(string[] args)
        {
            //Create PDF.
            PdfDocument doc = new PdfDocument();

            //Margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;

            //Add Section
            PdfSection section = doc.Sections.Add();
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Margins = margin;
            PdfPageBase page = section.Pages.Add();
            page.BackgroundColor = Color.LightCyan;
            DrawPage(page);

            //Rotate 180
            section = doc.Sections.Add();
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Margins = margin;
            section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;
            page = section.Pages.Add();
            page.BackgroundColor = Color.LightCyan;
            DrawPage(page);

            //Landscape
            section = doc.Sections.Add();
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Margins = margin;
            section.PageSettings.Orientation = PdfPageOrientation.Landscape;
            page = section.Pages.Add();
            page.BackgroundColor = Color.LightSeaGreen;
            DrawPage(page);

            //Rotate 90
            section = doc.Sections.Add();
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Margins = margin;
            section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
            page = section.Pages.Add();
            page.BackgroundColor = Color.LightCyan;
            DrawPage(page);

            //A3 Size, Rotate 180
            page = doc.Pages.Add(PdfPageSize.A3, margin, PdfPageRotateAngle.RotateAngle180, PdfPageOrientation.Landscape);
            page.BackgroundColor = Color.LightSeaGreen;
            DrawPage(page);

            //Save and Launch PDF
            doc.SaveToFile(“PageSetup.pdf”);
            doc.Close();
            System.Diagnostics.Process.Start(“PageSetup.pdf”);
        }

        private static void DrawPage(PdfPageBase page)
        {
            float pageWidth = page.Canvas.ClientSize.Width;
            float y = 0;

            //Title
            y = y + 20;
            PdfBrush brush2 = new PdfSolidBrush(Color.DarkBlue);
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font(“Calibri”, 14f, FontStyle.Regular));
            PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center);
            format2.CharacterSpacing = 1f;
            String text = “Someone Like You”;
            page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2);
            SizeF size = font2.MeasureString(text, format2);
            y = y + size.Height + 6;

            //Icon
            PdfImage image = PdfImage.FromFile(@”D:\work\My Documents\Adele.jpg”);
            page.Canvas.DrawImage(image, new PointF(pageWidth – image.PhysicalDimension.Width, y));
            float imageLeftSpace = pageWidth – image.PhysicalDimension.Width – 2;
            float imageBottom = image.PhysicalDimension.Height + y;

            //Reference
            PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font(“Calibri”, 11f));
            PdfStringFormat format3 = new PdfStringFormat();
            format3.ParagraphIndent = font3.Size * 2;
            format3.MeasureTrailingSpaces = true;
            format3.LineSpacing = font3.Size * 1.5f;
            String text1 = “(All text and picture from “;
            String text2 = “Wikipedia”;
            String text3 = “, the free encyclopedia)”;
            page.Canvas.DrawString(text1, font3, brush2, 0, y, format3);
            size = font3.MeasureString(text1, format3);
            float x1 = size.Width;
            format3.ParagraphIndent = 0;
            PdfTrueTypeFont font4 = new PdfTrueTypeFont(new Font(“Calibri”, 11f, FontStyle.Underline));
            PdfBrush brush3 = PdfBrushes.DarkCyan;
            page.Canvas.DrawString(text2, font4, brush3, x1, y, format3);
            size = font4.MeasureString(text2, format3);
            x1 = x1 + size.Width;
            page.Canvas.DrawString(text3, font3, brush2, x1, y, format3);
            y = y + size.Height;

            //Content
            PdfStringFormat format4 = new PdfStringFormat(PdfTextAlignment.Justify);
            text = System.IO.File.ReadAllText(@”D:\work\My Documents\Someone Like You.txt”);
            PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font(“Calibri”, 11f));
            format4.LineSpacing = font5.Size * 1.5f;
            PdfStringLayouter textLayouter = new PdfStringLayouter();
            float imageLeftBlockHeight = imageBottom – y;
            PdfStringLayoutResult result
                = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight));
            if (result.ActualSize.Height < imageBottom – y)
            {
                imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight;
                result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight));
            }

            foreach (LineInfo line in result.Lines)
            {
                page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4);
                y = y + result.LineHeight;
            }

            PdfTextWidget textWidget = new PdfTextWidget(result.Remainder, font5, brush2);
            PdfTextLayout textLayout = new PdfTextLayout();
            textLayout.Break = PdfLayoutBreakType.FitPage;
            textLayout.Layout = PdfLayoutType.Paginate;
            RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);
            textWidget.StringFormat = format4;
            textWidget.Draw(page, bounds, textLayout);
        }
    }
}

Result Shown by Following:

 Download Spire.PDF for .NET Here

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s