private static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins, string headerString) { SizeF pageSize = doc.PageSettings.Size; PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top + margins.Bottom) { Foreground = false }; float x = margins.Left; float y = 0; //Figure out what logo to use; either the application's logo or the owning company's logo Bitmap logo = null; //Resources.logo.ToBitmap(); //Draw image in header if we have one if (logo != null) { PdfImage headerImage = PdfImage.FromImage(Utility.ResizeImage(logo, 96, 96)); float width = headerImage.Width / 3; float height = headerImage.Height / 3; headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height); } DrawLine(headerSpace.Graphics, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2); //Draw string in header PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); SizeF size = headerFontBold.MeasureString(headerString, format); headerSpace.Graphics.DrawString(headerString, headerFontBold, PdfBrushes.Black, (headerSpace.Width / 2) - (size.Width / 2), margins.Top - (size.Height + 5), format); headerSpace.Graphics.DrawString(DateTime.Now.Date.ToString("MM-dd-yyyy"), headerFontBold, PdfBrushes.Black, GetRightPage() - headerFontBold.MeasureString(DateTime.Now.Date.ToString("MM-dd-yyyy")).Width, margins.Top - (size.Height + 5), format); return(headerSpace); }
public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. string pathToFile = "ImageMasks.pdf"; using (PdfDocument pdf = new PdfDocument()) { PdfCanvas canvas = pdf.Pages[0].Canvas; canvas.Resolution = 150; canvas.Brush.Color = new PdfRgbColor(0, 255, 0); canvas.DrawRectangle(new RectangleF(50, 450, 1150, 150), PdfDrawMode.Fill); PdfImage image = pdf.AddImage("Sample data/pink.png", "Sample data/pinkMask.tif"); canvas.DrawImage(image, 550, 200); pdf.Save(pathToFile); } Process.Start(pathToFile); }
private PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins, SizeF pageSize) { //create a PdfPageTemplateElement object as header space PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top); headerSpace.Foreground = false; //declare two float variables float x = margins.Left; float y = 0; //draw image in header space PdfImage headerImage = PdfImage.FromFile("../../../../../../../Data/E-iceblueLogo.png"); float width = headerImage.Width / 2; float height = headerImage.Height / 2; headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 5, width, height); //draw line in header space PdfPen pen = new PdfPen(PdfBrushes.LightGray, 1); headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2); //return headerSpace return(headerSpace); }
protected override void CheckImage(PdfWriter writer, int key, Object obj1) { PdfImage image = (PdfImage)obj1; if (image.Get(PdfName.SMASK) != null && !PdfName.NONE.Equals(image.GetAsName(PdfName.SMASK))) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.smask.key.is.not.allowed.in.images")); } if (image.Contains(PdfName.ALTERNATES)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("an.image.dictionary.shall.not.contain.alternates.key")); } if (image.Contains(PdfName.OPI)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("an.image.dictionary.shall.not.contain.opi.key")); } PdfBoolean interpolate = image.GetAsBoolean(PdfName.INTERPOLATE); if (interpolate != null && interpolate.BooleanValue) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.value.of.interpolate.key.shall.not.be.true")); } PdfName intent = image.GetAsName(PdfName.INTENT); if (intent != null && !(PdfName.RELATIVECOLORIMETRIC.Equals(intent) || PdfName.ABSOLUTECOLORIMETRIC.Equals(intent) || PdfName.PERCEPTUAL.Equals(intent) || PdfName.SATURATION.Equals(intent))) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("1.value.of.intent.key.is.not.allowed", intent.ToString())); } }
/// <summary>生成水印文件</summary> public void Process(string docPath, string savePath, bool useText, string text, string imgPath) { // 统一用图片水印 var size = new SizeF(1200, 1200); var bgColor = Color.White; Image img = useText ? Painter.CreateTextImage(text, size, Config.TextFont, Config.TextColor, Config.Padding, Config.Angle, bgColor) : Core.Painter.LoadImage(imgPath) ; //创建一个新的PDF实例,导入PDF文件 var doc = new PdfDocument(); doc.LoadFromFile(docPath); foreach (PdfPageBase page in doc.Pages) { // 背景图片方式,pdf会变得一片漆黑 //page.BackgroundImage = img; // 放置前景图片方式 page.Canvas.Save(); page.Canvas.SetTransparency(0.15f); PdfImage pdfImage = PdfImage.FromImage(img); page.Canvas.DrawImage(pdfImage, 0, 0); page.Canvas.Restore(); } // 保存 doc.SaveToFile(savePath); doc.Close(); }
private void DrawLogImage() { // define local image resources // resolution 96 pixels per inch, image quality 100% PdfImageControl ImageControl = new PdfImageControl(); ImageControl.Resolution = 96.0; ImageControl.ImageQuality = 100; CreateLogoFile(); PdfImage Image1 = new PdfImage(Document, "temps/" + pk.Logo.Filename, ImageControl); // save graphics state Contents.SaveGraphicsState(); // translate coordinate origin to the center of the picture Contents.Translate(0, 10); // adjust image size an preserve aspect ratio PdfRectangle NewSize = Image1.ImageSizePosition(3, 1.1, ContentAlignment.MiddleCenter); // clipping path Contents.DrawOval(NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height, PaintOp.Fill); // draw image Contents.DrawImage(Image1, NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height); // restore graphics state Contents.RestoreGraphicsState(); return; }
private void button1_Click(object sender, EventArgs e) { string path = "..\\..\\..\\..\\..\\..\\Data\\"; //pdf file string input = path + "Sample4.pdf"; //open pdf document PdfDocument doc = new PdfDocument(input); //create a pdf image PdfImage footerImage = PdfImage.FromFile(path + "logo2.png"); float x = 0; float y = 0; //draw footer image into pages foreach (PdfPageBase page in doc.Pages) { x = page.Canvas.ClientSize.Width - footerImage.PhysicalDimension.Width - 10; y = page.Canvas.ClientSize.Height - footerImage.PhysicalDimension.Height - 10; page.Canvas.DrawImage(footerImage, new PointF(x, y)); } string output = "AddImageInFooter.pdf"; //Save pdf file. doc.SaveToFile(output); doc.Close(); //Launching the Pdf file. PDFDocumentViewer(output); }
private void DrawPageHeaderAndFooter(PdfPageBase page, PdfMargins margin, bool isCover) { page.Canvas.SetTransparency(0.5f); PdfImage headerImage = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Header.png"); PdfImage footerImage = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Footer.png"); page.Canvas.DrawImage(headerImage, new PointF(0, 0)); page.Canvas.DrawImage(footerImage, new PointF(0, page.Canvas.ClientSize.Height - footerImage.PhysicalDimension.Height)); if (isCover) { page.Canvas.SetTransparency(1); return; } PdfBrush brush = PdfBrushes.Black; PdfPen pen = new PdfPen(brush, 0.75f); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right); format.MeasureTrailingSpaces = true; float space = font.Height * 0.75f; float x = margin.Left; float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right; float y = margin.Top - space; page.Canvas.DrawLine(pen, x, y, x + width, y); y = y - 1 - font.Height; page.Canvas.DrawString("Demo of Spire.Pdf", font, brush, x + width, y, format); page.Canvas.SetTransparency(1); }
private void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { // specify the Cell index for columns that will use images if ((args.CellIndex == 10 || args.CellIndex == 9) && args.Style.BackgroundImage != null) { PdfImage backgroundOriginalImage = args.Style.BackgroundImage; // Calculate the new image dimensions float thumbnailSize = args.Bounds.Height; float newWidth, newHeight; //Calculate Aspect ratio of image. float radiusX = (float)(args.Bounds.Width / backgroundOriginalImage.Width); float radiusY = (float)(args.Bounds.Height / backgroundOriginalImage.Height); float r = Math.Min(radiusX, radiusY); newWidth = backgroundOriginalImage.Width * r; newHeight = backgroundOriginalImage.Height * r; //Draw image in center of the cell. float centerX = args.Bounds.X + (args.Bounds.Width - newWidth) / 2; float centerY = args.Bounds.Y + (args.Bounds.Height - newHeight) / 2; //Draw the image in center of the cell. args.Graphics.DrawImage(backgroundOriginalImage, new RectangleF(centerX, centerY, newWidth, newHeight)); args.Style.BackgroundImage = null; } }
private void TransformImage(PdfPageBase page) { PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\ChartImage.png"); int skewX = 20; int skewY = 20; float scaleX = 0.2f; float scaleY = 0.6f; int width = (int)((image.Width + image.Height * Math.Tan(Math.PI * skewX / 180)) * scaleX); int height = (int)((image.Height + image.Width * Math.Tan(Math.PI * skewY / 180)) * scaleY); PdfTemplate template = new PdfTemplate(width, height); template.Graphics.ScaleTransform(scaleX, scaleY); template.Graphics.SkewTransform(skewX, skewY); template.Graphics.DrawImage(image, 0, 0); //Save graphics state PdfGraphicsState state = page.Canvas.Save(); page.Canvas.TranslateTransform(page.Canvas.ClientSize.Width - 50, 260); float offset = (page.Canvas.ClientSize.Width - 100) / 12; for (int i = 0; i < 12; i++) { page.Canvas.TranslateTransform(-offset, 0); page.Canvas.SetTransparency(i / 12.0f); page.Canvas.DrawTemplate(template, new PointF(0, 0)); } //Restore graphics page.Canvas.Restore(state); }
//////////////////////////////////////////////////////////////////// // Draw image and clip it //////////////////////////////////////////////////////////////////// private void DrawImage(float originx, float originy) { // define local image resources // resolution 96 pixels per inch, image quality 50% PdfImageControl ImageControl = new PdfImageControl(); ImageControl.Resolution = 300; ImageControl.ImageQuality = 80; ; // ImageControl.SaveAs = SaveImageAs.GrayImage; // ImageControl.ReverseBW = true; PdfImage Image1 = new PdfImage(Document, @"C:\Users\Sriram\Downloads\10-05-2017_17-59-05_Report\20170510063212x3.jpg", ImageControl); //TestImage.jpg // save graphics state Contents.SaveGraphicsState(); // translate coordinate origin to the center of the picture //Contents.Translate(2.6, 5.0); Contents.Translate(originx, originy); // adjust image size an preserve aspect ratio PdfRectangle NewSize = Image1.ImageSizePosition(300, 200, ContentAlignment.MiddleCenter); // clipping path //Contents.DrawOval(NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height, PaintOp.ClipPathEor); // draw image Contents.DrawImage(Image1, NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height); // restore graphics state Contents.RestoreGraphicsState(); return; }
//////////////////////////////////////////////////////////////////// // Draw image of a flower and clip it //////////////////////////////////////////////////////////////////// private void DrawFlower ( PdfDocument Document, PdfContents Contents ) { // define local image resources PdfImage Image1 = new PdfImage(Document, "flower.jpg"); // image size will be limited to 1.4" by 1.4" SizeD ImageSize = Image1.ImageSize(1.4, 1.4); // save graphics state Contents.SaveGraphicsState(); // translate coordinate origin to the center of the picture Contents.Translate(3.36, 5.7); // clipping path Contents.DrawOval(-ImageSize.Width / 2, -ImageSize.Height / 2, ImageSize.Width, ImageSize.Height, PaintOp.ClipPathEor); // draw image Contents.DrawImage(Image1, -ImageSize.Width / 2, -ImageSize.Height / 2, ImageSize.Width, ImageSize.Height); // restore graphics state Contents.RestoreGraphicsState(); return; }
internal PdfNewDocument method_10(List <Image> A_0) { if (A_0 == null) { return(null); } int count = A_0.Count; int num2 = (count > 90) ? 3 : ((count > 60) ? 5 : ((count > 30) ? 10 : count)); this.pdfNewDocument_0 = this.method_12(); for (int i = 0; i < count; i++) { using (Image image = A_0[i]) { PdfNewPage page = this.method_14(new SizeF(image.Width * 0.75f, image.Height * 0.75f)).get_Pages().Add(); this.pdfPageBase_0 = page; using (PdfMetafile metafile = PdfImage.FromImage(image)) { metafile.set_Quality((long)this.int_0); metafile.ᜀ(page, new RectangleF(PointF.Empty, page.get_Size()), true, this.method_1().EmbeddedFontNameList, this.method_1().IsEmbeddedAllFonts); metafile.Dispose(); } image.Dispose(); } if ((i % num2) == 0) { GC.Collect(); } } return(this.pdfNewDocument_0); }
private void ButtonToPDF_Click(object sender, RoutedEventArgs e) { string filename_pdf = TempFile.GenerateTempFilename("pdf"); using (PdfDocument doc = new PdfDocument()) { PdfPage page = doc.Pages.Add(); SizeF bounds = page.GetClientSize(); string filename_rtf = SaveToRTF(); string text = File.ReadAllText(filename_rtf); PdfMetafile metafile = (PdfMetafile)PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile); PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat(); // Allow the text to flow multiple pages without any breaks. format.SplitTextLines = true; format.SplitImages = true; // Draw the image. metafile.Draw(page, 0, 0, format); doc.Save(filename_pdf); } Process.Start(filename_pdf); }
private static void DrawImageInFrame_wxh_Postcard(PdfImage img, PdfPageBase page, float y, float w, float h, Globals.AspectRatio aspectRatioImage1) { PdfBrush brush = new PdfSolidBrush(Color.Silver); // float heightBtwImages = unitCvtr.ConvertUnits(0.5f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); //Load an image if (img != null) { if (aspectRatioImage1 == Globals.AspectRatio.S6x4) { page.Canvas.DrawImage(img, new System.Drawing.RectangleF(0, y, w, h)); } else { page.Canvas.DrawRectangle(brush, new Rectangle(new Point(0, (int)y), new Size(Convert.ToInt32(w), Convert.ToInt32(h)))); } if (aspectRatioImage1 == Globals.AspectRatio.GreaterThanS6x4) { float new_h = w * img.Height / img.Width; float new_MarginTopBottom = (h - new_h) / 2; page.Canvas.DrawImage(img, new System.Drawing.RectangleF(0, y + new_MarginTopBottom, w, new_h)); //use width of frame, adjust height. } if (aspectRatioImage1 == Globals.AspectRatio.LessThanS6x4) { float new_w = h * img.Width / img.Height; float new_MarginLeftRight = (w - new_w) / 2; page.Canvas.DrawImage(img, new System.Drawing.RectangleF(new_MarginLeftRight, y, new_w, h)); //use width of frame, adjust height. //Graphics.FromImage(img.); } } }
static void PdfHeaderFooterEventHandler(object sender, PdfHeaderFooterEventArgs e) { var width = e.PdfPage.GetClientSize().Width; PdfPageTemplateElement header = new PdfPageTemplateElement(width, 38); string filePath = ""; if (AppDomain.CurrentDomain.BaseDirectory.Contains("Binaries")) { filePath = System.IO.Path.GetFullPath(@"..\..\datagrid\Assets\datagrid\Header.jpg"); } else { filePath = System.IO.Path.GetFullPath(@"..\..\..\datagrid\Assets\datagrid\Header.jpg"); } header.Graphics.DrawImage(PdfImage.FromFile(filePath), 155, 5, width / 3f, 34); e.PdfDocumentTemplate.Top = header; PdfPageTemplateElement footer = new PdfPageTemplateElement(width, 30); if (AppDomain.CurrentDomain.BaseDirectory.Contains("Binaries")) { filePath = System.IO.Path.GetFullPath(@"..\..\datagrid\Assets\datagrid\Footer.jpg"); } else { filePath = System.IO.Path.GetFullPath(@"..\..\..\datagrid\Assets\datagrid\Footer.jpg"); } footer.Graphics.DrawImage(PdfImage.FromFile(filePath), 0, 0); e.PdfDocumentTemplate.Bottom = footer; }
private void SetHeader(PdfDocument document) { if (!checkBoxAddHeader.Checked) { return; } // create the document header document.CreateHeaderCanvas(50); // add PDF objects to the header canvas string headerImageFile = Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo.png"; PdfImage logoHeaderImage = new PdfImage(5, 5, 40, System.Drawing.Image.FromFile(headerImageFile)); document.Header.Layout(logoHeaderImage); // layout HTML in header PdfHtml headerHtml = new PdfHtml(50, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic""> Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null); headerHtml.FitDestHeight = true; headerHtml.FontEmbedding = true; document.Header.Layout(headerHtml); // create a border for header float headerWidth = document.Header.Width; float headerHeight = document.Header.Height; PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2); borderRectangle.LineStyle.LineWidth = 0.5f; borderRectangle.ForeColor = System.Drawing.Color.Navy; document.Header.Layout(borderRectangle); }
private void button1_Click(object sender, EventArgs e) { //Load document from disk PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile(@"../../../../../../Data/PDFTemplate_N.pdf"); //Load an image PdfImage image = PdfImage.FromFile("../../../../../../Data/E-iceblueLogo.png"); foreach (PdfPageBase page in pdf.Pages) { //Create PdfTilingBrush PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.Size.Width / 3, page.Canvas.Size.Height / 5)); //Set the transparency brush.Graphics.SetTransparency(0.3f); //Draw image on brush graphics brush.Graphics.DrawImage(image, new PointF((brush.Size.Width - image.Width) / 2, (brush.Size.Height - image.Height) / 2)); //use the brush to draw rectangle page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size)); } //Save the Pdf document string output = "AddTilingBackgroundImage_out.pdf"; pdf.SaveToFile(output, FileFormat.PDF); //Launch the Pdf file PDFDocumentViewer(output); }
private void button1_Click(object sender, EventArgs e) { //Load Pdf document from disk PdfDocument doc = new PdfDocument(); doc.LoadFromFile("../../../../../../Data/PDFTemplate_N.pdf"); //Load an image Image image = Image.FromFile("../../../../../../Data/E-logo.png"); //Adjust image size int width = image.Width; int height = image.Height; float schale = 1.5f; Size size = new Size((int)(width * schale), (int)(height * schale)); Bitmap schaleImage = new Bitmap(image, size); //Insert an image into the first PDF page at specific position PdfImage pdfImage = PdfImage.FromImage(schaleImage); PdfPageBase page = doc.Pages[0]; PointF position = new PointF(160, 260); page.Canvas.Save(); page.Canvas.SetTransparency(0.5f, 0.5f, PdfBlendMode.Multiply); page.Canvas.DrawImage(pdfImage, position); page.Canvas.Restore(); //Save the Pdf document string output = "ImageWatermarkSecondApproach_out.pdf"; doc.SaveToFile(output, FileFormat.PDF); //Launch the Pdf file PDFDocumentViewer(output); }
/// <summary> /// Gets the resource name of the specified image within this page. /// </summary> internal string GetImageName(XImage image) { PdfImage pdfImage = _document.ImageTable.GetImage(image); Debug.Assert(pdfImage != null); string name = Resources.AddImage(pdfImage); return name; }
/// <summary> /// Fired when Header and Footer are exported to PDF /// </summary> /// <param name="sender">PDF Export_HeaderAndFooterExporting sender</param> /// <param name="e">PDF Export_HeaderAndFooterExporting EventArgs e</param> private void PdfExport_HeaderAndFooterExporting(object sender, PdfHeaderFooterEventArgs e) { var width = e.PdfPage.GetClientSize().Width; PdfStandardFont font = null; PdfPageTemplateElement header = new PdfPageTemplateElement(width, 60); var assmbely = this.GetType().GetTypeInfo().Assembly; #if COMMONSB var imagestream = assmbely.GetManifestResourceStream("SampleBrowser.Icons.SyncfusionLogo.jpg"); #else var imagestream = assmbely.GetManifestResourceStream("SampleBrowser.SfDataGrid.Icons.SyncfusionLogo.jpg"); #endif PdfImage pdfImage = PdfImage.FromStream(imagestream); header.Graphics.DrawImage(pdfImage, new RectangleF(width - 148, 0, 148, 60)); if (Device.RuntimePlatform == Device.iOS) { font = new PdfStandardFont(PdfFontFamily.Helvetica, 18, PdfFontStyle.Bold); } else { font = new PdfStandardFont(PdfFontFamily.Helvetica, 13, PdfFontStyle.Bold); } PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); header.Graphics.DrawString("Customer Details", font, PdfBrushes.Black, new RectangleF(0, 25, 200, 60), format); e.PdfDocumentTemplate.Top = header; }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document PdfDocument doc = new PdfDocument(); //Load file from disk. doc.LoadFromFile(@"..\..\..\..\..\..\Data\ReplaceImage.pdf"); //Get the first page. PdfPageBase page = doc.Pages[0]; //Load a image PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\E-iceblueLogo.png"); //Replace the first image on the page. page.ReplaceImage(0, image); String result = "Output.pdf"; //Save the document doc.SaveToFile(result); //Launch the Pdf file PDFDocumentViewer("Output.pdf"); }
static async void PdfHeaderFooterEventHandler(object sender, TreeGridPdfHeaderFooterEventArgs e) { var width = e.PdfPage.GetClientSize().Width; PdfPageTemplateElement header = new PdfPageTemplateElement(width, 38); e.PdfDocumentTemplate.Top = header; PdfPageTemplateElement footer = new PdfPageTemplateElement(width, 30); e.PdfDocumentTemplate.Bottom = footer; var uri = new Uri("ms-appx:///Images/Header.png", UriKind.RelativeOrAbsolute); var srcfile = await StorageFile.GetFileFromApplicationUriAsync(uri); var stream = await srcfile.OpenStreamForReadAsync(); header.Graphics.DrawImage(PdfImage.FromStream(stream), 0, 0, width / 3f, 34); uri = new Uri("ms-appx:///Images/Footer.png", UriKind.RelativeOrAbsolute); srcfile = await StorageFile.GetFileFromApplicationUriAsync(uri); stream = await srcfile.OpenStreamForReadAsync(); footer.Graphics.DrawImage(PdfImage.FromStream(stream), 0, 0, width, 25); stream.Dispose(); }
//Exports and saves the chart in Pdf. #region Pdf private void buttonPdf_Click(object sender, EventArgs e) { try { exportFileName = fileName + ".pdf"; string file = fileName + ".gif"; if (!System.IO.File.Exists(file)) { this.chartControl1.SaveImage(file); } //Create a new PDF Document. The pdfDoc object represents the PDF document. //This document has one page by default and additional pages have to be added. PdfDocument pdfDoc = new PdfDocument(); pdfDoc.Pages.Add(); pdfDoc.Pages[0].Graphics.DrawImage(PdfImage.FromFile(file), new PointF(10, 30)); //Save the PDF Document to disk. pdfDoc.Save(exportFileName); OpenFile("Pdf", exportFileName); } catch (Exception ex) { this.toolStripStatusLabel1.Text = "Chart Export failed."; Console.WriteLine(ex.ToString()); } }
private void btnExportarPDF_Click(object sender, EventArgs e) { if (dgvMaterias.Rows.Count == 0) { MessageBox.Show("Sin datos por EXPORTAR", "Aviso", MessageBoxButtons.OK); } else { PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add(); PdfTable table = new PdfTable(); table.DataSource = dgvMaterias.DataSource; table.Style.ShowHeader = true; PdfImage image = PdfImage.FromFile(Path.Combine(System.IO.Path.GetFullPath(@"..\..\"), "Resources\\reporte.jpeg")); float width = image.Width * 0.75f; float height = image.Height * 0.75f; float x = (page.Canvas.ClientSize.Width - width) / 2; page.Canvas.DrawImage(image, x, 60, width, height); table.Style.CellPadding = 2; PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat(); tableLayout.Break = PdfLayoutBreakType.FitElement; tableLayout.Layout = PdfLayoutType.Paginate; table.BeginRowLayout += new BeginRowLayoutEventHandler(BeginRowLayout); table.Draw(page, new RectangleF(10, 30, 500, 700), tableLayout); pdf.SaveToFile("C:\\Users\\AbelFH\\Desktop\\Horarios-Asignados.pdf"); MessageBox.Show("PDF generado exitosamente", "Aviso", MessageBoxButtons.OK); } }
//////////////////////////////////////////////////////////////////// // Draw image and clip it //////////////////////////////////////////////////////////////////// private void DrawImage() { // define local image resources // resolution 96 pixels per inch, image quality 50% PdfImageControl ImageControl = new PdfImageControl(); ImageControl.Resolution = 96.0; ImageControl.ImageQuality = 50; // ImageControl.SaveAs = SaveImageAs.GrayImage; // ImageControl.ReverseBW = true; PdfImage Image1 = new PdfImage(Document, "TestImage.jpg", ImageControl); // save graphics state Contents.SaveGraphicsState(); // translate coordinate origin to the center of the picture Contents.Translate(2.6, 5.0); // adjust image size an preserve aspect ratio PdfRectangle NewSize = Image1.ImageSizePosition(1.75, 1.5, ContentAlignment.MiddleCenter); // clipping path Contents.DrawOval(NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height, PaintOp.ClipPathEor); // draw image Contents.DrawImage(Image1, NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height); // restore graphics state Contents.RestoreGraphicsState(); return; }
protected override void CheckImage(PdfWriter writer, int key, Object obj1) { PdfImage pdfImage = (PdfImage)obj1; if (pdfImage != null && (pdfImage.Image is Jpeg2000)) { Jpeg2000 jpeg2000 = (Jpeg2000)pdfImage.Image; if (!jpeg2000.IsJp2()) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("only.jpx.baseline.set.of.features.shall.be.used")); } if (jpeg2000.GetNumOfComps() != 1 && jpeg2000.GetNumOfComps() != 3 && jpeg2000.GetNumOfComps() != 4) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("the.number.of.colour.channels.in.the.jpeg2000.data.shall.be.123")); } if (jpeg2000.Bpc < 1 || jpeg2000.Bpc > 38) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("the.bit-depth.of.the.jpeg2000.data.shall.have.a.value.in.the.range.1to38")); } if (jpeg2000.GetBpcBoxData() != null) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("all.colour.channels.in.the.jpeg2000.data.shall.have.the.same.bit-depth")); } List <Jpeg2000.ColorSpecBox> colorSpecBoxes = jpeg2000.GetColorSpecBoxes(); if (colorSpecBoxes != null) { if (colorSpecBoxes.Count > 1) { int approx0x01 = 0; foreach (Jpeg2000.ColorSpecBox colorSpecBox in colorSpecBoxes) { if (colorSpecBox.GetApprox() == 1) { approx0x01++; } } if (approx0x01 != 1) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("exactly.one.colour.space.specification.shall.have.the.value.0x01.in.the.approx.field")); } } foreach (Jpeg2000.ColorSpecBox colorSpecBox in colorSpecBoxes) { if (colorSpecBox.GetMeth() != 1 && colorSpecBox.GetMeth() != 2 && colorSpecBox.GetMeth() != 3) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("the.value.of.the.meth.entry.in.colr.box.shall.be.123")); } if (colorSpecBox.GetEnumCs() == 19) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("jpeg2000.enumerated.colour.space.19.(CIEJab).shall.not.be.used")); } byte[] colorProfileBytes = colorSpecBox.GetColorProfile(); if (colorProfileBytes != null) { //ICC profile verification should follow here. } } } } }
internal static Globals.AspectRatio DetectImageAspectRatio_Postcard(PdfImage image) { logger.Info("Printing image"); float height = image.Width;// Swap width and height in case of A4 float width = image.Height; //if (width < height) // logger.Error("Image rotation is needed"); float ap = width / height; if (Math.Abs(Math.Round(ap, 2) - 1.5) < 0.1) //compare with 4/3 aspect ratio, if equal { return(Globals.AspectRatio.S6x4); } if (Math.Abs(Math.Round(ap, 2) - 1.5) > 0.1 && (Math.Round(ap, 2) - 1.5) < 0) // -ve value means aspect ratio is lower than 4x3 { return(Globals.AspectRatio.LessThanS6x4); } if (Math.Abs(Math.Round(ap, 2) - 1.5) > 0.1 && (Math.Round(ap, 2) - 1.5) > 0) // +ve value means aspect ratio is higher than 4x3, could be 3x2 = 1.5 { return(Globals.AspectRatio.GreaterThanS6x4); } return(Globals.AspectRatio.S6x4); }
public static void getReport(string path, Entrada oE, string pathImg) { PdfDocument myPdfDocument = new PdfDocument(PdfDocumentFormat.InCentimeters(21.59, 27.94)); PdfPage newPdfPage = myPdfDocument.NewPage(); // This will load the image without placing into the document. The good thing // is that the image will be written into the document just once even if we put it // more times and in different sizes and places! PdfImage LogoImage = myPdfDocument.NewImage(pathImg); //// now we start putting the logo into the right place with a high resoluton... newPdfPage.Add(LogoImage, 10, 20, 300); setHeader(myPdfDocument, newPdfPage, oE); setBody(myPdfDocument, newPdfPage, oE); setFooter(myPdfDocument, newPdfPage, oE); try { newPdfPage.SaveToDocument(); myPdfDocument.SaveToFile(path); } catch { throw; } }
private void button1_Click(object sender, EventArgs e) { //Create a new PDF document. PdfDocument doc = new PdfDocument(); //Load the document from disk. doc.LoadFromFile(@"..\..\..\..\..\..\Data\AddSeamSeals.pdf"); PdfUnitConvertor convert = new PdfUnitConvertor(); PdfPageBase pageBase = null; //Get the segmented seal image. Image[] images = GetImage(doc.Pages.Count); float x = 0; float y = 0; //Draw the picture to the designated location on the PDF page. for (int i = 0; i < doc.Pages.Count; i++) { pageBase = doc.Pages[i]; x = pageBase.Size.Width - convert.ConvertToPixels(images[i].Width, PdfGraphicsUnit.Point) + 40; y = pageBase.Size.Height / 2; pageBase.Canvas.DrawImage(PdfImage.FromImage(images[i]), new PointF(x, y)); } String result = "AddSeamSeals_out.pdf"; //Save the Pdf file. doc.SaveToFile(result); //Launch the Pdf file. PDFDocumentViewer(result); }