I haven't seen this particular situation mentioned anywhere on the boards.
I have a project where a vendor is sending us a single PDF containing a number of individual documents (1,800 individual documents, to be exact). Using PDFSharp, we created a routine to split these individual documents into their own PDF. This process splits the documents out correctly.
However, we are unable to open the resulting split PDFs with Adobe Acrobat or Adobe Reader. It opens fine in FoxIt, but we aren't going to be able to go to production without it opening via Adobe. We get the following error message on each page:
Dictionary keys must be direct name objects.
All pages in the document are blank, although their snapshots have the correct page dimensions/orientation.
I'll include the code below. I'd appreciate it if someone could tell me if I'm doing something wrong here, but if this is simply a limitation of PDFSharp I need to know so I can find a substitute tool.
Thanks for the assistance!
Nate Southerland
Code follows:
Code:
//Clipped
using (var combinedPdf = LoadCombinedPdf())
{
var firstPageIndices = GetEobFirstPageIndicesFromCombinedPdf(combinedPdf).ToArray();
var eobPackets = SplitIntoEobPackets(combinedPdf, firstPageIndices);
SaveEobPackets(eobPackets);
}
//Clipped
private PdfDocument LoadCombinedPdf()
{
if (Directory.Exists(sourceDirectory))
{
var pdfsInSourceDirectory = Directory.GetFiles(sourceDirectory, "*.pdf");
return PdfReader.Open(pdfsInSourceDirectory.First(), PdfDocumentOpenMode.Import);
}
return null;
}
private IEnumerable<int> GetEobFirstPageIndicesFromCombinedPdf(PdfDocument combinedPdf)
{
for (int i = 0; i < combinedPdf.PageCount; i++)
{
var text = combinedPdf.Pages[i].ExtractText();
if (text.Contains(newPageToken))
yield return i;
}
}
private IEnumerable<EobPdf> SplitIntoEobPackets(PdfDocument combinedPdf, int[] firstPageIndices)
{
var packets = new List<EobPdf>();
foreach (var index in firstPageIndices)
{
var eobPdf = ConfigureNewEobPdf(combinedPdf.Pages[index]);
var newDoc = new PdfDocument();
newDoc.Version = combinedPdf.Version;
newDoc.Info.Creator = "Creator";
var lastPageIndex = combinedPdf.Pages.Count - 1;
var goToNextPage = true;
for (int i = index; goToNextPage; i++)
{
newDoc.AddPage(combinedPdf.Pages[i]);
var nextIndex = i + 1;
goToNextPage = nextIndex <= lastPageIndex && !firstPageIndices.Contains(nextIndex);
}
eobPdf.Content = newDoc;
packets.Add(eobPdf);
}
return packets;
}
private EobPdf ConfigureNewEobPdf(PdfPage eobFrontPage)
{
var rawMemberId = GetMemberId(eobFrontPage);
var memberId = string.IsNullOrWhiteSpace(rawMemberId) ? "ERR" : rawMemberId;
var letterDate = GetLetterDate(eobFrontPage);
return new EobPdf()
{
MemberID = memberId,
EobDate = letterDate,
FullFileName = string.Format("{0}\\{1:yyyyMMdd}-{2} FileName.pdf", targetDirectory, letterDate, memberId) };
}
private static string GetMemberId(PdfPage eobFrontPage)
{
var splitMemberIdLine = eobFrontPage.ExtractText().Where(p => p.IndexOf("Member ID:") > -1).First().Split(':');
if (splitMemberIdLine.Count() > 1)
return splitMemberIdLine[1].Trim();
return string.Empty;
}
private DateTime GetLetterDate(PdfPage eobFrontPage)
{
var contents = eobFrontPage.ExtractText();
DateTime letterDate = DateTime.MinValue;
foreach (var phrase in contents)
{
if (DateTime.TryParse(phrase, out letterDate))
return letterDate;
}
return DateTime.MinValue;
}
private void SaveEobPackets(IEnumerable<EobPdf> eobPackets)
{
foreach (var eob in eobPackets)
{
if (Directory.Exists(targetDirectory))
SaveFile(eob);
}
}
private void SaveFile(EobPdf file, int iterations = 0)
{
var newFilename = iterations == 0 ? file.FullFileName :
GetIterativeFilename(file.FullFileName, iterations);
if (File.Exists(newFilename))
SaveFile(file, iterations + 1);
else
file.Content.Save(newFilename);
}
private string GetIterativeFilename(string fullFileName, int iterations)
{
var fileNameParts = fullFileName.Split('.');
return string.Format("{0}{1}.pdf", fileNameParts[0], iterations);
}