Skip to content

Instantly share code, notes, and snippets.

@aspnetde
Last active April 20, 2016 08:36
Show Gist options
  • Select an option

  • Save aspnetde/bfcc32a4e2ac698b9ad2ff67d7b29cd9 to your computer and use it in GitHub Desktop.

Select an option

Save aspnetde/bfcc32a4e2ac698b9ad2ff67d7b29cd9 to your computer and use it in GitHub Desktop.
Merge Pdfs on the fly with Xamarin.iOS
public class PdfMerger
{
private readonly IFileSystemUtility _fileSystem;
public PdfMerger(IFileSystemUtility fileSystem)
{
_fileSystem = fileSystem;
}
public string Merge(params string[] paths)
{
// Returns a full path to a temporary file
string mergedPdfPath = _fileSystem.GetFullTempFileName(Guid.NewGuid() + ".pdf");
using (var context = new CGContextPDF(NSUrl.FromFilename(mergedPdfPath)))
{
foreach (string path in paths)
{
using (CGPDFDocument pdf = CGPDFDocument.FromFile(path))
{
for (nint i = 1; i <= pdf.Pages; i++)
{
using (CGPDFPage page = pdf.GetPage(i))
{
CGRect box = page.GetBoxRect(CGPDFBox.Media);
context.BeginPage(box);
context.DrawPDFPage(page);
context.EndPage();
}
}
}
}
context.Close();
}
return mergedPdfPath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment