Last active
April 20, 2016 08:36
-
-
Save aspnetde/bfcc32a4e2ac698b9ad2ff67d7b29cd9 to your computer and use it in GitHub Desktop.
Merge Pdfs on the fly with Xamarin.iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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