PageSize.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Common;
using GrapeCity.Documents.Drawing;
namespace DsPdfWeb.Demos.Basics
{
// 演示如何在 GcDocs.Pdf 中更改页面大小和方向。
public class PageSize
{
public int CreatePDF(Stream stream)
{
const float in2mm = 72f / 25.4f;
var colorOrig = Color.Red;
var colorNew = Color.Blue;
var doc = new GcPdfDocument();
// 默认页面尺寸为 Letter (8 1/2" x 11"),纵向:
var page = doc.NewPage();
var sOrigPageInfo = $"Original page size: {page.Size} pts ({page.Size.Width / 72f}\" * {page.Size.Height / 72f}\"),\r\n" +
$"paper kind: {page.PaperKind}, landscape: {page.Landscape}.";
// 保存原始页面边界:
var rOrig = page.Bounds;
// 更改页面参数:
page.Landscape = true;
page.PaperKind = PaperKind.A4;
var sNewPageInfo = $"New page size: {page.Size} pts ({page.Size.Width / in2mm}mm * {page.Size.Height / in2mm}mm),\r\n" +
$"paper kind: {page.PaperKind}, landscape: {page.Landscape}.";
// 新页面边界:
var rNew = page.Bounds;
// 绘制原始页面边界和新页面边界:
page.Graphics.DrawRectangle(rOrig, colorOrig, 6);
page.Graphics.DrawRectangle(rNew, colorNew, 6);
// 绘制原始和新的页面信息:
var tf = new TextFormat()
{
Font = StandardFonts.Times,
FontSize = 14,
ForeColor = colorOrig
};
page.Graphics.DrawString(sOrigPageInfo, tf, new PointF(72, 72));
tf.ForeColor = colorNew;
page.Graphics.DrawString(sNewPageInfo, tf, new PointF(72, 72 * 2));
// 完毕:
doc.Save(stream);
return doc.Pages.Count;
}
}
}