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 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 void Render(ref PdfSharp.Drawing.XGraphics gfx) { foreach (PrintableComponent pc in Children) { // voor scaling/offset later pc.Render(ref gfx, 0.0, 0.0, 1.0, 1.0); } }
public void Draw(PdfSharp.Drawing.XGraphics g2, double x, double y) { if (g2 == null) { throw new ArgumentException("Null Graphics2D passed to TextLayout.draw()"); } PdfSharp.Drawing.XFont xfont = new PdfSharp.Drawing.XFont(GetFont(), new PdfSharp.Drawing.XPdfFontOptions(PdfSharp.Pdf.PdfFontEncoding.Unicode, PdfSharp.Pdf.PdfFontEmbedding.Always)); g2.DrawString(trimText, new PdfSharp.Drawing.XFont(GetFont(), new PdfSharp.Drawing.XPdfFontOptions(PdfSharp.Pdf.PdfFontEncoding.Unicode, PdfSharp.Pdf.PdfFontEmbedding.Always)), new PdfSharp.Drawing.XSolidBrush(PdfSharp.Drawing.XColor.FromArgb(stringColor)), x, y); }
public override void Render(ref PdfSharp.Drawing.XGraphics gfx, double offsetLeft, double offsetTop, double scaleX, double scaleY) { leftTextComponent.SetData(LeftText); leftTextComponent.Render(ref gfx, offsetLeft, offsetTop, scaleX, scaleY); rightTextComponent.SetData(RightText); rightTextComponent.Render(ref gfx, offsetLeft, offsetTop, scaleX, scaleY); base.Render(ref gfx, offsetLeft, offsetTop, scaleX, scaleY); }
} // paint public void PaintPdf(PdfSharp.Drawing.XGraphics g2D, Rectangle bounds, bool isView) { Point pageStart = new Point(bounds.X, bounds.Y); GetElements(); for (int i = 0; i < m_pe.Length; i++) { m_pe[i].PaintPdf(g2D, 0, pageStart, m_ctx, isView); } } // paint
public static void DrawCrosshairs(PdfSharp.Drawing.XGraphics gfx, double width, double height, PdfMargin margin) { PdfSharp.Drawing.XPen[] pens = new PdfSharp.Drawing.XPen[] { PdfSharp.Drawing.XPens.Black, PdfSharp.Drawing.XPens.Black }; // pens = new PdfSharp.Drawing.XPen[] { PdfSharp.Drawing.XPens.Yellow, PdfSharp.Drawing.XPens.Red }; double halfPenWidth = pens[0].Width / 2.0; DrawCrossTopLeft(gfx, width, height, margin, halfPenWidth, pens); DrawCrossTopRight(gfx, width, height, margin, halfPenWidth, pens); DrawCrossBottomLeft(gfx, width, height, margin, halfPenWidth, pens); DrawCrossBottomRight(gfx, width, height, margin, halfPenWidth, pens); }
public static void DrawCrossTopLeft(PdfSharp.Drawing.XGraphics gfx, double width, double height, PdfMargin margin, double halfPenWidth, PdfSharp.Drawing.XPen[] pens) { double x1 = 0; double y1 = margin.Top - halfPenWidth; double x2 = margin.Left; double y2 = margin.Top - halfPenWidth; gfx.DrawLine(pens[0], x1, y1, x2, y2); // Horizontal - Yellow x1 = margin.Left - halfPenWidth; y1 = 0; x2 = margin.Left - halfPenWidth; y2 = margin.Top; gfx.DrawLine(pens[1], x1, y1, x2, y2); // Vertical - Red }
public static void DrawCrossBottomRight(PdfSharp.Drawing.XGraphics gfx, double width, double height, PdfMargin margin, double halfPenWidth, PdfSharp.Drawing.XPen[] pens) { double x1 = width - margin.Right; double y1 = height - margin.Bottom + halfPenWidth; double x2 = width; double y2 = height - margin.Bottom + halfPenWidth; gfx.DrawLine(pens[0], x1, y1, x2, y2); // Horizontal - Yellow x1 = width - margin.Right + halfPenWidth; y1 = height - margin.Bottom; x2 = width - margin.Right + halfPenWidth; y2 = height; gfx.DrawLine(pens[1], x1, y1, x2, y2); // Vertical - red }
/// <summary>Add a new page if the current page is not empty</summary> public void addPage() { if (!_isPageEmpty) { if (_pageCount != 0) { addPagenumber(); } _page = new PdfSharp.Pdf.PdfPage(); if (_pageCount == 0) { _size.Width = _page.Width; _size.Height = _page.Height; _area = new PdfSharp.Drawing.XRect { X = PdfSharp.Drawing.XUnit.FromMillimeter(10), Y = PdfSharp.Drawing.XUnit.FromMillimeter(10), Width = _page.Width - PdfSharp.Drawing.XUnit.FromMillimeter(20), Height = _page.Height - PdfSharp.Drawing.XUnit.FromMillimeter(45) }; } else { _page = new PdfSharp.Pdf.PdfPage(); _page.Width = _size.Width; _page.Height = _size.Height; } _horizontalePosition = _area.X; _verticalPosition = _area.Y; _doc.AddPage(_page); if (_graphics != null) { _graphics.Dispose(); } _graphics = PdfSharp.Drawing.XGraphics.FromPdfPage(_page); _textformater = new PdfSharp.Drawing.Layout.XTextFormatter(_graphics); _pageCount++; _isPageEmpty = true; } }
} // paint public override void PaintPdf(PdfSharp.Drawing.XGraphics g2D, int pageNo, PointF pageStart, Ctx ctx, bool isView) { if (!m_valid || m_barcode == null) { return; } // Position PointF location = GetAbsoluteLocation(pageStart); int x = (int)location.X; if (MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight.Equals(p_FieldAlignmentType)) { x += (int)p_maxWidth - (int)p_width; } else if (MPrintFormatItem.FIELDALIGNMENTTYPE_Center.Equals(p_FieldAlignmentType)) { x += ((int)p_maxWidth - (int)p_width) / 2; } int y = (int)location.Y; //m_barcode.draw(g2D, x, y); } // paint
public void InitPdf() { // Create new PDF document this.Pdf = new PdfSharp.Pdf.PdfDocument(); this.Pdf.Info.CreationDate = System.DateTime.Now.AddYears(-400); this.Pdf.Info.ModificationDate = new System.DateTime(System.DateTime.UtcNow.Ticks); // Throws exception on UTC time this.Pdf.Info.Title = "PDFsharp SVG"; this.Pdf.Info.Author = "Stefan Steiger"; this.Pdf.Info.Subject = "SVG"; System.Console.WriteLine(this.Pdf.Info.Producer); // Create new page PdfSharp.Pdf.PdfPage page = this.Pdf.AddPage(); // Dimension page.Width = PdfSharp.Drawing.XUnit.FromMillimeter(1000); page.Height = PdfSharp.Drawing.XUnit.FromMillimeter(1000); this.gfxPdf = PdfSharp.Drawing.XGraphics.FromPdfPage(page); }
public static void DrawBorder(PdfSharp.Drawing.XGraphics gfx, double width, double height, PdfMargin margin) { PdfSharp.Drawing.XPen[] pens = new PdfSharp.Drawing.XPen[] { PdfSharp.Drawing.XPens.Black, PdfSharp.Drawing.XPens.Black, PdfSharp.Drawing.XPens.Black, PdfSharp.Drawing.XPens.Black }; // pens = new PdfSharp.Drawing.XPen[] { PdfSharp.Drawing.XPens.Yellow, PdfSharp.Drawing.XPens.HotPink, PdfSharp.Drawing.XPens.Blue, PdfSharp.Drawing.XPens.Green }; double halfPenWidth = pens[0].Width / 2.0; double x1 = 0; double y1 = margin.Top - halfPenWidth; double x2 = width; double y2 = margin.Top - halfPenWidth; gfx.DrawLine(pens[0], x1, y1, x2, y2); // Horizontal Top - Yellow // y1 = height-pageMargin; x1 = 0; y1 = height - margin.Bottom + halfPenWidth; x2 = width; // 1/4 + 1/8 ()*0.5= 3/16 y2 = height - margin.Bottom + halfPenWidth; gfx.DrawLine(pens[1], x1, y1, x2, y2); // Horizontal Bottom - Hotpink x1 = margin.Left - halfPenWidth; y1 = 0; x2 = margin.Left - halfPenWidth; y2 = height; gfx.DrawLine(pens[2], x1, y1, x2, y2); // Vertical Left - Blue x1 = width - margin.Right + halfPenWidth; y1 = 0; x2 = width - margin.Right + halfPenWidth; y2 = height; gfx.DrawLine(pens[3], x1, y1, x2, y2); // Vertical Right - Green }
} // End Sub CropPdf2 static void CropPdf3(double page_width, double page_height) { string fn = @"D:\username\Desktop\0001 Altstetten - GB01 H602 - OG14.pdf"; // fn = @"D:\username\Desktop\0030 Sentimatt - GB01 Sentimatt - EG00.pdf"; fn = @"D:\username\Desktop\Altstetten_1_50.pdf"; // The current implementation of PDFsharp has only one layout of the graphics context. // The origin(0, 0) is top left and coordinates grow right and down. // The unit of measure is always point (1 / 72 inch). // 1 pt = 0,0352778 cm // 1 pt = 0,352778 mm // 1 inch = 2,54 cm // 1/72 inch to cm = 0,035277777777777776 cm // A0: // w: 2384 pt = 84,10222 cm // h: 3370 pt = 118,8861 cm // A3: // w: 842 pt = 29,7039 cm // h: 1191 pt = 42,01583 cm // A4: // 595 pt to cm = 20,9903 w // 842 pt to cm = 29,7039 h // A0 page_width = 2384; page_height = 3370; // A3 page_width = 842; page_height = 1191; // A4 page_width = 595; page_height = 842; PdfMargin margin = new PdfMargin(mm2pt(10)); // 1cm in pt double crop_width = page_width - margin.Left - margin.Right; double crop_height = page_height - margin.Top - margin.Bottom; using (PdfSharp.Drawing.XPdfForm sourceForm = PdfSharp.Drawing.XPdfForm.FromFile(fn)) { sourceForm.PageNumber = 1; int numHori = (int)System.Math.Ceiling(sourceForm.Page.Width.Point / crop_width); int numVerti = (int)System.Math.Ceiling(sourceForm.Page.Height.Point / crop_height); PdfSharp.Drawing.XRect pageDimenstions = new PdfSharp.Drawing.XRect(0, 0, sourceForm.Page.Width, sourceForm.Page.Height); using (PdfSharp.Pdf.PdfDocument destDocument = new PdfSharp.Pdf.PdfDocument()) { for (int iverti = 0; iverti < numVerti; iverti++) { for (int ihori = 0; ihori < numHori; ihori++) { PdfSharp.Pdf.PdfPage destPage = destDocument.AddPage(); destPage.Width = crop_width; destPage.Height = crop_height; PdfSharp.Drawing.XRect cropRect = new PdfSharp.Drawing.XRect(ihori * crop_width, iverti * crop_height, sourceForm.Page.Width, sourceForm.Page.Height); using (PdfSharp.Drawing.XGraphics destGFX = PdfSharp.Drawing.XGraphics.FromPdfPage(destPage)) { destGFX.DrawImageCropped(sourceForm, cropRect, pageDimenstions, PdfSharp.Drawing.XGraphicsUnit.Point); } // End Using destGFX } // ihori } // iverti // destDocument.Save("chopped.pdf"); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { destDocument.Save(ms); ms.Position = 0; using (PdfSharp.Drawing.XPdfForm croppedImages = PdfSharp.Drawing.XPdfForm.FromStream(ms)) { using (PdfSharp.Pdf.PdfDocument finalDestination = new PdfSharp.Pdf.PdfDocument()) { PdfSharp.Drawing.XFont font = new PdfSharp.Drawing.XFont("Arial", 8); for (int i = 0; i < croppedImages.PageCount; ++i) { PdfSharp.Pdf.PdfPage targetPage = finalDestination.AddPage(); targetPage.Width = page_width; targetPage.Height = page_height; PdfSharp.Drawing.XRect pageSize = new PdfSharp.Drawing.XRect(0, 0, targetPage.Width, targetPage.Height); try { croppedImages.PageIndex = i; using (PdfSharp.Drawing.XGraphics targetGFX = PdfSharp.Drawing.XGraphics.FromPdfPage(targetPage)) { #if DEBUG_ME targetGFX.DrawRectangle(XBrushes.Honeydew, pageSize); #endif PdfSharp.Drawing.XRect targetRect = new PdfSharp.Drawing.XRect(margin.Left, margin.Top, crop_width, crop_height); targetGFX.DrawImage(croppedImages, targetRect, targetRect, PdfSharp.Drawing.XGraphicsUnit.Point); DrawBorder(targetGFX, targetPage.Width.Point, targetPage.Height.Point, margin); DrawCrosshairs(targetGFX, targetPage.Width.Point, targetPage.Height.Point, margin); // int numHori = (int)System.Math.Ceiling(sourceForm.Page.Width / crop_width); // int numVerti = (int)System.Math.Ceiling(sourceForm.Page.Height / crop_height); int col = i % numHori; int row = i / numHori; // targetGFX.DrawString($"Column {col + 1}/{numHori} Row {row + 1}/{numVerti}, Page {i + 1}/{croppedImages.PageCount}", font, PdfSharp.Drawing.XBrushes.Black, margin.Left + 5, targetPage.Height.Point - margin.Bottom + font.Size + 5); targetGFX.DrawString($"Spalte {col + 1}/{numHori} Zeile {row + 1}/{numVerti}, Seite {i + 1}/{croppedImages.PageCount}", font, PdfSharp.Drawing.XBrushes.Black, margin.Left + 5, targetPage.Height.Point - margin.Bottom + font.Size + 5); } // End using targetGFX } catch (System.Exception ex) { System.Console.WriteLine(croppedImages.PageIndex); System.Console.WriteLine(ex.Message); } } // Next i finalDestination.Save("CropPdf3.pdf"); } // End Using finalDestination } // End Using croppedImages } // End Using ms } // End Using destDocument } // End Using sourceForm } // End Sub CropPdf3
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
public static void CropPdf1(double crop_width, double crop_height) { string fn = @"D:\username\Desktop\0001 Altstetten - GB01 H602 - OG14.pdf"; // fn = @"D:\username\Desktop\0030 Sentimatt - GB01 Sentimatt - EG00.pdf"; // The current implementation of PDFsharp has only one layout of the graphics context. // The origin(0, 0) is top left and coordinates grow right and down. // The unit of measure is always point (1 / 72 inch). // 1 pt = 0,0352778 cm // 1 pt = 0,352778 mm // 1 inch = 2,54 cm // 1/72 inch to cm = 0,035277777777777776 cm // A0: // w: 2384 pt = 84,10222 cm // h: 3370 pt = 118,8861 cm // A3: // w: 842 pt = 29,7039 cm // h: 1191 pt = 42,01583 cm // A4: // 595 pt to cm = 20,9903 w // 842 pt to cm = 29,7039 h using (PdfSharp.Drawing.XPdfForm sourceForm = PdfSharp.Drawing.XPdfForm.FromFile(fn)) { sourceForm.PageNumber = 1; int numHori = (int)System.Math.Ceiling(sourceForm.Page.Width.Point / crop_width); int numVerti = (int)System.Math.Ceiling(sourceForm.Page.Height.Point / crop_height); PdfSharp.Drawing.XRect pageDimenstions = new PdfSharp.Drawing.XRect(0, 0, sourceForm.Page.Width.Point, sourceForm.Page.Height.Point); // Crop the PDF - HAS NO EFFECT... // sourceForm.Page.CropBox = new PdfSharp.Pdf.PdfRectangle(cropDim); using (PdfSharp.Pdf.PdfDocument destDocument = new PdfSharp.Pdf.PdfDocument()) { for (int iverti = 0; iverti < numHori; iverti++) { for (int ihori = 0; ihori < numHori; ihori++) { PdfSharp.Pdf.PdfPage destPage = destDocument.AddPage(); destPage.Width = sourceForm.Page.Width; destPage.Height = sourceForm.Page.Height; PdfSharp.Drawing.XRect cropDim = new PdfSharp.Drawing.XRect(ihori * crop_width, iverti * crop_height, crop_width, crop_height); PdfSharp.Drawing.XRect cropRect = new PdfSharp.Drawing.XRect(cropDim.X, destPage.Height.Point - cropDim.Height - cropDim.Y, cropDim.Width, cropDim.Height); using (PdfSharp.Drawing.XGraphics destGFX = PdfSharp.Drawing.XGraphics.FromPdfPage(destPage)) { destGFX.DrawImage(sourceForm, pageDimenstions); destGFX.DrawRectangle(PdfSharp.Drawing.XPens.DeepPink, cropDim); } // End Using destGFX // PdfSharp.Drawing.XRect cropRect1 = new PdfSharp.Drawing.XRect(cropRect.X - 30, cropRect.Y - 30, cropRect.Width + 30, cropRect.Height + 30); destPage.CropBox = new PdfSharp.Pdf.PdfRectangle(cropRect); // destPage.MediaBox = new PdfSharp.Pdf.PdfRectangle(cropRect1); // destPage.CropBox = new PdfSharp.Pdf.PdfRectangle(new XPoint(cropDim.X, destPage.Height - cropDim.Height - cropDim.Y), // new XSize(cropDim.Width, cropDim.Height)); } // Next ihori } // Next iverti destDocument.Save("CropPdf1.pdf"); } // End Using gfx } // End Using document } // End Sub CropPdf1
public override void PaintPdf(PdfSharp.Drawing.XGraphics g2D, int pageNo, System.Drawing.PointF pageStart, Ctx ctx, bool isView) { throw new NotImplementedException(); }
/// <summary>Add a new page if the current page is not empty</summary> public void addPage() { if (!_isPageEmpty) { if (_pageCount != 0) addPagenumber(); _page = new PdfSharp.Pdf.PdfPage(); if (_pageCount == 0) { _size.Width = _page.Width; _size.Height = _page.Height; _area = new PdfSharp.Drawing.XRect { X = PdfSharp.Drawing.XUnit.FromMillimeter(10), Y = PdfSharp.Drawing.XUnit.FromMillimeter(10), Width = _page.Width - PdfSharp.Drawing.XUnit.FromMillimeter(20), Height = _page.Height - PdfSharp.Drawing.XUnit.FromMillimeter(45) }; } else { _page = new PdfSharp.Pdf.PdfPage(); _page.Width = _size.Width; _page.Height = _size.Height; } _horizontalePosition = _area.X; _verticalPosition = _area.Y; _doc.AddPage(_page); if (_graphics != null) _graphics.Dispose(); _graphics = PdfSharp.Drawing.XGraphics.FromPdfPage(_page); _textformater = new PdfSharp.Drawing.Layout.XTextFormatter(_graphics); _pageCount++; _isPageEmpty = true; } }