将图像导出为 PDF

发布于 2024-12-27 16:09:48 字数 1266 浏览 2 评论 0原文

我有一个必须将图像导出为 PDF 的项目。图像和文本都需要导出为 pdf。有没有办法通过使用 silverPDF.dll 和 PdfReader 来做到这一点?

代码在这里。

 private void btnOutlook_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        XBrush xbrush;
        SaveFileDialog savePDF = new SaveFileDialog();
        savePDF.Filter = "PDF file format | *.pdf";
        if (savePDF.ShowDialog() == true)
        {
            PdfDocument document = new PdfDocument();
            PdfPage page = document.AddPage();
            XGraphics gfx = XGraphics.FromPdfPage(page);
            XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

            XFont font = new XFont("Huxtable", 20, XFontStyle.Bold, options);

            for (int x = 0; x < 10; x++)
            {
                if (x % 2 == 0)
                {
                    xbrush = XBrushes.Red;
                }
                else
                    xbrush = XBrushes.Black;
                gfx.DrawString(string.Format("{0}", stringArray[x]), font, xbrush, new XRect(0, (x * 20), page.Width, page.Height), XStringFormats.TopLeft);
            }

            document.Save(savePDF.OpenFile());
        }

    }

我可以在这段代码中的哪里插入将其插入到pdf中的图像?有什么办法吗?感谢您的所有回复。

I have a project that has to export images to PDF. Images and text alike are needed to be exported into pdf. is there a way to do this by using silverPDF.dll and PdfReader?

Code here.

 private void btnOutlook_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        XBrush xbrush;
        SaveFileDialog savePDF = new SaveFileDialog();
        savePDF.Filter = "PDF file format | *.pdf";
        if (savePDF.ShowDialog() == true)
        {
            PdfDocument document = new PdfDocument();
            PdfPage page = document.AddPage();
            XGraphics gfx = XGraphics.FromPdfPage(page);
            XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

            XFont font = new XFont("Huxtable", 20, XFontStyle.Bold, options);

            for (int x = 0; x < 10; x++)
            {
                if (x % 2 == 0)
                {
                    xbrush = XBrushes.Red;
                }
                else
                    xbrush = XBrushes.Black;
                gfx.DrawString(string.Format("{0}", stringArray[x]), font, xbrush, new XRect(0, (x * 20), page.Width, page.Height), XStringFormats.TopLeft);
            }

            document.Save(savePDF.OpenFile());
        }

    }

where in this code can I insert an Image that inserts it to pdf? Is there any way? Thanks for all replies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2025-01-03 16:09:48

必须是 SilverPDF 吗?正如我以前使用 iTextSharp 库在我以前的雇主那里做过类似的事情(否则我会粘贴示例代码)

iTextSharp-Working-with-images

下载链接

Does it need to be SilverPDF? As Iv'e done something similar before at my previous employer using the iTextSharp library (otherwise I would've pasted sample code)

iTextSharp-Working-with-images

Download Link

抽个烟儿 2025-01-03 16:09:48

Tesseract 4.1.1,在 C# 中将图像转换为 PDF 全文

这是一种从 jpg 图像构建具有全文图层的 PDF 的方法,只需使用 Visual Studio 2022 和 DotNet 6 内的 Nugget 包中的 Tesseract 4.1.1。

这是用于创建的函数来自多个 jpg 的 PDF。

  1. jpg 文件名的结构必须如下所示:IMG1.jpg、IMG2.jpg、IMG3.jpg...
  2. 所有图像文件都存储在同一文件夹内
private void PDFRenderer_OCR_MutipleJPG(String PDFName, String PDFTitle, String FolderJpg, int NumberImage)
  {
     IResultRenderer renderer = Tesseract.PdfResultRenderer.CreatePdfRenderer(PDFName, "tessdata", false);
     renderer.BeginDocument(PDFTitle);
     string configurationFilePath = "tessdata";
     string configfile = Path.Combine("tessdata", "pdf");
     //using (TesseractEngine engine = new TesseractEngine(configurationFilePath, "eng", EngineMode.TesseractAndLstm, configfile))
     EngineMode M1 = EngineMode.TesseractOnly;//election OCR mode
     TesseractEngine engine = new TesseractEngine("tessdata", "eng", M1);
     // progressBar1.Maximum= NumberImage;
     // progressBar1.Minimum = 1;   
          for (i =1 ; i < NumberImage+1;i++)//i=1, because my first image name is img1.jpg
            {
               //progressBar1.Value= i+1;
                lPage.Text = "Processing page: " + i.ToString();
                lPage.Refresh();       
                TesseractEngine engine = new TesseractEngine("tessdata", "eng", M1);
                string ImageJpg = "c:\\temp" + "\\" + FolderJpg + "\\" + FolderJpg + i.ToString() + ".jpg";
               // MessageBox.Show("IMG: " + ImageJpg);      
                using (var imagefile = new Bitmap(ImageJpg))
                 {
                    using (var img = PixConverter.ToPix(imagefile))
                    {
                        using (Tesseract.Page page = engine.Process(img, PDFTitle))
                        {
                            renderer.AddPage(page); // Adding converted page 
                        }
                    }                  
                }      
            }
            renderer.Dispose(); // Clear ressourcce
            //Reset progressBar1 to origin setting
            //progressBar1.Value= 1;
            //progressBar1.Maximum = 100;
            //progressBar1.Minimum= 0;
            //progressBar1.Value = 0;
  }

只需像这样调用此方法:

PDFRenderer_OCR_MutipleJPG("C:\temp\outp","tilte","Document", 20);

  • "C:\temp\outp" ->不带扩展名的最终 pdf 的名称和路径,它将是“C:\temp\outp.pdf”。

  • Title=“标题”PDF 元信息

  • 文档 ->是我的图像文件夹源和每个图像名称的一部分,没有索引。

  • 这里要转换的图像文件夹是:
    C:\temp\document\ ->里面:->文档1.jpg、文档2.jpg、文档3.jpg、...

  • 20 是索引,表示要转换的图像数量。

  • 可选的进度条,用于在处理过程中查看 UI 中的页码

最好的祝福
来自法国的弗朗西斯

Tesseract 4.1.1, converting Images to PDF Full Text in C#

Here is a way to build PDF with full text layer from jpg images, just using Tesseract 4.1.1 from Nugget package inside visual Studio 2022 and DotNet 6.

Here is Function for creating PDF from multiple jpg.

  1. The jpg files name must be structured like that: IMG1.jpg,IMG2.jpg,IMG3.jpg, ...
  2. All images file are stored inside the same folder

private void PDFRenderer_OCR_MutipleJPG(String PDFName, String PDFTitle, String FolderJpg, int NumberImage)
  {
     IResultRenderer renderer = Tesseract.PdfResultRenderer.CreatePdfRenderer(PDFName, "tessdata", false);
     renderer.BeginDocument(PDFTitle);
     string configurationFilePath = "tessdata";
     string configfile = Path.Combine("tessdata", "pdf");
     //using (TesseractEngine engine = new TesseractEngine(configurationFilePath, "eng", EngineMode.TesseractAndLstm, configfile))
     EngineMode M1 = EngineMode.TesseractOnly;//election OCR mode
     TesseractEngine engine = new TesseractEngine("tessdata", "eng", M1);
     // progressBar1.Maximum= NumberImage;
     // progressBar1.Minimum = 1;   
          for (i =1 ; i < NumberImage+1;i++)//i=1, because my first image name is img1.jpg
            {
               //progressBar1.Value= i+1;
                lPage.Text = "Processing page: " + i.ToString();
                lPage.Refresh();       
                TesseractEngine engine = new TesseractEngine("tessdata", "eng", M1);
                string ImageJpg = "c:\\temp" + "\\" + FolderJpg + "\\" + FolderJpg + i.ToString() + ".jpg";
               // MessageBox.Show("IMG: " + ImageJpg);      
                using (var imagefile = new Bitmap(ImageJpg))
                 {
                    using (var img = PixConverter.ToPix(imagefile))
                    {
                        using (Tesseract.Page page = engine.Process(img, PDFTitle))
                        {
                            renderer.AddPage(page); // Adding converted page 
                        }
                    }                  
                }      
            }
            renderer.Dispose(); // Clear ressourcce
            //Reset progressBar1 to origin setting
            //progressBar1.Value= 1;
            //progressBar1.Maximum = 100;
            //progressBar1.Minimum= 0;
            //progressBar1.Value = 0;
  }

Just call this method like this:

PDFRenderer_OCR_MutipleJPG("C:\temp\outp","tilte","Document", 20);

  • "C:\temp\outp" -> name and path for final pdf without extension,it will be "C:\temp\outp.pdf".

  • Title= "title" meta information on PDF

  • Document -> is my image folder source and part of each image name without index.

  • Here the Folder of images to convert is:
    C:\temp\document\ -> inside: -> Document1.jpg, Document2.jpg, Document3.jpg, ...

  • 20 is the index, represent the number of images to convert.

  • Optional progressBar for watching page number in UI during processing

Best Regards
Francis from France

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文