parzival-au20 / sefa

0 stars 0 forks source link

test #42

Open parzival-au20 opened 8 months ago

parzival-au20 commented 8 months ago

private void CreateDocumentsWithWord(List newsEntities, string selectedFilePath) { List yurtDisiHaberler = newsEntities.Where(p => p.WebSiteName != "DefenceTurk" && p.WebSiteName != "DefenceTurkey" && p.WebSiteName != "SavunmaSanayist").ToList(); List yurtIciHaberler = newsEntities.Where(p => p.WebSiteName == "DefenceTurk" || p.WebSiteName == "DefenceTurkey" || p.WebSiteName == "SavunmaSanayist").ToList();

        Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();

        //Set animation status for word application  
        winword.ShowAnimation = false;

        //Set status for word application is to be visible or not.  
        winword.Visible = false;

        //Create a missing variable for missing value  
        object missing = System.Reflection.Missing.Value;

        string bitisDateText = newsEntities.OrderByDescending(p=>p.PublishDate).ToList().FirstOrDefault().PublishDate.ToString("dd.MM.yyyy");
        string baslangicDateText = newsEntities.OrderBy(p => p.PublishDate).ToList().FirstOrDefault().PublishDate.ToString("dd.MM.yyyy");

        int greenComponent = 32;
        int blueComponent = 96;
        System.Drawing.Color mixedColor = System.Drawing.Color.FromArgb(0, greenComponent, blueComponent);

        try
        {
            //Create a new document
            Microsoft.Office.Interop.Word.Document document = winword.Documents.Open(@"C:\Users\s20380\Desktop\MedyaTakipDocumentFormat_v2\belge.docx");
            document.PageSetup.LeftMargin = winword.InchesToPoints(0.5f);
            document.PageSetup.RightMargin = winword.InchesToPoints(0.5f);
            document.PageSetup.BottomMargin = winword.InchesToPoints(0.5f);

            Microsoft.Office.Interop.Word.Paragraph tarih = document.Content.Paragraphs.Add(ref missing);
            tarih.Range.Text = baslangicDateText + " - " + bitisDateText;
            tarih.Range.Font.Name = "Calibri Light";
            tarih.Range.Font.Color = (Microsoft.Office.Interop.Word.WdColor)ColorTranslator.ToOle(mixedColor);
            tarih.Range.Font.Size = 16;
            tarih.Range.Font.Bold = 2;
            tarih.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            tarih.Range.InsertParagraphAfter();
            tarih.Range.InsertParagraphAfter();
            tarih.Range.InsertParagraphAfter();

            document.PageSetup.TopMargin = winword.InchesToPoints(0);
            //document.PageSetup.BottomMargin = winword.InchesToPoints(0.5f);
            document.PageSetup.LeftMargin = winword.InchesToPoints(0.5f);
            document.PageSetup.RightMargin = winword.InchesToPoints(0.5f);

            if (yurtIciHaberler.Any())
            {
                //var newLine = document.Content.Paragraphs.Add(ref missing);
                //newLine.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newLine.Range.Text = System.Environment.NewLine;
                //newLine.Range.InsertParagraphAfter();
                //newLine.Range.InsertParagraphAfter();
                //newline added
                //newLine.Range.InsertParagraphAfter();

                Microsoft.Office.Interop.Word.Paragraph yurtIciKaynakliHaberlerTitle = document.Content.Paragraphs.Add(ref missing);
                yurtIciKaynakliHaberlerTitle.Range.Text = "YURT İÇİ HABERLER";
                yurtIciKaynakliHaberlerTitle.Range.Font.Name = "Calibri Light";
                yurtIciKaynakliHaberlerTitle.Range.Font.Color = (Microsoft.Office.Interop.Word.WdColor)ColorTranslator.ToOle(mixedColor);
                yurtIciKaynakliHaberlerTitle.Range.Font.Size = 16;
                yurtIciKaynakliHaberlerTitle.Range.Font.Bold = 2;
                yurtIciKaynakliHaberlerTitle.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;               
                yurtIciKaynakliHaberlerTitle.Range.InsertParagraphAfter();
                yurtIciKaynakliHaberlerTitle.Range.InsertParagraphAfter();
                yurtIciKaynakliHaberlerTitle.Range.InsertParagraphAfter();

                int sayfaNo = 1;
                for (int i = 0; i < yurtIciHaberler.Count; i++)
                {
                    /*var nl = document.Content.Paragraphs.Add(ref missing);
                    nl.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    nl.Range.InsertParagraphAfter();*/

                    Microsoft.Office.Interop.Word.Paragraph title = document.Content.Paragraphs.Add(ref missing);
                    title.Range.Text = yurtIciHaberler[i].Title;
                    title.Range.Font.Name = "Calibri Light";
                    title.Range.Font.Color = (Microsoft.Office.Interop.Word.WdColor)ColorTranslator.ToOle(mixedColor);
                    title.Range.Font.Size = 13;
                    title.Range.Font.Bold = 2;
                    title.Range.Font.Italic = 0;
                    title.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;                      
                    title.Range.InsertParagraphAfter();

                    Microsoft.Office.Interop.Word.Paragraph date = document.Content.Paragraphs.Add(ref missing);
                    date.Range.Text = yurtIciHaberler[i].PublishDate.ToString("dd.MM.yyyy");
                    date.Range.Font.Name = "Calibri Light";
                    date.Range.Font.Color = (Microsoft.Office.Interop.Word.WdColor)ColorTranslator.ToOle(mixedColor);
                    date.Range.Font.Size = 10;
                    date.Range.Font.Bold = 0;
                    date.Range.Font.Italic = 0;
                    date.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                    date.Range.InsertParagraphAfter();

                    Microsoft.Office.Interop.Word.Paragraph webSiteName = document.Content.Paragraphs.Add(ref missing);

                    webSiteName.Range.Text = yurtIciHaberler[i].WebSiteName.ToString();
                    webSiteName.Range.Font.Name = "Calibri Light";
                    webSiteName.Range.Font.Color = (Microsoft.Office.Interop.Word.WdColor)ColorTranslator.ToOle(mixedColor);
                    webSiteName.Range.Font.Size = 10;
                    webSiteName.Range.Font.Bold = 0;
                    webSiteName.Range.Font.Italic = 0;
                    webSiteName.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                    webSiteName.Range.InsertParagraphAfter();

                    if (yurtIciHaberler[i].Image != null)
                    {
                        try
                        {
                            System.Drawing.Image image = ByteArrayToImage(yurtIciHaberler[i].Image);
                            Clipboard.SetDataObject(image);
                            Microsoft.Office.Interop.Word.Paragraph newsImage = document.Content.Paragraphs.Add(ref missing);
                            newsImage.Range.Paste();
                            newsImage.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                            newsImage.Range.InsertParagraphAfter();
                            newsImage.Range.InsertParagraphAfter();
                        }
                        catch
                        {
                            var newsImage = document.Content.Paragraphs.Add(ref missing);
                            newsImage.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                            newsImage.Range.Text = System.Environment.NewLine;
                            newsImage.Range.InsertParagraphAfter();
                        }
                    }

                    Microsoft.Office.Interop.Word.Paragraph text = document.Content.Paragraphs.Add(ref missing);

                    string[] sentences = Regex.Split(yurtIciHaberler[i].Text, @"(?<=[\.!\?])\s+");

                    string newsContent = "";

                    foreach (var sentence in sentences)
                    {

                        if (newsContent.Length + sentence.Length <= 1500)
                        {
                            newsContent += sentence;
                        }

                        if (newsContent.Length >= 1000 && newsContent.Length <= 1500)
                        {

                            text.Range.Text = newsContent;
                            text.Range.Bold = 0;
                            text.Range.Italic = 0;
                            text.Range.Font.Size = 11;
                            text.Range.Font.Name = "Calibri Light";
                            text.Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;                      
                            text.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
                            text.Range.InsertParagraphAfter();

                            Microsoft.Office.Interop.Word.Paragraph devam = document.Content.Paragraphs.Add(ref missing);                        
                            devam.Range.InsertParagraphAfter();
                            Microsoft.Office.Interop.Word.Hyperlink link = document.Content.Hyperlinks.Add(devam.Range, yurtIciHaberler[i].Url, ref missing, ref missing, "Devamı için tıklayınız.");
                            link.Range.Font.Size = 12;
                            link.Range.InsertParagraphAfter();

                            break;
                        }
                    }
                    if (document.Content.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages) > sayfaNo)
                    {
                        pageNumber = document.Content.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages);
                        text.Range.Delete();
                        document.Content.Paragraphs[9].Range.Delete();//title.Range.Delete();
                        yurtIciHaberler[i].Title.Remove(9);
                        date.Range.Delete();
                        webSiteName.Range.Delete();

                        document.Words.Last.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
                        //text.Range.Text = newsContent;
                        sayfaNo++;
                    }

                    //Microsoft.Office.Interop.Word.Paragraph webSiteUrl = document.Content.Paragraphs.Add(ref missing);
                    //int index = !yurtIciHaberler[i].Url.Contains(".net") ? yurtIciHaberler[i].Url.IndexOf(".com") : yurtIciHaberler[i].Url.IndexOf(".net");
                    //webSiteUrl.Range.Text = yurtIciHaberler[i].Url.Substring(0, index + 4);
                    //webSiteUrl.Range.Font.Name = "Calibri Light";
                    //webSiteUrl.Range.Font.Size = 12;
                    //webSiteUrl.Range.Font.Italic = 0;
                    //webSiteUrl.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                    //webSiteUrl.Range.InsertParagraphAfter();

                    if (i%2 == 0)
                    {
                        //document.Words.Last.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
                    }
                }
            }

            if (yurtDisiHaberler.Any() && yurtIciHaberler.Any())
            {
                document.Words.Last.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
            }

            if (yurtDisiHaberler.Any())                  
            {
                Microsoft.Office.Interop.Word.Paragraph yurtDisiKaynakliHaberlerTitle = document.Content.Paragraphs.Add(ref missing);
                yurtDisiKaynakliHaberlerTitle.Range.Text = "YURT DIŞI HABERLER";
                yurtDisiKaynakliHaberlerTitle.Range.Font.Name = "Calibri Light";
                yurtDisiKaynakliHaberlerTitle.Range.Font.Color = (Microsoft.Office.Interop.Word.WdColor)ColorTranslator.ToOle(mixedColor);
                yurtDisiKaynakliHaberlerTitle.Range.Font.Size = 16;
                yurtDisiKaynakliHaberlerTitle.Range.Font.Italic = 0;
                yurtDisiKaynakliHaberlerTitle.Range.Font.Bold = 2;
                yurtDisiKaynakliHaberlerTitle.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                yurtDisiKaynakliHaberlerTitle.Range.InsertParagraphAfter();
                yurtDisiKaynakliHaberlerTitle.Range.InsertParagraphAfter();
                yurtDisiKaynakliHaberlerTitle.Range.InsertParagraphAfter();

                for (int i = 0; i < yurtDisiHaberler.Count; i++)
                {
                    if (i != 0)
                    {
                        var newL = document.Content.Paragraphs.Add(ref missing);
                        newL.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                        newL.Range.Text = System.Environment.NewLine;
                        newL.Range.InsertParagraphAfter();
                        newL.Range.InsertParagraphAfter();
                    }

                    if (yurtDisiHaberler[i].Title.Contains("Source:"))
                    {
                        CultureInfo cultureInfo = new CultureInfo("en-US");
                        string month = cultureInfo.DateTimeFormat.GetAbbreviatedMonthName(yurtDisiHaberler[i].PublishDate.Month).ToString();

                        string splitText = yurtDisiHaberler[i].PublishDate.Day.ToString() + "-" + month + "-" + yurtDisiHaberler[i].PublishDate.Year.ToString();

                        yurtDisiHaberler[i].Title = yurtDisiHaberler[i].Title.Substring(0, yurtDisiHaberler[i].Title.IndexOf(splitText)) + '"';
                    }

                    else
                    {
                        CultureInfo cultureInfo = new CultureInfo("en-US");
                        string month = cultureInfo.DateTimeFormat.GetMonthName(yurtDisiHaberler[i].PublishDate.Month).ToString();

                        string splitText = month + " " + yurtDisiHaberler[i].PublishDate.Day.ToString() + ", " + yurtDisiHaberler[i].PublishDate.Year.ToString();

                        if (yurtDisiHaberler[i].Title.Contains(splitText))
                        {
                            yurtDisiHaberler[i].Title = yurtDisiHaberler[i].Title.Substring(0, yurtDisiHaberler[i].Title.IndexOf(splitText)) + '"';
                        }

                    }

                    Microsoft.Office.Interop.Word.Paragraph title = document.Content.Paragraphs.Add(ref missing);
                    title.Range.Text = yurtDisiHaberler[i].Title;
                    title.Range.Font.Name = "Calibri Light";
                    title.Range.Font.Color = (Microsoft.Office.Interop.Word.WdColor)ColorTranslator.ToOle(mixedColor);
                    title.Range.Font.Size = 13;
                    title.Range.Font.Bold = 2;
                    title.Range.Font.Italic = 0;
                    title.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    title.Range.InsertParagraphAfter();

                    //var newLine1 = document.Content.Paragraphs.Add(ref missing);
                    //newLine1.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    //removed newline
                    //newLine1.Range.InsertParagraphAfter();

                    Microsoft.Office.Interop.Word.Paragraph date = document.Content.Paragraphs.Add(ref missing);
                    date.Range.Text = yurtDisiHaberler[i].PublishDate.ToString("dd.MM.yyyy");
                    date.Range.Font.Name = "Calibri Light";
                    date.Range.Font.Color = (Microsoft.Office.Interop.Word.WdColor)ColorTranslator.ToOle(mixedColor);
                    date.Range.Font.Size = 10;
                    date.Range.Font.Bold = 0;
                    date.Range.Font.Italic = 0;
                    date.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                    date.Range.InsertParagraphAfter();

                    Microsoft.Office.Interop.Word.Paragraph webSiteName = document.Content.Paragraphs.Add(ref missing);

                    webSiteName.Range.Text = yurtDisiHaberler[i].WebSiteName.ToString();
                    webSiteName.Range.Font.Name = "Calibri Light";
                    webSiteName.Range.Font.Size = 10;
                    webSiteName.Range.Font.Bold = 0;
                    webSiteName.Range.Font.Italic = 0;
                    webSiteName.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                    webSiteName.Range.InsertParagraphAfter();

                    if (yurtDisiHaberler[i].Image != null)
                    {
                        try
                        {
                            System.Drawing.Image image = ByteArrayToImage(yurtDisiHaberler[i].Image);
                            Clipboard.SetDataObject(image);
                            Microsoft.Office.Interop.Word.Paragraph newsImage = document.Content.Paragraphs.Add(ref missing);
                            newsImage.Range.Paste();
                            newsImage.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                            newsImage.Range.InsertParagraphAfter();
                            newsImage.Range.InsertParagraphAfter();
                        }
                        catch
                        {
                            var newsImage = document.Content.Paragraphs.Add(ref missing);
                            newsImage.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                            newsImage.Range.Text = System.Environment.NewLine;
                            newsImage.Range.InsertParagraphAfter();
                        }
                    }

                    string[] sentences = Regex.Split(yurtDisiHaberler[i].Text, @"(?<=[\.!\?])\s+");

                    string newsContent = "";

                    Microsoft.Office.Interop.Word.Paragraph text = document.Content.Paragraphs.Add(ref missing);

                    foreach (var sentence in sentences)
                    {
                        newsContent += sentence;
                        if (newsContent.Length >= 1000)
                        {
                            text.Range.Text = newsContent;
                            text.Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
                            text.Range.Font.Size = 11;
                            text.Range.Bold = 0;
                            text.Range.Italic = 0;
                            text.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
                            text.Range.InsertParagraphAfter();

                            Microsoft.Office.Interop.Word.Paragraph devam = document.Content.Paragraphs.Add(ref missing);                            
                            devam.Range.InsertParagraphAfter();
                            Microsoft.Office.Interop.Word.Hyperlink link = document.Content.Hyperlinks.Add(devam.Range, yurtDisiHaberler[i].Url, ref missing, ref missing, "Devamı için tıklayınız.");
                            link.Range.Font.Size = 12;
                            link.Range.InsertParagraphAfter();

                            break;
                        }

                        if (newsContent.Length < 1000 && sentence == sentences.LastOrDefault())
                        {
                            text.Range.Text = newsContent;
                            text.Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
                            text.Range.Font.Size = 11;
                            text.Range.Bold = 0;
                            text.Range.Italic = 0;
                            text.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
                            text.Range.InsertParagraphAfter();
                        }
                    }

                    //Microsoft.Office.Interop.Word.Paragraph webSiteUrl = document.Content.Paragraphs.Add(ref missing);
                    //webSiteUrl.Range.Font.Name = "Calibri Light";
                    //date.Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
                    //webSiteUrl.Range.Font.Size = 12;
                    //webSiteUrl.Range.Font.Italic = 0;
                    //webSiteUrl.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                    //webSiteUrl.Range.InsertParagraphAfter();
                    //int index = !yurtDisiHaberler[i].Url.Contains(".net") ? yurtDisiHaberler[i].Url.IndexOf(".com") : yurtDisiHaberler[i].Url.IndexOf(".net");
                    //Microsoft.Office.Interop.Word.Hyperlink webSiteUrlLink = document.Content.Hyperlinks.Add(webSiteUrl.Range, yurtDisiHaberler[i].Url, ref missing, ref missing, yurtDisiHaberler[i].Url.Substring(0, index + 4));

                    if (yurtDisiHaberler.Count - 1 != i)
                    {
                        document.Words.Last.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
                    }
                }
            }

            object filename = selectedFilePath;
            document.SaveAs2(ref filename);
            document.Close(ref missing, ref missing, ref missing);
            document = null;
            winword.Quit(ref missing, ref missing, ref missing);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(winword);
            winword = null;
        }

        catch (Exception ex)
        {
            winword.Quit(ref missing, ref missing, ref missing);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(winword);
            winword = null;
        }

    }
parzival-au20 commented 8 months ago

using System; using System.Collections.Generic; using Microsoft.Office.Interop.Word;

namespace MetinHesaplamaUygulamasi { class Program { static void Main(string[] args) { // Örnek metinler List paragraflar = new List { "Bu birinci paragraf.", "Bu ikinci paragraf.", "Bu üçüncü paragraf." };

        // Yazı tipi ve sayfa boyutunu tanımla
        float sayfaGenisligiInch = 8.5f; // Örnek: A4 kağıdı genişliği (inç cinsinden)
        float sayfaUzunluguInch = 11f; // Örnek: A4 kağıdı uzunluğu (inç cinsinden)
        float yazıTipiBoyutu = 12f; // Örnek: 12 punto
        float kenarBosluguInch = 1f; // Kenar boşluğu (inç cinsinden)

        // Maksimum metin uzunluğunu hesapla
        float maksimumMetinUzunlugu = ((sayfaGenisligiInch - kenarBosluguInch * 2) * (sayfaUzunluguInch - kenarBosluguInch * 2))
                                      / (yazıTipiBoyutu * 0.75f); // Yazı tipi boyutuna göre ortalama karakter uzunluğu hesaplanabilir

        // Toplam metin uzunluğunu hesapla
        float toplamMetinUzunlugu = 0;
        foreach (string paragraf in paragraflar)
        {
            toplamMetinUzunlugu += paragraf.Length;
        }

        // Kalan alanı hesapla
        float kalanAlan = maksimumMetinUzunlugu - toplamMetinUzunlugu;

        Console.WriteLine("Maksimum metin uzunluğu: " + maksimumMetinUzunlugu);
        Console.WriteLine("Toplam metin uzunluğu: " + toplamMetinUzunlugu);
        Console.WriteLine("Kalan alan: " + kalanAlan);
    }
}

}

parzival-au20 commented 8 months ago

System.Drawing.Image image = ByteArrayToImage(yurtDisiHaberler[i].Image); int desiredWidth = 300; // İstenen genişlik int desiredHeight = 200; // İstenen yükseklik image = ResizeImage(image, desiredWidth, desiredHeight); // ResizeImage fonksiyonunu kullandık

parzival-au20 commented 8 months ago

private System.Drawing.Image ResizeImage(System.Drawing.Image image, int width, int height) { // Yeni bir Bitmap oluştur System.Drawing.Image newImage = new System.Drawing.Bitmap(width, height);

// Graphics objesi oluştur ve yeni resmi oluşturulan Bitmap'e çiz
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(newImage))
{
    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    graphics.DrawImage(image, 0, 0, width, height);
}

return newImage;

}

parzival-au20 commented 8 months ago

error CS0051: Inconsistent accessibility: parameter type 'ListNewsEntity' is less accessible than method 'Form1.AddStyledParagraph(Document, ListNewsEntity, int, string, object, Color)' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

parzival-au20 commented 8 months ago

int PageNo = 1;

                    if (yurtIciHaberler.Any())
                    {
                        iTextSharp.text.Paragraph yurtIciHaberlerTitle = new iTextSharp.text.Paragraph(new Chunk("YURT ICI KAYNAKLI HABERLER", FontFactory.GetFont(FontFactory.TIMES_BOLD, 12, iTextSharp.text.Font.NORMAL)));
                        yurtIciHaberlerTitle.Alignment = Element.ALIGN_CENTER;
                        document.Add(yurtIciHaberlerTitle);
                        for (int y = 0; y < yurtIciHaberler.Count; y++)
                        {
                            List<Paragraph> addedParagraphs = new List<Paragraph>();

                            var anchor = new Chunk("Devami için tiklayiniz.")
                            {
                                Font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLUE)
                            };
                            string[] sentences = Regex.Split(yurtIciHaberler[y].Text, @"(?<=[\.!\?])\s+");

                            foreach (var sentence in sentences)
                            {
                                newsContent += sentence.Replace('ş', 's').Replace('ü', 'u').Replace('ı', 'i').Replace('ö', 'o').Replace('ğ', 'g').Replace('ç', 'c').Replace('Ü', 'U')
                                    .Replace('Ş', 'S').Replace('İ', 'I').Replace('Ğ', 'G').Replace('Ö', 'O');
                                if (newsContent.Length >= 1000)
                                {
                                    anchor.SetAnchor(yurtIciHaberler[y].Url);
                                    break;
                                }
                            }
                            iTextSharp.text.Paragraph paragraph = new iTextSharp.text.Paragraph(new Chunk(newsContent, FontFactory.GetFont(FontFactory.TIMES_ROMAN, 11, iTextSharp.text.Font.NORMAL)));
                            if (newsContent.Length >= 1000)
                            {
                                paragraph.Add("\n");
                                paragraph.Add(anchor);
                            }

                            if (yurtIciHaberler[y].Title.Contains("Haber Merkezi"))
                            {
                                yurtIciHaberler[y].Title = yurtIciHaberler[y].Title.Substring(0, yurtIciHaberler[y].Title.IndexOf("Haber Merkezi"));
                            }
                            yurtIciHaberler[y].Title = yurtIciHaberler[y].Title.Replace('ş', 's').Replace('ü', 'u').Replace('ı', 'i').Replace('ö', 'o').Replace('ğ', 'g').Replace('ç', 'c').Replace('Ü', 'U')
                                    .Replace('Ş', 'S').Replace('İ', 'I').Replace('Ğ', 'G').Replace('Ö', 'O');
                            iTextSharp.text.Paragraph title = new iTextSharp.text.Paragraph(new Chunk(yurtIciHaberler[y].Title, FontFactory.GetFont(FontFactory.TIMES_BOLD, 16, iTextSharp.text.Font.NORMAL)));
                            iTextSharp.text.Paragraph date = new iTextSharp.text.Paragraph(new Chunk(yurtIciHaberler[y].PublishDate.ToString("dd.MM.yyyy"), FontFactory.GetFont(FontFactory.TIMES_ITALIC, 11, iTextSharp.text.Font.NORMAL)));
                            iTextSharp.text.Paragraph newsSource = new iTextSharp.text.Paragraph(new Chunk(yurtIciHaberler[y].WebSiteName, FontFactory.GetFont(FontFactory.TIMES_ROMAN, 11, iTextSharp.text.Font.NORMAL)));
                            int index = !yurtIciHaberler[y].Url.Contains(".net") ? yurtIciHaberler[y].Url.IndexOf(".com") : yurtIciHaberler[y].Url.IndexOf(".net");
                            iTextSharp.text.Paragraph newsUrl = new iTextSharp.text.Paragraph(new Chunk(yurtIciHaberler[y].Url.Substring(0, index + 4), FontFactory.GetFont(FontFactory.TIMES_ITALIC, 11, iTextSharp.text.Font.NORMAL)));

                            paragraph.Alignment = Element.ALIGN_JUSTIFIED;
                            title.Alignment = Element.ALIGN_CENTER;
                            date.Alignment = Element.ALIGN_RIGHT;
                            newsSource.Alignment = Element.ALIGN_RIGHT;
                            newsUrl.Alignment = Element.ALIGN_LEFT;

                            addedParagraphs.Add(new Chunk("\n\n\n"));
                            addedParagraphs.Add(title);
                            addedParagraphs.Add(new Chunk("\n"));
                            addedParagraphs.Add(date);
                            addedParagraphs.Add(newsSource);
                            addedParagraphs.Add(new Chunk("\n"));

                            if (yurtIciHaberler[y].Image != null)
                            {
                                try
                                {
                                    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(yurtIciHaberler[y].Image);
                                    img.ScaleToFit(250f, 250f);
                                    img.Alignment = iTextSharp.text.Image.TEXTWRAP | iTextSharp.text.Image.ALIGN_CENTER;
                                    img.IndentationLeft = 9f;
                                    img.SpacingAfter = 9f;

                                    addedParagraphs.Add(img);
                                    addedParagraphs.Add(new Chunk("\n"));
                                }
                                catch
                                {
                                    addedParagraphs.Add(new Chunk("\n"));
                                }
                            }

                            addedParagraphs.Add(paragraph);
                            addedParagraphs.Add(new Chunk("\n"));
                            addedParagraphs.Add(newsUrl);

                            foreach (var item in addedParagraphs){
                                document.Add(item);
                            }

                            if(writer.PageNumber > PageNo){
                                document.rem
                                document.NewPage();
                                PageNo++;
                            }

                            newsContent = "";

                            //document.NewPage();
                        }

                    }
parzival-au20 commented 8 months ago

iText represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high- and low-level programming capabilities and the ability to create, edit and enhance PDF documents, the iText PDF library can be a boon to nearly every workflow. iText allows you to build custom PDF scenarios for web, mobile, desktop or cloud apps in .NET.

iText was built on nearly a decade of lessons learned from iText 5 (iTextSharp) development. It is a simpler, more performant and extensible library that is ready to handle the increased challenges of today's document workflows, one add-on at a time.

The iText Suite consists of iText Core and several add-ons. The add-ons are accessible as different packages.

What you can do with iText: Generate PDFs:

Mass generation of PDFs, including tagged PDFs which contain metadata to describe the document structure and the order of its elements (e.g. titles, text blocks, columns and pictures) Convert images to PDF Convert HTML to PDF (with the pdfHTML iText add-on) iText is unique in its breadth of language support, including Indic languages, Thai, Khmer, Arabi, Hebrew, Chinese, Japanese, Korean, Cyrillic languages and many more, in combination of (with the pdfCalligraph iText add-on) Edit and manipulate PDFs:

Split or merge PDFs, delete pages from a PDF Rotate a PDF or specific pages Add passwords and PDF permission options to a PDF, or remove password protection from a PDF Update/add content, PDF objects [dictionaries etc.], watermarks, bookmarks… Remove sensitive data, Regex based redaction (PDF redaction) (with the pdfSweep iText add-on) Create and modify annotations Programmatically fill out PDF forms (AcroForm and XFA) Flatten AcroForms Read XFA Flatten XFA (with the pdfXFA iText add-on)

parzival-au20 commented 8 months ago

'Paragraph' is an ambiguous reference between 'Microsoft.Office.Interop.Word.Paragraph' and 'iTextSharp.text.Paragraph'

parzival-au20 commented 8 months ago

: error CS0104: 'Paragraphs' is an ambiguous reference between 'Aspose.Pdf.Paragraphs' and 'Microsoft.Office.Interop.Word.Paragraphs' 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(1800,54,1800,76): error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List' to 'iTextSharp.text.Paragraph'

parzival-au20 commented 8 months ago

public class News { public List iTextSharp.text.Paragraph Paragraphs { get; set; }

        public News()
        {
          iTextSharp.text.Paragraph Paragraphs = new List<Paragraphs>();
        }
    }
parzival-au20 commented 8 months ago

The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2115,9,2115,16): error CS0106: The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2132,9,2132,16): error CS0106: The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2144,9,2144,16): error CS0106: The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2149,9,2149,16): error CS0106: The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2154,9,2154,16): error CS0106: The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2159,9,2159,16): error CS0106: The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2180,12,2180,19): error CS0106: The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2185,9,2185,16): error CS0106: The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2194,9,2194,16): error CS0106: The modifier 'private' is not valid for this item 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2194,17,2194,23): error CS8370: Feature 'static local functions' is not available in C# 7.3. Please use language version 8.0 or greater. 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2194,24,2194,30): error CS8370: Feature 'extern local functions' is not available in C# 7.3. Please use language version 9.0 or greater. 1>C:\Users\s20128\Desktop\MedyaTakip2 - 2.0 - Prod-To\MedyaTakip2\Form1.cs(2204,2,2204,2): error CS1513: } expected

parzival-au20 commented 8 months ago

public void AddNews(List newsList, iTextSharp.text.Document document) { foreach (News news in newsList) { // Haber boyutunu kontrol ederek sığdırabildiği kadar haber ekle float remainingSpace = document.PageSize.Height - document.TopMargin - document.BottomMargin - document.Bottom; foreach (Paragraph paragraph in news.Paragraphs) { float paragraphHeight = paragraph.CalculateHeights();

                // Eğer paragraf mevcut sayfaya sığmazsa yeni bir sayfa ekle
                if (paragraphHeight > remainingSpace)
                {
                    document.NewPage();
                    remainingSpace = document.PageSize.Height - document.TopMargin - document.BottomMargin - document.Bottom;
                }

                // Paragrafı sayfaya ekle
                document.Add(paragraph);
                remainingSpace -= paragraphHeight;
            }
        }
parzival-au20 commented 8 months ago

Paragraph' does not contain a definition for 'CalculateHeights' and no accessible extension method 'CalculateHeights' accepting a first argument of type 'Paragraph' could be found (are you missing a using directive or an assembly reference?)

parzival-au20 commented 8 months ago

public float CalculateParagraphHeight(Paragraph paragraph, PdfWriter writer) { // PdfContentByte nesnesi oluşturun PdfContentByte canvas = writer.DirectContent;

// ColumnText nesnesi oluşturun
ColumnText column = new ColumnText(canvas);
column.AddElement(paragraph);
column.SetSimpleColumn(0, 0, 300, 800); // Genişlik ve yükseklik ayarlarını dilediğiniz gibi ayarlayabilirsiniz
column.Go();

// Yüksekliği hesaplayın
return column.YLine;

}

parzival-au20 commented 8 months ago

public void AddNews(List newsList, iTextSharp.text.Document document, PdfWriter writer) { foreach (News news in newsList) { // Haber boyutunu kontrol ederek sığdırabildiği kadar haber ekle float remainingSpace = document.PageSize.Height - document.TopMargin - document.BottomMargin - document.Bottom; iTextSharp.text.Paragraph newsParagraph = new iTextSharp.text.Paragraph(); foreach (iTextSharp.text.Paragraph paragraph in news.Paragraphs) { newsParagraph.Add(paragraph); } float paragraphHeight = CalculateParagraphHeight(newsParagraph, writer);

            // Eğer paragraf mevcut sayfaya sığmazsa yeni bir sayfa ekle
            if (paragraphHeight > remainingSpace)
            {
                document.NewPage();
                remainingSpace = document.PageSize.Height - document.TopMargin - document.BottomMargin - document.Bottom;
            }

            // Paragrafı sayfaya ekle
            document.Add(newsParagraph);
            remainingSpace -= paragraphHeight;
        }
    }
parzival-au20 commented 8 months ago

iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 40, 40, 0, 0);

parzival-au20 commented 8 months ago

public float CalculateParagraphHeight(iTextSharp.text.Paragraph paragraph, PdfWriter writer, iTextSharp.text.Document document) { // PdfContentByte nesnesi oluşturun PdfContentByte canvas = writer.DirectContent;

        // ColumnText nesnesi oluşturun
        ColumnText column = new ColumnText(canvas);
        column.AddElement(paragraph);
        column.SetSimpleColumn(40, 0, 555, 842); // Genişlik ve yükseklik ayarlarını dilediğiniz gibi ayarlayabilirsiniz
        column.Go();
        document.NewPage();
        // Yüksekliği hesaplayın
        float lineHeight = column.YLine;
        //canvas.Reset();

        return lineHeight; 

    }
    Bunun paragraf boyutunu doğru hesapladığına emin misin