Wednesday, November 21, 2018

Archiving your project's files in PDF


Archiving your projects files in PDF with easyPDF SDK

Archive your project files safely and securely. See how easy it is to develop a powerful PDF Converter that combines your project documents into a single PDF.

Here we are going to show you how to develop an application that combines and converts multiple files to PDF. Most companies need a tool to convert their artifacts to a single PDF for ease of record keeping and archival. Since a PDF document cannot be tampered, it is the most popular format for archival, and is widely supported. When you finish a project, you would want to transfer all the documents to one file in portable document format. This file then is safely and securely stored in your knowledge database.

Convert multiple files to PDF easy

Let’s suppose we want to build an app that does the following operations:

  1. Combine all files in different format inside the project folder together; 
  2. Convert those files to a single PDF file (that to be used as an archive); 
  3. Add date and time of the operation (conversion) at the top of each page, as a header; 
  4. Add a watermark (as an example: "Archive") on every page in the document. 
Let’s review how to do that with convert to PDF in-bulk application in C# with easy PDF SDK.

The coding process uses three core methods. We will begin with the core Method, that will make reference to two other specialized Methods as well, that will be discussed later.

First, this block below is primarily involved with preparation work: to get the file directory we are using, check to make sure it exists, and set up some information to sort through input files. In this case, we have restricted it to some general file types.

string iDIR = "";
List<string> iFILs = new List<string>();
List<string> mFILs = new List<string>();
List<string> fFILs = new List<string>();

List<string> cFILs = new List<string>() { ".doc", ".docx", ".rtf", ".txt", ".xls", ".xlsx", ".htm", ".html" };

if (args.Length >= 1) { iDIR = args[0]; }

Next, we get the list of files in the directory and sort through it to only convert files included in the list of cFILs which lists the File Extensions the application will happen.

string[] tiFILs = Directory.GetFiles(iDIR);
foreach(string s in tiFILs)
string ext = Path.GetExtension(s).ToLower();
if (cFILs.Contains(ext)) { iFILs.Add(s); }

After that, we convert each file. If it is successful, we add the output file's name to the mFILs Array to get a single list of filenames for merging later.

foreach(string s in iFILs)
string cvr = Convert(s);

if (File.Exists(cvr)) { mFILs.Add(cvr); }

else { fFILs.Add(s + ":" + cvr); }

Once the files are all converted, Merge the array of files into a single PDF. This DOES NOT Delete the Intermediary Files because this is just a demo. In a Production Application they would probably want to delete these files eventually, or even just convert them into memory, but that's more advanced.

if (mFILs.Count > 0)
res = Merge(mFILs.ToArray(), iDIR);

From here we need to look at the Convert Method. This is a very important Method because it not only converts the file, but takes advantage of PDFSetting Object to apply both the ‘ARCHIVE’ Watermark and the Conversion Heading in one single method call that automatically applies these to all pages in the PDF. Fortunately easyPDF SDK can apply multiple Watermarks which can each have their own settings.

Printer oPRIN = new Printer();
PrintJob oPJOB = oPRIN.PrintJob;

PDFSetting oPSET = oPJOB.PDFSetting;
oPSET.set_Watermark(0, true);
oPSET.set_WatermarkText(0, "ARCHIVE");
oPSET.set_WatermarkFirstPageOnly(0, false);
oPSET.set_WatermarkHPosition(0, prnWmarkHPosition.PRN_WMARK_HPOS_CENTER);
oPSET.set_WatermarkVPosition(0, prnWmarkVPosition.PRN_WMARK_VPOS_CENTER);
oPSET.set_WatermarkZOrder(0, prnWmarkZOrder.PRN_WMARK_ZORDER_TOP);
oPSET.set_WatermarkTextAlignment(0, prnWmarkAlignment.PRN_WMARK_ALIGN_LEFT);
oPSET.set_WatermarkHOffset(0, 0);
oPSET.set_WatermarkVOffset(0, 0);
oPSET.set_WatermarkFontName(0, "Arial");
oPSET.set_WatermarkFontSize(0, 100);
oPSET.set_WatermarkAngle(0, 60);
oPSET.set_WatermarkOutlineOnly(0, true);
oPSET.set_WatermarkColor(0, 0x0);
oPSET.set_WatermarkOpacity(0, 40);

oPSET.set_Watermark(1, true);
oPSET.set_WatermarkText(1, "Archived On :: " + DateTime.Now);
oPSET.set_WatermarkFirstPageOnly(1, false);
oPSET.set_WatermarkHPosition(1, prnWmarkHPosition.PRN_WMARK_HPOS_LEFT);
oPSET.set_WatermarkVPosition(1, prnWmarkVPosition.PRN_WMARK_VPOS_TOP);
oPSET.set_WatermarkZOrder(1, prnWmarkZOrder.PRN_WMARK_ZORDER_TOP);
oPSET.set_WatermarkTextAlignment(1, prnWmarkAlignment.PRN_WMARK_ALIGN_LEFT);
oPSET.set_WatermarkHOffset(1, 1);
oPSET.set_WatermarkVOffset(1, 0.5);
oPSET.set_WatermarkFontName(1, "Arial");
oPSET.set_WatermarkFontSize(1, 8);
oPSET.set_WatermarkAngle(1, 0);
oPSET.set_WatermarkOutlineOnly(1, false);
oPSET.set_WatermarkColor(1, 0x0);
oPSET.set_WatermarkOpacity(1, 100);
oPJOB.PrintOut(iFile, iFile + ".pdf");
res = iFile + ".pdf";
catch(PrinterException ex)
res = "ERROR : " + iFile + " : " + ex.Message;
res += @"\n" + " CRM : " + oPJOB.ConversionResultMessage;
res += @"\n" + " PRM : " + oPJOB.PrinterResultMessage;

Since we are done with converting and processing PDF files, now we need to merge all of them together. The Merging Method is far simpler, because since we have the Array List of Files, easy PDF SDK has a Method basically built to do this right out of the box. Namely, MergeBatch.

PDFProcessor oPROC = new PDFProcessor();
oPROC.OptimizeAfterEachProcess = true;
oPROC.MergeBatch(iFiles, iDIR + "Output.pdf");
oPROC.Optimize(iDIR + "Output.pdf", iDIR + "Output.pdf", "");
res = "SUCCESS" + " :: Output File :: " + iDIR + "Output.pdf";
catch(PDFProcessorException ex)
res = "ERROR : " + ex.Message;

That is it! We did it. At the end of the process you will get a powerful application that can combine and convert multiple files to PDF and add additional information to it. Keep your project data safe and secure, accessible with a mouse click, and eliminate any anxiety of losing it.

Read more about our PDF SDK features on our website. Do not hesitate to check how it performs all PDF operations in a variety of programing languages. Download it now and start a free trial today.

No comments:
Write comments

© 1993 - , BCL Technologies. All other trademarks are the property of their respective owners. Privacy Policy