如何使用iTextSharp将四个“四分之一页”组合成一个完整大小的页面?

时间:2016-03-12 15:22:01

标签: c# itextsharp

我想在一个PDF页面上创建四个重复的“模板”。每个季度页面中的“静态”信息将是相同的,但每个页面中的变量内容会有所不同。您可以将其视为登录表单或其他类似的四个“副本”,但以编程方式添加条目,例如:

Real name: B. Clay Shannon
User name: Radicalia
Password: thatDarnedKatzenjammer
Email address: clay@pigeon.com

Real name: Albert L.B. Kollenborn
User name: Grandpa
Password: caseyJonesBigBrother1907
Email address: princealbertinacan@balpeenhammer.org

(另外两个在右边)

我有代码来创建文档并添加如下所示的段落:

using (var ms = new MemoryStream())
{
    using (var doc = new Document(PageSize.A4, 25, 25, 25, 25))
    {
        using (var writer = PdfWriter.GetInstance(doc, ms))
        {
            doc.Open();

            Paragraph realName = new Paragraph("Real name:", 
times16BoldCyan);
            doc.Add(realName);
            . . .

这种东西适用于“普通”页面,但在上述场景中(四页四分之一页面填入一页),我应该怎么做呢?它应该是这样的:

    using (var msNorthWestQuadrant = new MemoryStream())
    {
        using (var docNorthWestQuadrant = new Document(PageSize.A4, 6.25, 
6.25, 6.25, 6.25))
        {
            using (var writer = PdfWriter.GetInstance(docNorthWestQuadrant, 
            msNorthWestQuadrant))
            {
                docNorthWestQuadrant.Open();

                Paragraph realName = new Paragraph("Real name:", 
times16BoldCyan);
                docNorthWestQuadrant.Add(realName);
                . . .

        using (var msNorthEastQuadrant = new MemoryStream())
        {
            using (var docNorthEastQuadrant = new Document(PageSize.A4, 
12.5, 12.5, 6.25, 6.25))
            {
                using (var writer = 
PdfWriter.GetInstance(docNorthEastQuadrant, msNorthEastQuadrant))
                {
                    docNorthEastQuadrant.Open();

                    Paragraph realName = new Paragraph("Real name:", 
times16BoldCyan);
                    docNorthEastQuadrant.Add(realName);
                    . . .

(等等 - 还有SouthEastQuadrant和SouthEastQuadrant吗?)

......或者这是错误的做法?如果是,我对PageSize位置/大小所需的精确值是什么(上面的“1.25”只是一个疯狂的猜测)。

更新

或者是否需要一个“主”doc对象,然后是四个“sub”doc对象,它们被添加到主服务器中(伪代码):

using (var doc = new Document(PageSize.A4, 25, 25, 25, 25))

using (var docNorthwest = new Document(PageSize.A4, 6.25, 6.25, 6.25, 6.25))
. . .
docNorthwest.Add(par);
doc.Add(docNorthwest);
. . .
using (var docSouthwest = new Document(PageSize.A4, 6.25, 6.25, 6.25, 6.25))
docSouthwest.Add(par2);
doc.Add(docSouthwest);
. . .
using (var docSoutheast = new Document(PageSize.A4, 6.25, 6.25, 6.25, 6.25))
docSoutheast.Add(par3);
doc.Add(docSoutheast);
. . .
using (var docNortheast = new Document(PageSize.A4, 6.25, 6.25, 6.25, 6.25))
docNortheast.Add(par2);
doc.Add(docNortheast);
. . .

1 个答案:

答案 0 :(得分:2)

从上面的描述中,一种实现目标的简单方法:

  1. 创建单个内存中Document
  2. 将重复内容放在Document
  3. 的不同页面上
  4. 使用Document
  5. 将原始Document的各个网页转储到新的PdfPTable

    首先,上面步骤1-2的一些测试数据:

    byte[] GetMasterDocument(int count)
    {
        using (var stream = new MemoryStream())
        {
            using (var document = new Document())
            {
                PdfWriter.GetInstance(document, stream);
                document.Open();
                for (int i = 1; i <= count; ++i)
                {
                    document.Add(new Paragraph(string.Format(
    @"Real name: real-name-{0:D4}
    User name: user-name-{0:D4}
    Password: password-{0:D4}
    Email address: email-{0:D4}@invalid.com",
                     i)));
                    if (i < count) document.NewPage();
                }
            }
            return stream.ToArray();
        }
    }
    

    将重复内容转储到输出文件中:

    PdfReader reader = new PdfReader(GetMasterDocument(38));
    Rectangle pageSize = reader.GetPageSize(1);
    using (FileStream stream = new FileStream(
        outputFile,
        FileMode.Create,
        FileAccess.Write))
    {
        using (Document document = new Document(pageSize, 0, 0, 0, 0))
        {
            PdfWriter writer = PdfWriter.GetInstance(document, stream);
            document.Open();
            PdfPTable table = new PdfPTable(2);
            table.TotalWidth = pageSize.Width;
            table.LockedWidth = true;
            table.DefaultCell.Border = Rectangle.NO_BORDER;
            table.DefaultCell.FixedHeight = pageSize.Height / 2;
    
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfImportedPage page = writer.GetImportedPage(reader, i);
                table.AddCell(Image.GetInstance(page));
            }
            document.Add(table);
        }
    }
    

    如果重复内容与上述一样小,并且空白空间过多(缩放页面以适合PdfPCell),请在GetMasterDocument()中设置较小的页面大小。 PageSize.A6与上面的示例数据非常吻合:

    enter image description here