Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

itext - How can I combine multiple PDF files excluding page breaks using iTextSharp?

I wonder if anyone has done this with iTextSharp, but I would like to combine multiple PDF files into one but leave the page breaks out. For example, I would like to create 4 PDF files containing 3 lines of text each, so I want the resulting file to have all 12 lines in 1 page. Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For those of you who want the above code in C#, here you go.

using System;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

namespace Test.WebService.Support {

  public class PDFMerge {

    private Rectangle PageSize;
    private float TopMargin;
    private float BottomMargin;
    private float Gap;
    private Document Document = null;
    private PdfWriter Writer = null;
    private float YPosition = 0;

    public PDFMerge(Rectangle size, float top, float bottom, float gap) {
      this.PageSize = size;
      this.TopMargin = top;
      this.BottomMargin = bottom;
      this.Gap = gap;
    } // PDFMerge

    public void Merge(MemoryStream outputStream, List<PdfReader> inputs) {
      try {
        this.OpenDocument(outputStream);

        foreach (PdfReader reader in inputs) {
          this.Merge(reader);
        }
      } finally {
        this.CloseDocument();
      }
    } // Merge

    private void Merge(PdfReader reader) {
      PdfReaderContentParser parser = new PdfReaderContentParser(reader);

      for (int p = 1; p <= reader.NumberOfPages; p++) {
        this.Merge(reader, parser, p);
      }
    } // Merge

    private void Merge(PdfReader reader, PdfReaderContentParser parser, int pageIndex) {
      TextMarginFinder Finder = parser.ProcessContent(pageIndex, new TextMarginFinder());
      Rectangle PageSizeToImport = reader.GetPageSize(pageIndex);
      float HeightToImport = Finder.GetHeight();
      float MaxHeight = PageSize.Height - TopMargin - BottomMargin;

      if (HeightToImport > MaxHeight) {
        throw new ArgumentException(string.Format("Page {0} content too large; height: {1}, limit: {2}.", pageIndex, HeightToImport, MaxHeight));
      }

      if (HeightToImport > YPosition - PageSize.GetBottom(BottomMargin)) {
        this.NewPage();
      } else if (!Writer.PageEmpty) {
        HeightToImport += Gap;
      }

      YPosition -= HeightToImport;

      PdfImportedPage ImportedPage = Writer.GetImportedPage(reader, pageIndex);
      Writer.DirectContent.AddTemplate(ImportedPage, 0, YPosition - (Finder.GetLly() - PageSizeToImport.Bottom));
    } // Merge

    private void OpenDocument(MemoryStream outputStream) {
      Document Document = new Document(PageSize, 36, 36, this.TopMargin, BottomMargin);
      PdfWriter Writer = PdfWriter.GetInstance(Document, outputStream);
      Document.Open();
      this.Document = Document;
      this.Writer = Writer;
      this.NewPage();
    } // OpenDocument

    private void CloseDocument() {
      try {
        Document.Close();
      } finally {
        this.Document = null;
        this.Writer = null;
        this.YPosition = 0;
      }
    } // CloseDocument

    private void NewPage() {
      Document.NewPage();
      YPosition = PageSize.GetTop(TopMargin);
    } // NewPage

  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...