将图像导出为PDF

本文关键字:PDF 图像 | 更新日期: 2023-09-27 18:20:19

我有一个项目必须将图像导出为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的图像?有办法吗?感谢所有回复。

将图像导出为PDF

它需要是SilverPDF吗?正如Iv'e之前在我的前雇主使用iTextSharp库做过类似的事情一样(否则我会粘贴示例代码)

iTextSharp使用图像

下载链接

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;
  }

只需这样调用此方法:

PDFRender_OCR_MutipleJPG("C:''temp''outp","tilte","Document",20)

  • "C: ''temp''outp&quot-&gt;没有扩展名的最终pdf的名称和路径;C: ''temp''outp.pdf";。

  • 标题=";标题";PDF 上的元信息

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

  • 此处要转换的图像文件夹为:C: ''temp''document''->内部:->Document1.jpg、Document2.jpg、Document3.jpg。。。

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

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

致以最诚挚的问候Francis来自法国