Scenario: Enhance an existing feature on the server side to complete the process faster. Using a queue mechanism we have tried to go from sequential process to kind-of parallel processing.
Client invokes a Webmethod on a webservice that takes each page of the PDF and saves it a Bitmap image.
Characteristics:
Each document set will have 20 PDFs.
Concurrent requests: 8-12 (8-12 is the concurrent requests when there is a document package being processed, however there are times when there are no requests to this method at all, this is a shared server.) We do not see the exception when we process one document at a time sequentially.
Typical PDF size = 4-6 pages
One in every 20 PDF will have 20-40 pages
docId-reportId combination is unique, no duplicate requests are sent.
Library used PDFSharp-WPF Version 1.32.2608.0 App/Webservice is in .NET 4.0
Code:
public int SplitReportPDFToImages(int docId, int ReportId, int SortOrder, string filePath)
{
int retVal = 0;
string imagPath = ConfigurationManager.AppSettings["REPORT_SPLIT_IMAGES_FOLDER"];
imagPath = imagPath + docId.ToString();
string baseFileName = docId.ToString()+"_"+ReportId.ToString()+"_"+SortOrder.ToString()+"_";
if (!Directory.Exists(imagPath))
{
Directory.CreateDirectory(imagPath);
}
try
{
PdfXpress gPdfXpress = new PdfXpress();
Document document = null;
gPdfXpress.Initialize();
document = new Document(gPdfXpress, filePath);
RectangleF selectionInInches = new RectangleF(0.0f, 0.0f, 0.0f, 0.0f);
SizeF resolutionInDpi = new SizeF(96, 96);
Bitmap bitmap = null;
RenderOptions options = new RenderOptions();
// Create a render options object.
options.ReduceToBitonal = false;
options.ResolutionX = (double)resolutionInDpi.Width;
options.ResolutionY = (double)resolutionInDpi.Height;
// Express the selection area as userspace units
const double UserspaceDpi = 72.0;
options.SelectionX = UserspaceDpi * selectionInInches.Left;
options.SelectionY = UserspaceDpi * selectionInInches.Bottom;
options.SelectionWidth = UserspaceDpi * selectionInInches.Width;
options.SelectionHeight = UserspaceDpi * selectionInInches.Height;
DataTable dtImagesList = new DataTable();
dtImagesList.Columns.AddRange(new DataColumn[6] { new DataColumn("docId", typeof(int)), new DataColumn("ReportId", typeof(int)), new DataColumn("SortOrder", typeof(int)), new DataColumn("PageNo", typeof(int)),
new DataColumn("ImageFileName", typeof(string)),
new DataColumn("ImageHeight", typeof(int)) });
// Render the selection area to a System.Drawing.Bitmap object instance
int docPageCount = document.PageCount;
for (int itr = 0; itr < docPageCount; itr++)
{
int pageShown = itr + 1;
bitmap = document.RenderPageToBitmap(itr, options);
bitmap.Save(String.Format("{0}\\{1}.gif", imagPath, baseFileName + pageShown.ToString()), ImageFormat.Gif);
int imageHeight = bitmap.Height;
bitmap.Dispose();
bitmap = null;
dtImagesList.Rows.Add(docId, ReportId, SortOrder, pageShown, baseFileName + pageShown.ToString()+".GIF", imageHeight);
}
ProjectDao.InsertImagesList(dtImagesList);
retVal = 1;
if (document != null)
{
document.Dispose();
document = null;
}
if (null != gPdfXpress)
{
gpdfXpress.Dispose();
gPdfXpress = null;
}
return retVal;
}
catch (Exception ex)
{
return 0;
}
}