Beispiel #1
0
        /// <summary>
        /// Converts an image file such as Tiff to PDF
        /// </summary>
        /// <param name="originalFileName">the file path and name with its extension</param>
        /// <returns>filepath and filename of the converted image file to a PDF file</returns>
        public string ConvertImageToPdf(string originalFileName)
        {
            try
            {
                if (!System.IO.File.Exists(originalFileName))
                {
                    return(string.Empty);
                }

                using (XImage theImg = new XImage())
                {
                    using (Doc theDoc = new Doc())
                    {
                        string filename = originalFileName.Remove(originalFileName.LastIndexOf(".")) + ".pdf";

                        theImg.SetFile(originalFileName);

                        for (int i = 1; i <= theImg.FrameCount; i++)
                        {
                            theImg.Frame = i;
                            theDoc.Page  = theDoc.AddPage();
                            theDoc.AddImageObject(theImg, false);
                        }

                        theImg.Clear();
                        theDoc.Save(filename);
                        theDoc.Clear();
                        return(filename);
                    }
                }
            }
            catch (Exception e)
            {
                //this.Log().Error(string.Format("Error converting image to Pdf: {0}", originalFileName), e);
                //Throw the stack trace with it.
                throw;
            }
        }
Beispiel #2
0
        public async Task FormatHeaderSection(int startingPageNumber, string firstHeaderField,
                                              string secondHeaderField, string thirdHeaderField,
                                              bool isHeaderLogoRequired = false)
        {
            try
            {
                //theDoc.Rect.String = "36 770 700 800";
                //theDoc.TextStyle.HPos = 0.5;
                //theDoc.TextStyle.VPos = 0.5;
                //theDoc.Color.String = "0 255 0";

                theDoc.Rect.String  = "39 732 0 0";
                theDoc.Color.String = "0 255 0";

                Color backgroundColour = ColorTranslator.FromHtml(BackgroundColor);
                Color borderColor      = ColorTranslator.FromHtml(BorderColor);

                int headerID = 0;

                for (int page = startingPageNumber; page <= theDoc.PageCount; page++)
                {
                    theDoc.PageNumber = page;
                    theDoc.Flatten();
                    if (page == startingPageNumber)
                    {
                        theDoc.Rect.Top = 200;
                        if (isHeaderLogoRequired)
                        {
                            theDoc.TextStyle.HPos = 0;

                            headerID = theDoc.AddImageUrl(this.ImageResize(this.LogoUrl, 200, 40));

                            //if (logoURL != null)
                            //{

                            //}
                            //else
                            //{
                            //    headerID = theDoc.AddImageUrl(WebURl + (string.IsNullOrWhiteSpace(logoURL) == true ? ImageURl : logoURL));
                            //}
                        }
                    }
                    else
                    {
                        theDoc.TextStyle.HPos = 0;
                        //headerID = theDoc.AddImageUrl(WebURl + (string.IsNullOrWhiteSpace(logoURL) == true ? ImageURl : logoURL));
                        XImage image        = new XImage();
                        string tempImageUrl = "";
                        //if (logoURL != null)
                        //{

                        tempImageUrl = this.ImageResize(LogoUrl, 200, 40);


                        //headerID = theDoc.AddImageUrl(this.ImageResize(logoURL, 200, 40));
                        //theDoc.Rect.Width = 200;
                        //theDoc.Rect.Height = 40;
                        image.SetFile(tempImageUrl);
                        theDoc.Rect.Width  = image.Width;
                        theDoc.Rect.Height = image.Height;
                        theDoc.AddImageObject(image, true);
                        image.Clear();
                        //}
                        //else
                        //{
                        //    headerID = theDoc.AddImageUrl(WebURl + (string.IsNullOrWhiteSpace(logoURL) == true ? ImageURl : logoURL));
                        //}
                    }
                }

                theDoc.Rect.String    = "36 735 866 920";
                theDoc.TextStyle.HPos = 0.5;
                theDoc.TextStyle.VPos = 0.5;

                for (int page = startingPageNumber; page <= theDoc.PageCount; page++)
                {
                    theDoc.PageNumber = page;
                    if (page == startingPageNumber)
                    {
                        theDoc.Rect.Top = 200;
                        if (isHeaderLogoRequired)
                        {
                            theDoc.TextStyle.HPos = 0;
                            //theDoc.AddImageUrl(WebURl + (string.IsNullOrWhiteSpace(headerURL) == true ? HeaderURl : headerURL));
                            if (BackgroundColor != null)
                            {
                                Rectangle rc = theDoc.MediaBox.Rectangle;
                                rc.Inflate(-40, -61);
                                rc.Height             = 20;
                                rc.Width              = 530;
                                theDoc.Rect.Rectangle = rc;
                                theDoc.Color.String   = string.Format("{0} {1} {2} ", backgroundColour.R, backgroundColour.G, backgroundColour.B);
                                theDoc.FillRect();
                            }

                            if (BorderColor != null)
                            {
                                Rectangle rc1 = theDoc.MediaBox.Rectangle;
                                rc1.Inflate(-40, -81);
                                rc1.Height            = 3;
                                rc1.Width             = 530;
                                theDoc.Rect.Rectangle = rc1;
                                theDoc.Color.String   = string.Format("{0} {1} {2} ", borderColor.R, borderColor.G, borderColor.B);
                                theDoc.FillRect();
                            }
                        }
                    }
                    else
                    {
                        //if (isHeaderLogoRequired)
                        //{
                        theDoc.TextStyle.HPos = 0;
                        //theDoc.AddImageUrl(WebURl + (string.IsNullOrWhiteSpace(headerURL) == true ? HeaderURl : headerURL));
                        if (BackgroundColor != null)
                        {
                            Rectangle rc = theDoc.MediaBox.Rectangle;
                            rc.Inflate(-40, -61);
                            rc.Height             = 20;
                            rc.Width              = 530;
                            theDoc.Rect.Rectangle = rc;
                            theDoc.Color.String   = string.Format("{0} {1} {2} ", backgroundColour.R, backgroundColour.G, backgroundColour.B);
                            theDoc.FillRect();
                        }

                        if (BorderColor != null)
                        {
                            Rectangle rc1 = theDoc.MediaBox.Rectangle;
                            rc1.Inflate(-40, -81);
                            rc1.Height            = 3;
                            rc1.Width             = 530;
                            theDoc.Rect.Rectangle = rc1;
                            theDoc.Color.String   = string.Format("{0} {1} {2} ", borderColor.R, borderColor.G, borderColor.B);
                            theDoc.FillRect();
                        }
                        //}
                    }
                }
                int previewOrderID = 0;
                if (previewOrderID != 0)
                {
                    string headerInfo = string.Empty;
                    headerInfo = firstHeaderField + "                  ";
                    if (!string.IsNullOrEmpty(secondHeaderField))
                    {
                        headerInfo = headerInfo + secondHeaderField + "                  ";
                    }
                    if (!string.IsNullOrEmpty(thirdHeaderField))
                    {
                        headerInfo = headerInfo + thirdHeaderField;
                    }

                    theDoc.Rect.String = "42 172 866 1270";
                    theDoc.FontSize    = 8;
                    for (int page = 1; page <= theDoc.PageCount; page++)
                    {
                        theDoc.PageNumber = page;

                        //if (documentType == OrderConstants.DocumentType.Invoice)
                        //{
                        if (page != startingPageNumber || startingPageNumber == theDoc.PageCount)
                        {
                            theDoc.Color.String   = "245 245 245";
                            theDoc.TextStyle.HPos = 0;
                            theDoc.AddText(headerInfo);
                        }
                        //}
                        //else
                        //{
                        //if (documentType == OrderConstants.DocumentType.Report)
                        //{
                        //    theDoc.Color.String = "255 0 0";
                        //    theDoc.TextStyle.HPos = 1.0;
                        //    theDoc.AddText(headerInfo);
                        //}
                        //}
                    }
                }

                await Task.FromResult(0);

                return;
                //await Task.Delay(1000);
            }
            catch (Exception)
            {
                throw;
            }
        }