private void button1_Click(object sender, EventArgs e) { const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"; String Archivo = "D:\\EJEMPLO_ESCANEO_012"; //CommonDialogClass wiaDiag = new CommonDialogClass(); ICommonDialog wiaDiag = new WIA.CommonDialog(); WIA.ImageFile wiaImage = null; wiaImage = wiaDiag.ShowAcquireImage(WiaDeviceType.UnspecifiedDeviceType, WiaImageIntent.GrayscaleIntent, WiaImageBias.MaximizeQuality, wiaFormatJPEG, true, true, false); WIA.Vector vector = wiaImage.FileData; pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])vector.get_BinaryData())); Image i = Image.FromStream(new MemoryStream((byte[])vector.get_BinaryData())); i.Save(Archivo + ".TIFF"); PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument(); doc.Pages.Add(new PdfSharp.Pdf.PdfPage()); PdfSharp.Drawing.XGraphics xgr = PdfSharp.Drawing.XGraphics.FromPdfPage(doc.Pages[0]); PdfSharp.Drawing.XImage img = PdfSharp.Drawing.XImage.FromFile(Archivo + ".TIFF"); xgr.DrawImage(img, 0, 0); doc.Save(Archivo + ".PDF"); doc.Close(); }
public static byte[] AppendImageToPdf(byte[] pdf, byte[] img, Point position, double scale) { using (System.IO.MemoryStream msPdf = new System.IO.MemoryStream(pdf)) { using (System.IO.MemoryStream msImg = new System.IO.MemoryStream(img)) { System.Drawing.Image image = System.Drawing.Image.FromStream(msImg); PdfSharp.Pdf.PdfDocument document = PdfSharp.Pdf.IO.PdfReader.Open(msPdf); PdfSharp.Pdf.PdfPage page = document.Pages[0]; PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page); PdfSharp.Drawing.XImage ximg = PdfSharp.Drawing.XImage.FromGdiPlusImage(image); gfx.DrawImage( ximg, position.X, position.Y, ximg.Width * scale, ximg.Height * scale ); using (System.IO.MemoryStream msFinal = new System.IO.MemoryStream()) { document.Save(msFinal); return(msFinal.ToArray()); } } } }
private void AddImageToPdf(System.Drawing.Image image, double width_pt, double heigth_pt) { var rect = new System.Drawing.RectangleF { X = (float)_horizontalePosition, Y = (float)_verticalPosition, Width = (float)width_pt, Height = (float)heigth_pt }; PdfSharp.Drawing.XImage xImage = PdfSharp.Drawing.XImage.FromGdiPlusImage(image); PdfSharp.Drawing.XPen pen = new PdfSharp.Drawing.XPen(PdfSharp.Drawing.XColors.Black, 1f); _graphics.DrawImage(xImage, rect); _graphics.DrawRectangle(pen, rect); _verticalPosition += heigth_pt; _isPageEmpty = false; }
static void WriteTest() { using (PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument()) { document.Info.Title = "Family Tree"; document.Info.Author = "FamilyTree Ltd. - Stefan Steiger"; document.Info.Subject = "Family Tree"; document.Info.Keywords = "Family Tree, Genealogical Tree, Genealogy, Bloodline, Pedigree"; PdfSharp.Pdf.Security.PdfSecuritySettings securitySettings = document.SecuritySettings; // Setting one of the passwords automatically sets the security level to // PdfDocumentSecurityLevel.Encrypted128Bit. securitySettings.UserPassword = "******"; securitySettings.OwnerPassword = "******"; // Don't use 40 bit encryption unless needed for compatibility //securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit; // Restrict some rights. securitySettings.PermitAccessibilityExtractContent = false; securitySettings.PermitAnnotations = false; securitySettings.PermitAssembleDocument = false; securitySettings.PermitExtractContent = false; securitySettings.PermitFormsFill = true; securitySettings.PermitFullQualityPrint = false; securitySettings.PermitModifyDocument = true; securitySettings.PermitPrint = false; document.ViewerPreferences.Direction = PdfSharp.Pdf.PdfReadingDirection.LeftToRight; PdfSharp.Pdf.PdfPage page = document.AddPage(); // page.Width = PdfSettings.PaperFormatSettings.Width // page.Height = PdfSettings.PaperFormatSettings.Height page.Orientation = PdfSharp.PageOrientation.Landscape; double dblLineWidth = 1.0; string strHtmlColor = "#FF00FF"; PdfSharp.Drawing.XColor lineColor = XColorHelper.FromHtml(strHtmlColor); PdfSharp.Drawing.XPen pen = new PdfSharp.Drawing.XPen(lineColor, dblLineWidth); PdfSharp.Drawing.XFont font = new PdfSharp.Drawing.XFont("Arial" , 12.0, PdfSharp.Drawing.XFontStyle.Bold ); using (PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page)) { gfx.MUH = PdfSharp.Pdf.PdfFontEncoding.Unicode; PdfSharp.Drawing.Layout.XTextFormatter tf = new PdfSharp.Drawing.Layout.XTextFormatter(gfx); tf.Alignment = PdfSharp.Drawing.Layout.XParagraphAlignment.Left; PdfSharp.Drawing.Layout.XTextFormatterEx2 etf = new PdfSharp.Drawing.Layout.XTextFormatterEx2(gfx); gfx.DrawRectangle(pen, new PdfSharp.Drawing.XRect(100, 100, 100, 100)); using (PdfSharp.Drawing.XImage img = PdfSharp.Drawing.XImage.FromFile(@"D:\username\Documents\Visual Studio 2017\Projects\PdfSharp\TestApplication\Wikipedesketch1.png")) { gfx.DrawImage(img, 500, 500); } string text = "Lalala"; tf.DrawString(text , font , PdfSharp.Drawing.XBrushes.Black , new PdfSharp.Drawing.XRect(300, 300, 100, 100) , PdfSharp.Drawing.XStringFormats.TopLeft ); } // End Using gfx // Save the document... string filename = "TestFilePwProtected.pdf"; document.Save(filename); // ...and start a viewer. System.Diagnostics.Process.Start(filename); } // End Using document } // End Sub WriteTest