// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Numerics;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.Annotations;
using GrapeCity.Documents.Pdf.Graphics;
namespace DsPdfWeb.Demos
{
// 此示例演示如何创建表示页面的 FormXObject
// 从加载到临时 GcPdfDocument 的 PDF 中,然后渲染该文件
// 对象到当前文档中。
public class PageFormXObject
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;
var rc = Common.Util.AddNote(
"下面的缩略图是现有文档的页面。" +
"",
page);
// 布局参数:
var margin = rc.Left;
var pad = 36;
var side = (page.Size.Width - margin * 2 - pad) / 2;
var ip = new PointF(margin, rc.Bottom + pad);
// 镜像变换:
var tr = Matrix3x2.CreateScale(-1, 1) * Matrix3x2.CreateTranslation(page.Size.Width, 0);
// 覆盖页面标题的文本格式:
var color = Color.DarkRed;
var tf = new TextFormat()
{
Font = StandardFonts.HelveticaBold,
FontSize = 16,
ForeColor = Color.FromArgb(128, color),
};
// 打开任意 PDF,将其加载到临时文档中并循环遍历其页面,
// 将每个绘制到当前文档中:
using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf"));
var doc1 = new GcPdfDocument();
doc1.Load(fs);
// 为加载的 PDF 页面创建 FormXObject 列表:
var fxos = new List<FormXObject>();
doc1.Pages.ToList().ForEach(p_ => fxos.Add(new FormXObject(doc, p_)));
// 将缩略图渲染到当前文档中:
for (int i = 0; i < fxos.Count; ++i)
{
if (ip.Y + side > page.Size.Height - margin)
{
page = doc.NewPage();
g = page.Graphics;
ip = new PointF(margin, margin);
}
var rcfx = new RectangleF(ip.X, ip.Y, side, side);
// 直接绘制:
g.DrawForm(fxos[i], rcfx, null, ImageAlign.ScaleImage);
g.DrawRectangle(rcfx, color);
g.DrawString($"Page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, false);
// 绘制反转:
g.Transform = tr;
g.DrawForm(fxos[i], rcfx, null, ImageAlign.ScaleImage);
g.DrawRectangle(rcfx, color);
g.Transform = Matrix3x2.Identity;
rcfx.Offset(side + pad, 0);
g.DrawString($"Reversed page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, false);
//
ip.Y += side + pad;
}
// 完毕:
doc.Save(stream);
return doc.Pages.Count;
}
}
}