Exemplo n.º 1
0
        private void createPDF(PDFJob job, string outFld)
        {
            if (!Directory.Exists(outFld))
            {
                Directory.CreateDirectory(outFld);
            }
            DateTime currentDate    = DateTime.Now;
            string   strNow         = currentDate.ToString("yyyyMMddHHmmssffff", System.Globalization.CultureInfo.GetCultureInfo("en-US"));
            string   strPDFFilename = strNow + "_" + job.orderid + "_" + job.id + "_" + job.sku + ".pdf";
            string   strOutputPDF   = outFld + strPDFFilename;

            FileStream fs = new FileStream(strOutputPDF, FileMode.Create);

            switch (job.pgunit)
            {
            case "mm":
                job.fpgwidth  = Utilities.MillimetersToPoints(job.fpgwidth);
                job.fpgheight = Utilities.MillimetersToPoints(job.fpgheight);
                job.fpgbleed  = Utilities.MillimetersToPoints(job.fpgbleed);
                break;

            case "in":
                job.fpgwidth  = job.fpgwidth * 72;
                job.fpgheight = job.fpgheight * 72;
                job.fpgbleed  = job.fpgbleed * 72;
                break;

            default:
                break;
            }

            // pgsz is mediabox
            Rectangle pgsz = new Rectangle(job.fpgwidth + (2 * job.fpgbleed), job.fpgheight + (2 * job.fpgbleed));
            // trbx = trimbox i.e. actual pagesize
            Rectangle trbx = new Rectangle(job.fpgbleed, job.fpgbleed, job.fpgwidth + job.fpgbleed, job.fpgheight + job.fpgbleed);
            // blbx = bleedbox i.e. setting to be the mediabox
            Rectangle blbx = new Rectangle(0, 0, job.fpgwidth + (2 * job.fpgbleed), job.fpgheight + (2 * job.fpgbleed));

            Document  doc    = new Document(pgsz);
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);

            writer.SetBoxSize("bleed", blbx);
            writer.SetBoxSize("trim", trbx);
            // Open the document to enable you to write to the document
            doc.Open();
            foreach (string strImg in job.pgimg)
            {
                Image imgPage = Image.GetInstance(strImg);
                imgPage.Alignment = Element.ALIGN_CENTER;
                imgPage.ScaleToFit(job.fpgwidth + (2 * job.fpgbleed), job.fpgheight + (2 * job.fpgbleed));
                imgPage.SetAbsolutePosition(0, 0);
                doc.Add(imgPage);

                doc.NewPage();
            }

            // Close the document
            doc.Close();
            // Close the writer instance
            writer.Close();
            // Always close open filehandles explicity
            fs.Close();
        }
Exemplo n.º 2
0
        void StartProcessXML(string xmlfile)
        {
            //update notification textbox
            string   filename    = Path.GetFileName(xmlfile);
            DateTime currentDate = DateTime.Now;

            updateLog(filename + " -- " + currentDate.ToLocalTime());

            string            message = "";
            string            caption = "Error";
            MessageBoxButtons buttons = MessageBoxButtons.OK;

            bool blnXmlIsValid = false;

            string OutputPath = txtOutPDFFolder.Text.Trim();

            if (OutputPath == "")
            {
                OutputPath = strWatchFolder + "\\" + cOutput + "\\";
            }
            else
            {
                OutputPath += "\\";
                if (!Directory.Exists(OutputPath))
                {
                    OutputPath = strWatchFolder + "\\" + cOutput + "\\";
                }
            }

            //validate xml
            try
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.Schemas.Add(null, cJobXSD);
                settings.ValidationType = ValidationType.Schema;

                using (XmlReader reader = XmlReader.Create(xmlfile, settings))
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(reader);

                    PDFJob  job     = new PDFJob();
                    XmlNode jobNode = document.SelectSingleNode("job");

                    job.id      = jobNode.Attributes["id"].Value;
                    job.orderid = jobNode["orderid"].InnerText;
                    job.sku     = jobNode["sku"].InnerText;

                    XmlNode pagesize = jobNode["pagesize"];
                    job.pgunit    = pagesize["unit"].InnerText;
                    job.fpgwidth  = float.Parse(pagesize["width"].InnerText);
                    job.fpgheight = float.Parse(pagesize["height"].InnerText);
                    job.fpgbleed  = float.Parse(pagesize["bleed"].InnerText);

                    XmlNode pages = jobNode["pages"];
                    job.imgfld = pages.Attributes["imgfld"].Value;
                    //check if image folder exist
                    if (!Directory.Exists(job.imgfld))
                    {
                        updateLog(job.imgfld + " folder does not exist!");
                        blnXmlIsValid = false;
                        goto outer;
                    }

                    List <string> pgimg = new List <string>();
                    foreach (XmlNode page in pages.ChildNodes)
                    {
                        string strImg  = page.Attributes["img"].Value;
                        string strJpeg = job.imgfld + "\\" + strImg;
                        // check file exist
                        if (!File.Exists(strJpeg))
                        {
                            updateLog(strJpeg + " image file does not exist!");
                            blnXmlIsValid = false;
                            goto outer;
                        }
                        pgimg.Add(strJpeg);
                    }
                    job.pgimg = new List <string>(pgimg);

                    createPDF(job, OutputPath);
                }

                // everything is ok
                blnXmlIsValid = true;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                MessageBox.Show(message, caption, buttons);
                blnXmlIsValid = false;
            }

outer:
            // once done, move to success directory
            string donePath = "";

            if (blnXmlIsValid)
            {
                donePath = strWatchFolder + "\\" + cSuccess + "\\";
                updateLog("OK: " + filename);
            }
            else
            {
                donePath = strWatchFolder + "\\" + cError + "\\";
                updateLog("ERROR: " + filename);
            }
            //check first
            if (!Directory.Exists(donePath))
            {
                Directory.CreateDirectory(donePath);
            }

            string sourceFile      = xmlfile;
            string destinationFile = donePath + filename;

            try
            {
                // To move a file or folder to a new location
                File.Move(sourceFile, destinationFile);
            }
            catch (Exception e)
            {
                if (e is IOException)
                {
                    File.Copy(sourceFile, destinationFile, true);
                    File.Delete(sourceFile);
                }
                else
                {
                    // do nothing
                }
            }
        }