5

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.

Nathan
  • 1,520
  • 1
  • 12
  • 21
  • I don't know anything about SilverPDF but there are several tutorials online using other utility classes. [Here's](http://forums.asp.net/t/1348035.aspx/1) one. – M.Babcock Jan 16 '12 at 05:25

2 Answers2

1

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

Rohan Büchner
  • 5,333
  • 4
  • 62
  • 106
  • I can't use them, they are not built against Silverlight runtime. Huge problem for me. – Nathan Jan 18 '12 at 07:10
  • 1
    I am now using them. I just created the project in the .Web application, then passed it on bytes to the Silverlight application for saving. Thanks for the help! – Nathan Mar 22 '12 at 08:37
0

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