🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# RMOS / devexpress ile docx veya doc to pdf convert çevirme kullanımı word to pdf

1-) C# RMOS - devexpress ile docx veya doc to pdf convert çevirme kullanımı word to pdf

 

 

1- 1.yol word docx doc dan pdf e çevirir

 

 

 private void button1_Click(object sender, EventArgs e)

 {

     string docxPath = @"C:\cc\dikey.docx";

     string pdfPath = @"C:\cc\dikey_sonuc.pdf";

     bool result = ConvertDocxToPdf(docxPath, pdfPath);

 

 }

 private bool ConvertDocxToPdf(string docxPath, string pdfPath)

 {

     if (!File.Exists(docxPath))

     {

         MessageBox.Show("DOCX dosyası bulunamadı.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);

         return false;

     }

 

     using (RichEditDocumentServer documentServer = new RichEditDocumentServer())

     {

         documentServer.LoadDocument(docxPath);

         documentServer.ExportToPdf(pdfPath);

         return File.Exists(pdfPath);

     }

 }

 

 

 

2- 2.yol word docx doc dan pdf e çevirir

 

 

using DevExpress.XtraPrinting;

using DevExpress.XtraReports.UI;

using DevExpress.XtraRichEdit;

using KaliteApi.ReportPk;

using System;

using System.Diagnostics;

using System.IO;

using System.Windows.Forms;

namespace Deneme_1

{

    public partial class Form4 : Form

    {

        public Form4()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            string docxPath = @"C:\cc\55.docx";

            string outPath = @"C:\cc\Document_PDF22.pdf";

            using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())

            {

                wordProcessor.LoadDocument(docxPath);

                PdfExportOptions options = new PdfExportOptions();

                options.DocumentOptions.Author = "Ramazan HABER";

                options.Compressed = false;

                options.ImageQuality = PdfJpegImageQuality.Highest;

                using (FileStream pdfFileStream = new FileStream(outPath, FileMode.Create))

                {

                    wordProcessor.ExportToPdf(pdfFileStream, options);

                }

            }

            Process.Start("explorer.exe", outPath);

        }

    }

}

 

 

 

2- docx to Xtrareport to pdf sayfa sayfa

 

using DevExpress.Pdf;

using DevExpress.XtraRichEdit;

using DevExpress.XtraRichEdit.API.Layout;

using DevExpress.XtraRichEdit.API.Native;

using KaliteApi.ReportPk;

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.IO;

using System.Windows.Forms;

namespace Deneme_1

{

    public partial class Form4 : Form

    {

        public Form4()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            string docxPath = @"C:\cc\55.docx";

            string outputPath = @"C:\cc\Document_PDF22_yeni.pdf";

            using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())

            {

                wordProcessor.LoadDocument(docxPath);

                List<byte[]> pdfFiles = new List<byte[]>();

                for (int pageNumber = 0; pageNumber < wordProcessor.DocumentLayout.GetPageCount(); pageNumber++)

                {

                    string pageRtfText = GetPageRtfText(wordProcessor, pageNumber);

                    byte[] pdfBytes = GeneratePdfFromRtf(pageRtfText);

                    pdfFiles.Add(pdfBytes);

                }

                MergePdfFiles(pdfFiles, outputPath);

            }

            Process.Start("explorer.exe", outputPath);

            MessageBox.Show("işlem bitti");

        }

        private string GetPageRtfText(RichEditDocumentServer documentServer, int pageNumber)

        {

            Document document = documentServer.Document;

            LayoutPage page = documentServer.DocumentLayout.GetPage(pageNumber);

            if (page != null)

            {

                LayoutPageArea pageArea = page.PageAreas[0];

                FixedRange pageRange = pageArea.Range;

                DocumentRange range = document.CreateRange(pageRange.Start, pageRange.Length);

                return document.GetRtfText(range);

            }

            return string.Empty;

        }

        private byte[] GeneratePdfFromRtf(string rtfText)

        {

            using (MemoryStream pdfStream = new MemoryStream())

            {

                Report1 report = new Report1();

                report.xrRichText1.Rtf = rtfText;

                report.ExportToPdf(pdfStream);

                return pdfStream.ToArray();

            }

        }

        private void MergePdfFiles(List<byte[]> pdfFiles, string outputPath)

        {

            using (PdfDocumentProcessor pdfProcessor = new PdfDocumentProcessor())

            {

                pdfProcessor.CreateEmptyDocument();

                foreach (byte[] pdfBytes in pdfFiles)

                {

                    using (MemoryStream pdfStream = new MemoryStream(pdfBytes))

                    {

                        pdfProcessor.AppendDocument(pdfStream);

                    }

                }

                pdfProcessor.SaveDocument(outputPath);

            }

        }

    }

}

 

 

 

 

 

 

 

 

 

 

FileContentResult api tarafında dönmek için

 

var fileName = dosyam.orjiAd.Replace(dosyam.uzanti, "") + ".pdf";

var contentType = "application/pdf";

 

var memo = MergePdfFiles(pdfFiles);

 

var content = File(memo.ToArray(), contentType, fileName);

 

if (mobile == 1)

{

    mobilDropboxLinkKaydet(id, content);

}

 

return content;

 

 

private MemoryStream MergePdfFiles(List<byte[]> pdfFiles)

{

    MemoryStream outputStream = new MemoryStream();

 

    using (PdfDocumentProcessor pdfProcessor = new PdfDocumentProcessor())

    {

        pdfProcessor.CreateEmptyDocument(outputStream);

 

        foreach (byte[] pdfBytes in pdfFiles)

        {

            using (MemoryStream pdfStream = new MemoryStream(pdfBytes))

            {

                pdfProcessor.AppendDocument(pdfStream);

            }

        }

    }

 

    return outputStream;

}

 

 2025 Ocak 27 Pazartesi
 278