예제 #1
0
        private void InsertPicture(Paragraph paragraphToInsert, ObservableCollection <Picture> pictures)
        {
            Table picTable = paragraphToInsert.InsertTableAfterSelf(1, 2);

            picTable.AutoFit   = AutoFit.Window;
            picTable.Alignment = Alignment.left;

            foreach (var p in pictures)
            {
                p.CreatePicture();
            }

            var i = 0;

            while (i < pictures.Count)
            {
                Xceed.Words.NET.Image   img = _templateDocx.AddImage(pictures[i].FullPath);
                Xceed.Words.NET.Picture p   = img.CreatePicture();

                //非手機圖
                if (!IsCellPhoneScreenshot(pictures[i]))
                {
                    var row = picTable.InsertRow();
                    row.MergeCells(0, 1);

                    var row2 = picTable.InsertRow();
                    row2.MergeCells(0, 1);

                    //按照比例縮小圖片,如圖片不足600寬,則保留原大小
                    ResizePicture(pictures[i], 600, out var width, out var height);
                    p.Width  = width;
                    p.Height = height;

                    //新增table放圖片
                    row.Cells[0].Paragraphs.First().Append(pictures[i].Caption).Font(Font);
                    row2.Cells[0].Paragraphs.First().InsertPicture(p);

                    if (i == pictures.Count - 1)
                    {
                        break;
                    }

                    i++;
                }

                //第一張手機圖
                else
                {
                    //最後一張圖
                    if (i == pictures.Count - 1)
                    {
                        var row = picTable.InsertRow();
                        row.MergeCells(0, 1);

                        var row2 = picTable.InsertRow();
                        row2.MergeCells(0, 1);

                        //按照比例縮小圖片,如圖片不足200寬,則保留原大小
                        ResizePicture(pictures[i], 200, out var width, out var height);
                        p.Width  = width;
                        p.Height = height;

                        //新增table放圖片
                        row.Cells[0].Paragraphs.First().Append(pictures[i].Caption).Font(Font);
                        row2.Cells[0].Paragraphs.First().InsertPicture(p);

                        break;
                    }
                    //下一張不是手機圖
                    if (!IsCellPhoneScreenshot(pictures[i + 1]))
                    {
                        var row = picTable.InsertRow();
                        row.MergeCells(0, 1);

                        var row2 = picTable.InsertRow();
                        row2.MergeCells(0, 1);

                        //按照比例縮小圖片,如圖片不足200寬,則保留原大小
                        ResizePicture(pictures[i], 200, out var width, out var height);
                        p.Width  = width;
                        p.Height = height;

                        //新增table放圖片
                        row2.Cells[0].Paragraphs.First().InsertPicture(p);
                        row.Cells[0].Paragraphs.First().Append(pictures[i].Caption).Font(Font);

                        i++;
                    }

                    //下一張也是手機圖
                    else
                    {
                        Xceed.Words.NET.Image   img2 = _templateDocx.AddImage(pictures[i + 1].FullPath);
                        Xceed.Words.NET.Picture p2   = img2.CreatePicture();

                        var row  = picTable.InsertRow();
                        var row2 = picTable.InsertRow();


                        //按照比例縮小圖片,如圖片不足200寬,則保留原大小
                        ResizePicture(pictures[i], 200, out var width, out var height);
                        p.Width  = width;
                        p.Height = height;

                        ResizePicture(pictures[i + 1], 200, out var width2, out var height2);
                        p2.Width  = width2;
                        p2.Height = height2;

                        //新增table放圖片
                        row.Cells[0].Paragraphs.First().Append(pictures[i].Caption).Font(Font);
                        row2.Cells[0].Paragraphs.First().InsertPicture(p);

                        row.Cells[1].Paragraphs.First().Append(pictures[i + 1].Caption).Font(Font);
                        row2.Cells[1].Paragraphs.First().InsertPicture(p2);

                        i += 2;
                    }
                }
            }
            picTable.RemoveRow(0);
            picTable.InsertParagraphAfterSelf("\r\n");

            foreach (var p in pictures)
            {
                p.ReleasePictureResource();
            }
        }
예제 #2
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");
        }