Hello,
I'm attempting to split a Pdf by removing pages. After page removal, I export the RadFixedDocument back to a PDF through a stream. I've tried various streams like MemoryStream and FileStream without luck. All PDFs come about with blank pages. Code is below.
I'm attempting to split a Pdf by removing pages. After page removal, I export the RadFixedDocument back to a PDF through a stream. I've tried various streams like MemoryStream and FileStream without luck. All PDFs come about with blank pages. Code is below.
public static IEnumerable<
string
> SplitPdf(PdfFileDefinition pdfFileDef)
{
List<
string
> pdfs = new List<
string
>();
Stream stream = null;
PdfFormatProvider formatProvider = new PdfFormatProvider();
// Save temp file to disk
var path = @"c:\temp";
if(!Directory.Exists(path))
Directory.CreateDirectory(path);
// Pdf streams must support read & seek operations
var fileUri = new Uri(pdfFileDef.FilePath);
// Is pdf a file or resource
if (fileUri.Scheme == "file")
{
var tempFilePath = Path.Combine(path, "temp.pdf");
File.Copy(Path.Combine(path,fileUri.LocalPath), tempFilePath,true);
stream = File.Open(tempFilePath, FileMode.Open,FileAccess.Read);
}
else if (fileUri.Scheme == "pack" && fileUri.Host.Contains("siteoforigin"))
{
stream = Application.GetRemoteStream(fileUri).Stream;
}
foreach(var pageBreak in pdfFileDef.PageBreaks)
{
RadFixedDocument document = new PdfFormatProvider(stream, FormatProviderSettings.ReadOnDemand).Import();
RadFixedDocument splitDocument = new RadFixedDocument();
splitDocument = document;
for (int pageNumber = splitDocument.Pages.Count - 1; pageNumber >= 0; pageNumber--)
{
if(!pageNumber.IsBetween<
int
>(pageBreak.StartPage, pageBreak.EndPage))
{
splitDocument.Pages.RemoveAt(pageNumber);
}
}
// Generate temp pdf file for review
var fileName = Path.Combine(path, Path.GetRandomFileName());
fileName = Path.ChangeExtension(fileName, ".pdf");
// Export filestream to Pdf
using(var fs = new FileStream(fileName,FileMode.Create))
{
formatProvider.Export(splitDocument, fs);
}
pdfs.Add(fileName);
}
return pdfs;
}