Reputation: 2621
We have a C# Windows service that currently processes all the PDFs by reading the 2D barcode on the PDF using a 3rd party component and then updates the database and stores the document in the Document repository.
Is there a way I can cut the files after reading the barcode and store it as another document?
For example if there is a 10 page document, it should split into 10 different files.
Thanks.
Upvotes: 14
Views: 54149
Reputation: 815
An easy way , using Spire PDF :
using Spire.Pdf;
namespace split_pdf
{
class Program
{
static void Main(string[] args)
{
//open an existing pdf document
PdfDocument doc = new PdfDocument(@"..\Sample3.pdf");
//Split one PDF document to multiple files
String pattern = "SplitDocument-{0}.pdf";
doc.Split(pattern);
String lastPageFileName
= String.Format(pattern, doc.Pages.Count - 1);
doc.Close();
}
}
}
here the Documentation Page : SpirePdf
Upvotes: -1
Reputation: 1
public void SplitPDFByBookMark(string fileName)
{
string sInFile = fileName;
var pdfReader = new PdfReader(sInFile);
try
{
IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(pdfReader);
for (int i = 0; i < bookmarks.Count; ++i)
{
IDictionary<string, object> BM = (IDictionary<string, object>)bookmarks[i];
IDictionary<string, object> nextBM = i == bookmarks.Count - 1 ? null : bookmarks[i + 1];
string startPage = BM["Page"].ToString().Split(' ')[0].ToString();
string startPageNextBM = nextBM == null ? "" + (pdfReader.NumberOfPages + 1) : nextBM["Page"].ToString().Split(' ')[0].ToString();
SplitByBookmark(pdfReader, int.Parse(startPage), int.Parse(startPageNextBM), bookmarks[i].Values.ToArray().GetValue(0).ToString() + ".pdf", fileName);
}
}
catch (Exception ex)
{
throw ex;
}
}
private void SplitByBookmark(PdfReader reader, int pageFrom, int PageTo, string outPutName, string inPutFileName)
{
Document document = new Document();
using (var fs = new FileStream(Path.GetDirectoryName(inPutFileName) + '\\' + outPutName, System.IO.FileMode.Create))
{
try
{
using (var writer = PdfWriter.GetInstance(document, fs))
{
document.Open();
PdfContentByte cb = writer.DirectContent;
//holds pdf data
PdfImportedPage page;
if (pageFrom == PageTo && pageFrom == 1)
{
document.NewPage();
page = writer.GetImportedPage(reader, pageFrom);
cb.AddTemplate(page, 0, 0);
pageFrom++;
fs.Flush();
document.Close();
fs.Close();
}
else
{
while (pageFrom < PageTo)
{
document.NewPage();
page = writer.GetImportedPage(reader, pageFrom);
cb.AddTemplate(page, 0, 0);
pageFrom++;
fs.Flush();
document.Close();
fs.Close();
}
}
}
//PdfWriter writer = PdfWriter.GetInstance(document, fs);
}
catch (Exception ex)
{
throw ex;
}
}
}
You can install itextsharp
from nuget and copy and paste this code in a c# app Call the SplitPDFByBookMark()
method and pass the pdf filename.
This code is going to search for you bookmarks and done!
Upvotes: 0
Reputation: 72
public int ExtractPages(string sourcePdfPath, string DestinationFolder)
{
int p = 0;
try
{
iTextSharp.text.Document document;
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdfPath), new ASCIIEncoding().GetBytes(""));
if (!Directory.Exists(sourcePdfPath.ToLower().Replace(".pdf", "")))
{
Directory.CreateDirectory(sourcePdfPath.ToLower().Replace(".pdf", ""));
}
else
{
Directory.Delete(sourcePdfPath.ToLower().Replace(".pdf", ""), true);
Directory.CreateDirectory(sourcePdfPath.ToLower().Replace(".pdf", ""));
}
for (p = 1; p <= reader.NumberOfPages; p++)
{
using (MemoryStream memoryStream = new MemoryStream())
{
document = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, memoryStream);
writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_2);
writer.CompressionLevel = iTextSharp.text.pdf.PdfStream.BEST_COMPRESSION;
writer.SetFullCompression();
document.SetPageSize(reader.GetPageSize(p));
document.NewPage();
document.Open();
document.AddDocListener(writer);
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
iTextSharp.text.pdf.PdfImportedPage pageImport = writer.GetImportedPage(reader, p);
int rot = reader.GetPageRotation(p);
if (rot == 90 || rot == 270)
{
cb.AddTemplate(pageImport, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
}
else
{
cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0);
}
document.Close();
document.Dispose();
File.WriteAllBytes(DestinationFolder + "/" + p + ".pdf", memoryStream.ToArray());
}
}
reader.Close();
reader.Dispose();
}
catch
{
}
finally
{
GC.Collect();
}
return p - 1;
}
call this function where ever you want and pass the source and destination folder path
Upvotes: 2
Reputation: 1
This code is based on the PDFsharp library
http://www.pdfsharp.com/PDFsharp/
If you want to split by Book Mark then here is the code.
public static void SplitPDFByBookMark(string fileName)
{
string sInFile = fileName;
PdfReader pdfReader = new PdfReader(sInFile);
try
{
IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(pdfReader);
for (int i = 0; i < bookmarks.Count; ++i)
{
IDictionary<string, object> BM = (IDictionary<string, object>)bookmarks[0];
IDictionary<string, object> nextBM = i == bookmarks.Count - 1 ? null : bookmarks[i + 1];
string startPage = BM["Page"].ToString().Split(' ')[0].ToString();
string startPageNextBM = nextBM == null ? "" + (pdfReader.NumberOfPages + 1) : nextBM["Page"].ToString().Split(' ')[0].ToString();
SplitByBookmark(pdfReader, int.Parse(startPage), int.Parse(startPageNextBM), bookmarks[i].Values.ToArray().GetValue(0).ToString() + ".pdf", fileName);
}
}
catch (Exception ex)
{
throw ex;
}
}
private static void SplitByBookmark(PdfReader reader, int pageFrom, int PageTo, string outPutName, string inPutFileName)
{
Document document = new Document();
FileStream fs = new System.IO.FileStream(System.IO.Path.GetDirectoryName(inPutFileName) + '\\' + outPutName, System.IO.FileMode.Create);
try
{
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = writer.DirectContent;
//holds pdf data
PdfImportedPage page;
if (pageFrom == PageTo && pageFrom == 1)
{
document.NewPage();
page = writer.GetImportedPage(reader, pageFrom);
cb.AddTemplate(page, 0, 0);
pageFrom++;
fs.Flush();
document.Close();
fs.Close();
}
else
{
while (pageFrom < PageTo)
{
document.NewPage();
page = writer.GetImportedPage(reader, pageFrom);
cb.AddTemplate(page, 0, 0);
pageFrom++;
fs.Flush();
document.Close();
fs.Close();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (document.IsOpen())
document.Close();
if (fs != null)
fs.Close();
}
}
Upvotes: 0
Reputation: 71
I met the same question, you can use itextsharp component tools to split the document
public Split(String[] args)
{
if (args.Length != 4)
{
Console.Error.WriteLine("This tools needs 4 parameters:\njava Split srcfile destfile1 destfile2 pagenumber");
}
else
{
try
{
int pagenumber = int.Parse(args[3]);
// we create a reader for a certain document
PdfReader reader = new PdfReader(args[0]);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
if (pagenumber < 2 || pagenumber > n)
{
throw new DocumentException("You can't split this document at page " + pagenumber + "; there is no such page.");
}
// step 1: creation of a document-object
Document document1 = new Document(reader.GetPageSizeWithRotation(1));
Document document2 = new Document(reader.GetPageSizeWithRotation(pagenumber));
// step 2: we create a writer that listens to the document
PdfWriter writer1 = PdfWriter.GetInstance(document1, new FileStream(args[1], FileMode.Create));
PdfWriter writer2 = PdfWriter.GetInstance(document2, new FileStream(args[2], FileMode.Create));
// step 3: we open the document
document1.Open();
PdfContentByte cb1 = writer1.DirectContent;
document2.Open();
PdfContentByte cb2 = writer2.DirectContent;
PdfImportedPage page;
int rotation;
int i = 0;
// step 4: we add content
while (i < pagenumber - 1)
{
i++;
document1.SetPageSize(reader.GetPageSizeWithRotation(i));
document1.NewPage();
page = writer1.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
while (i < n)
{
i++;
document2.SetPageSize(reader.GetPageSizeWithRotation(i));
document2.NewPage();
page = writer2.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb2.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb2.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
Console.WriteLine("Processed page " + i);
}
// step 5: we close the document
document1.Close();
document2.Close();
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
}
}
Upvotes: 7
Reputation: 72230
You can use a PDF library like PDFSharp, read the file, iterate through each of the pages, add them to a new PDF document and save them on the filesystem. You can then also delete or keep the original.
It's quite a bit of code, but very simple and these samples should get you started.
Upvotes: 4
Reputation: 11502
A previous question answers your partially - how to split pdf documents, if you know where the barcodes are then you can split the documents easily:
How can I split up a PDF file into pages (preferably C#)
The recommendation is a library called PDFSharp and a sample demonstrating PDF splitting.
Upvotes: 2