예제 #1
0
        public void fromvariableCreateDocument(GenericDocumentParameters in_documentArgs, String obtainedPath)
        {
            //prepare important days list
            MainXMLprocessor importantDaysProcessor = new MainXMLprocessor();
            importantdays    alldaysListRaw         = importantDaysProcessor.loadImportantDaysListFromFile(obtainedPath);
            Dictionary <System.DateTime, List <importantday> > importantDaysProcessed = importantDaysProcessor.getDictionaryForProcessing(alldaysListRaw);
            //=========
            DocX document = DocX.Create(in_documentArgs.documentName);

            // https://stackoverflow.com/a/90699/
            ConsoleAppCalendar.Properties.Resources.sunrise1f305.Save("sunrisetmp.png");
            ConsoleAppCalendar.Properties.Resources.sunset1f307.Save("sunsettmp.png");
            // https://xceed.com/wp-content/documentation/xceed-words-for-net/Xceed.Words.NET~Xceed.Words.NET.DocX~AddImage(Stream,String).html
            Xceed.Words.NET.Image imgSunrise = document.AddImage("sunrisetmp.png"); Xceed.Words.NET.Picture picSunrise = imgSunrise.CreatePicture();
            picSunrise.Height = (int)Math.Round(picSunrise.Height * 0.32);
            picSunrise.Width  = (int)Math.Round(picSunrise.Width * 0.32);
            Xceed.Words.NET.Image imgSunset = document.AddImage("sunsettmp.png"); Xceed.Words.NET.Picture picSunset = imgSunset.CreatePicture();
            picSunset.Height = (int)Math.Round(picSunset.Height * 0.38);
            picSunset.Width  = (int)Math.Round(picSunset.Width * 0.38);
            System.DateTime theCurrentDate = date1;
            do
            {
                document.PageHeight = in_documentArgs.pageHeight;
                document.PageWidth  = in_documentArgs.pageWidth;
                float btmMargin = 1.0f; float topMargin = 1.0f; float leftMargin = 1.0f; float rightMargin = 1.0f;
                document.MarginBottom = cmToPoints(btmMargin); document.MarginTop = cmToPoints(topMargin); document.MarginLeft = cmToPoints(leftMargin); document.MarginRight = cmToPoints(rightMargin);
                Table  insertedTable = document.InsertTable(in_documentArgs.numrowsTable, in_documentArgs.numcolsTable);
                Border b             = new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.Blue);
                //calculate each column width
                float bestColumnWidth = cmToPixels(((float)in_documentArgs.pageWidth - (float)(leftMargin + rightMargin)) / (float)(in_documentArgs.numcolsTable));
                float bestRowHeightPt = (((float)in_documentArgs.pageHeight - (float)(topMargin + btmMargin)) / (float)(in_documentArgs.numrowsTable));
                // Set the tables Top, Bottom, Left and Right Borders to b.
                insertedTable.SetBorder(TableBorderType.Top, b);
                insertedTable.SetBorder(TableBorderType.Bottom, b);
                insertedTable.SetBorder(TableBorderType.Left, b);
                insertedTable.SetBorder(TableBorderType.Right, b);
                insertedTable.SetBorder(TableBorderType.InsideH, b);
                insertedTable.SetBorder(TableBorderType.InsideV, b);
                byte currentRow = 0; byte currentCol = 0;
                while ((currentRow < in_documentArgs.numrowsTable) && (currentCol < in_documentArgs.numcolsTable) && (theCurrentDate <= date2))
                {
                    Table internalTable1 = insertedTable.Rows[currentRow].Cells[currentCol].InsertTable(1, 2);
                    internalTable1.SetWidthsPercentage(new float[] { 30.0f, 70.0f }, 100.0f);

                    /*
                     *  Paragraph yearMonthP = insertedTable.Rows[currentRow].Cells[currentCol].InsertParagraph(String.Format("{0:yyyy, MMMM}", theCurrentDate));
                     *  Paragraph dayNumberP = insertedTable.Rows[currentRow].Cells[currentCol].InsertParagraph(String.Format("{0:dd}", theCurrentDate));
                     *  Paragraph weekdayP = insertedTable.Rows[currentRow].Cells[currentCol].InsertParagraph(String.Format("{0:dddd}", theCurrentDate));
                     */
                    Paragraph yearMonthP = internalTable1.Rows[0].Cells[1].InsertParagraph(String.Format("{0:yyyy, MMMM}", theCurrentDate));
                    Paragraph dayNumberP = internalTable1.Rows[0].Cells[1].InsertParagraph(String.Format("{0:dd}", theCurrentDate));
                    Paragraph weekdayP   = internalTable1.Rows[0].Cells[1].InsertParagraph(String.Format("{0:dddd}", theCurrentDate));
                    yearMonthP.Alignment = Alignment.center; yearMonthP.Font("Courier New");
                    dayNumberP.Alignment = Alignment.center; dayNumberP.Font("Courier New"); dayNumberP.FontSize(15); dayNumberP.Bold();
                    weekdayP.Alignment   = Alignment.center; weekdayP.Font("Courier New");

                    Tuple <string, string> sunTimes = getSunsetAndSunRise(true, 2, theCurrentDate, 49.4444, 32.0597);
                    Paragraph sunriseParagraph      = internalTable1.Rows[0].Cells[0].InsertParagraph(String.Format("{0}", sunTimes.Item1));
                    // https://xceed.com/wp-content/documentation/xceed-words-for-net/webframe.html#Xceed.Words.NET~Xceed.Words.NET.Paragraph~InsertPicture.html
                    sunriseParagraph.InsertPicture(picSunrise);
                    Paragraph sunsetParagraph = internalTable1.Rows[0].Cells[0].InsertParagraph(String.Format("{0}", sunTimes.Item2));
                    sunsetParagraph.InsertPicture(picSunset);
                    internalTable1.Rows[0].Cells[1].Paragraphs[0].Remove(false);
                    internalTable1.Rows[0].Cells[0].Paragraphs[0].Remove(false);
                    insertedTable.Rows[currentRow].Cells[currentCol].Paragraphs[0].Remove(false);
                    insertedTable.SetColumnWidth(currentCol, bestColumnWidth);
                    // 100*2/3pt -> 3.53 cm
                    // x  px -> bestRowHeightCm
                    if (importantDaysProcessed.ContainsKey(theCurrentDate))
                    {
                        List <importantday> eventsForCurrentDate = importantDaysProcessed[theCurrentDate];
                        bool holidayDisplayed = false;
                        foreach (importantday specialevent in eventsForCurrentDate)
                        {
                            Paragraph eventParagraph = insertedTable.Rows[currentRow].Cells[currentCol].InsertParagraph("\u25EF " + specialevent.description);
                            eventParagraph.Font("Courier New");
                            if ((specialevent.typeOfDay == "holiday") && (holidayDisplayed == false))
                            {
                                holidayDisplayed = true;
                                Paragraph officialWeekendParagraph = internalTable1.Rows[0].Cells[1].InsertParagraph("/вихідний/");
                                officialWeekendParagraph.Alignment = Alignment.center; officialWeekendParagraph.Font("Courier New"); officialWeekendParagraph.FontSize(7.0); officialWeekendParagraph.Bold();
                            }
                        }
                    }

                    insertedTable.Rows[currentRow].Height = Math.Round(bestRowHeightPt * 0.88);

                    theCurrentDate = theCurrentDate.AddDays(1.0);
                    currentCol++;
                    if ((currentCol >= in_documentArgs.numcolsTable) && (currentRow < in_documentArgs.numrowsTable - 1))
                    {
                        currentCol = 0; currentRow++;
                    }
                }

                if (theCurrentDate <= date2)
                {
                    //document.InsertSectionPageBreak();
                    insertedTable.InsertPageBreakAfterSelf();
                }
            } while (theCurrentDate <= date2);


            //document.InsertTable(in_documentArgs.numrowsTable, in_documentArgs.numcolsTable);
            document.Save();
            System.IO.File.Delete("sunsettmp.png");
            System.IO.File.Delete("sunrise.png");
        }
예제 #2
0
        //parse command line arguments http://www.ndesk.org/Options
        static void Main(string[] args)
        {
            System.DateTime?firstDate        = null;
            System.DateTime?secondDate       = null;
            String          templateFilePath = "";
            String          outputPath       = "";

            StatusStructure messageHandlingStatus = new StatusStructure {
                show_help = false, templateExpected = false, resultUnspecified = false
            };

            OptionSet p = new OptionSet()
                          .Add("d1|date1=", "First {DATE1} of span to generate calendar", delegate(string v) {
                if (v != null)
                {
                    firstDate = System.DateTime.Parse(v);
                }
            })
                          .Add("d2|date2=", "Second {DATE2} of span to generate calendar", delegate(string v) {
                if (v != null)
                {
                    secondDate = System.DateTime.Parse(v);
                }
            })
                          .Add("h|?|help", "Show help message", delegate(string v) { messageHandlingStatus.show_help = (v != null); })
                          .Add("t|template=", "{TEMPLATE} xml file containing list of important dates", delegate(string v) {
                if (v != null)
                {
                    templateFilePath = v;
                }
                else
                {
                    messageHandlingStatus.templateExpected = true;
                }
            })
                          .Add("o|output=", "{RESULT} file with calendar: docx", delegate(string v) {
                if (v != null)
                {
                    outputPath = v;
                }
                else
                {
                    messageHandlingStatus.resultUnspecified = true;
                    outputPath = String.Format("{0: yyyyMMdd_mmHHss}.docx", System.DateTime.Now);
                }
            });

            try {
                List <string> extra = p.Parse(args);
            }
            catch (OptionException e) {
                Console.WriteLine(e.Message);
                p.WriteOptionDescriptions(Console.Out);
            }

            if (messageHandlingStatus.show_help)   //user requested for help
            {
                DisplayHelpMessage(p);
                return;
            }
            else
            {
                if ((firstDate == null) || (secondDate == null))
                {
                    System.Console.WriteLine("Explicitly specify both dates");
                    p.WriteOptionDescriptions(Console.Out);
                    return;
                }
                else
                if (messageHandlingStatus.templateExpected == true)
                {
                    System.Console.WriteLine("Explicitly specify path to template");
                    p.WriteOptionDescriptions(Console.Out);
                    return;
                }
            }
            if (messageHandlingStatus.resultUnspecified)
            {
                System.Console.WriteLine("You have not specified path to result...");
            }
            System.Console.WriteLine("Writing to file {0}", outputPath);
            //==============Program. Main part.======================
            CodeHandling myHandl = new CodeHandling();
            GenericDocumentParameters genericDocumentParameters = new GenericDocumentParameters();

            myHandl.constructDocument(firstDate.GetValueOrDefault(), secondDate.GetValueOrDefault());
            myHandl.fromvariableCreateDocument(genericDocumentParameters, templateFilePath);
        }