- How to add page numbers to your PDF
- Add text field to PDF
- Append multiple PDF documents
- Bulleted list from XML and XSL
- Merge PDF
- Calculate the height of a paragraph in PDF
- Multipage TIFF to PDF
- Convert TXT to PDF
- Convert XHTML to PDF
- Create PDF in C# - Tall Components - Check out our PDF code samples
- Text formatting
- Generate PDF form from XML
- Generate PDF with local images from XML with Xamarin.iOS
- XhtmlParagraph and TrueType fonts
- Add footer with left and right aligned text on same line
- Read and write meta data from PDF
- Stitch PDF documents
- Use multiple licenses
- What is the resulting fontsize in PDF rich text used in SimpleXhtmlShape
Merge PDF
It is possible to merge PDFs in .NET using TallPDF. In this code sample we will create a new PDF by stitching two existing PDF documents.
C# merge PDF code sample
using System;
using System.IO;
using TallComponents.PDF.Layout;
using TallComponents.PDF.Layout.Paragraphs;
using TallComponents.PDF.Layout.Shapes;
namespace PageShapeSample
{
class Program
{
static void Main(string[] args)
{
using ( FileStream fileA = new FileStream(
"SuccesStory_Gems.pdf", FileMode.Open, FileAccess.Read))
using ( FileStream fileB = new FileStream(
"SuccesStory_Netco.pdf", FileMode.Open, FileAccess.Read))
{
Document document = new Document();
addPages(document, fileA);
addPages(document, fileB);
using (FileStream file = new FileStream(
"out.pdf", FileMode.Create, FileAccess.Write))
{
document.Write(file);
}
}
}
static void addPages(Document document, FileStream file)
{
PageShape pageShape = new PageShape(file, 0, "");
int n = pageShape.PageCount;
for (int i = 0; i < n; i++)
{
pageShape.PageIndex = i;
Section section = new Section();
section.StartOnNewPage = true;
section.PageSize = new PageSize(
pageShape.Width, pageShape.Height);
section.Margin.Left = 0;
section.Margin.Right = 0;
section.Margin.Top = 0;
section.Margin.Bottom = 0;
document.Sections.Add(section);
Drawing drawing = new Drawing(
pageShape.Width, pageShape.Height);
drawing.Shapes.Add(pageShape.Clone());
section.Paragraphs.Add(drawing);
}
}
}
}
Sub Main()
' Find all pdf documents in input folder.
Dim directoryInfo As New DirectoryInfo("..\..\../inputdocuments")
Dim allPDFs As FileInfo() = directoryInfo.GetFiles("*.pdf")
' Create a document for the merged result.
Dim mergedDocument As New Document()
' Keep a list of input streams to close when done.
Dim streams As New List(Of FileStream)()
For Each fileInfo As FileInfo In allPDFs
' Open input stream and add to list of streams.
Dim stream As New FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read)
streams.Add(stream)
' Open input document.
Dim document As New Document(stream)
' Append all pages to target document.
' The target document holds references to data in the input stream.
' For efficiency it does not copy this data so the input streams
' should not be closed before the merged document is saved.
mergedDocument.Pages.AddRange(document.Pages.CloneToArray())
Next
' Save merged document.
Using output As New FileStream("..\..\output.pdf", FileMode.Create, FileAccess.Write)
mergedDocument.Write(output)
End Using
' Close all input streams.
For Each stream As FileStream In streams
stream.Close()
Next
End Sub