public void DrawImage(Bitmap image, string noi_luu) { using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) { documentProcessor.LoadDocument(fileName); DevExpress.Pdf.PdfPage page = documentProcessor.Document.Pages[pdfViewer1.CurrentPageNumber - 1]; using (PdfGraphics graphics = documentProcessor.CreateGraphics()) { float width = (float)Math.Abs(startPosition.Point.X - endPosition.Point.X); float height = (float)Math.Abs(startPosition.Point.Y - endPosition.Point.Y); PdfRectangle cropbox = page.CropBox; // MessageBox.Show(cropbox.Height.ToString()); float Y = (float)(cropbox.Height - startPosition.Point.Y); RectangleF rec = new RectangleF((float)startPosition.Point.X, Y, width, height); // RectangleF test = new RectangleF(10, 30, width, height); graphics.DrawImage(image, rec); graphics.AddToPageForeground(page, 72, 72); } documentProcessor.SaveDocument(noi_luu); LoadPDFFile(noi_luu); } }
//, DrawText waterMark, DrawText numberDowload, QR qr public void AddGraphicJoin(PdfDocumentProcessor processor, Object data, EPDFFile pdfFile) { IList <PdfPage> Pages = processor.Document.Pages; for (int i = 0; i < Pages.Count; i++) { PdfPage Page = Pages[i]; using (PdfGraphics graphics = processor.CreateGraphics()) { SizeF actualPageSize = PrepareGraphics.PrepareGraphicsSizeF(Page, graphics, DrawIngDpi, DrawIngDpi); switch (pdfFile) { case EPDFFile.SolicitudLinea4: SolicitudCreditoL4 SolicitudL4 = (SolicitudCreditoL4)data; SolicitudCreditoContent.NPageContent(graphics, i, actualPageSize.Width, actualPageSize.Height, SolicitudL4); break; case EPDFFile.Presupuesto: PresupuestoInfonavit Presupuesto = (PresupuestoInfonavit)data; PresupuestoInfonavitContent.NPageContent(graphics, i, actualPageSize.Width, actualPageSize.Height, Presupuesto); break; case EPDFFile.PresupuestoDesglose: PresupuestoDInfonavit PresupuestoDesglose = (PresupuestoDInfonavit)data; PresupuestoDInfonavitContent.NPageContent(graphics, i, actualPageSize.Width, actualPageSize.Height, PresupuestoDesglose); break; } graphics.AddToPageForeground(Page, DrawIngDpi, DrawIngDpi); } } }
static void DrawGraphics(PdfGraphics graph, PdfTextSearchResults result, SolidBrush brush) { for (int i = 0; i < result.Rectangles.Count; i++) { RectangleF rect = new RectangleF(new PointF((float)result.Rectangles[i].Left, (float)result.Page.CropBox.Top - (float)result.Rectangles[i].Top), new SizeF((float)result.Rectangles[i].Width, (float)result.Rectangles[i].Height)); graph.FillRectangle(brush, rect); } graph.AddToPageForeground(result.Page, 72, 72); }
static void AddGraphics(PdfDocumentProcessor processor, string text, SolidBrush textBrush) { IList <PdfPage> pages = processor.Document.Pages; for (int i = 0; i < pages.Count; i++) { PdfPage page = pages[i]; using (PdfGraphics graphics = processor.CreateGraphics()) { SizeF actualPageSize = PrepareGraphics(page, graphics); using (Font font = new Font("Segoe UI", 20, FontStyle.Regular)) { SizeF textSize = graphics.MeasureString(text, font, PdfStringFormat.GenericDefault, DrawingDpi, DrawingDpi); PointF topLeft = new PointF(0, 0); PointF bottomRight = new PointF(actualPageSize.Width - textSize.Width, actualPageSize.Height - textSize.Height); graphics.DrawString(text, font, textBrush, topLeft); graphics.DrawString(text, font, textBrush, bottomRight); graphics.AddToPageForeground(page, DrawingDpi, DrawingDpi); } } } }
public void AddWatermark(string text, string fileName, string resultFileName) { using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) { string fontName = "Arial Black"; int fontSize = 12; PdfStringFormat stringFormat = PdfStringFormat.GenericTypographic; stringFormat.Alignment = PdfStringAlignment.Center; stringFormat.LineAlignment = PdfStringAlignment.Center; documentProcessor.LoadDocument(fileName); using (SolidBrush brush = new SolidBrush(Color.FromArgb(63, Color.Black))) { using (Font font = new Font(fontName, fontSize)) { foreach (var page in documentProcessor.Document.Pages) { var watermarkSize = page.CropBox.Width * 0.75; using (PdfGraphics graphics = documentProcessor.CreateGraphics()) { SizeF stringSize = graphics.MeasureString(text, font); Single scale = Convert.ToSingle(watermarkSize / stringSize.Width); graphics.TranslateTransform(Convert.ToSingle(page.CropBox.Width * 0.5), Convert.ToSingle(page.CropBox.Height * 0.5)); graphics.RotateTransform(-45); graphics.TranslateTransform(Convert.ToSingle(-stringSize.Width * scale * 0.5), Convert.ToSingle(-stringSize.Height * scale * 0.5)); using (Font actualFont = new Font(fontName, fontSize * scale)) { RectangleF rect = new RectangleF(0, 0, stringSize.Width * scale, stringSize.Height * scale); graphics.DrawString(text, actualFont, brush, rect, stringFormat); } graphics.AddToPageForeground(page, 72, 72); } } } } documentProcessor.SaveDocument(resultFileName); } }
static void Main(string[] args) { const float dpix = 72f; const float dpiY = 72f; using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) { // Load a PDF document with AcroForm data. processor.LoadDocument("..\\..\\InteractiveForm.pdf"); PdfDocumentFacade documentFacade = processor.DocumentFacade; PdfAcroFormFacade acroForm = documentFacade.AcroForm; string fieldName = "Address"; PdfTextFormFieldFacade formField = acroForm.GetFormField(fieldName) as PdfTextFormFieldFacade; if (formField == null) { return; } foreach (PdfWidgetFacade widget in formField) { PdfRectangle rect = widget.Rectangle; PdfPage page = processor.Document.Pages[widget.PageNumber - 1]; double x = rect.Left - page.CropBox.Left; double y = page.CropBox.Top - rect.Bottom; // Create graphics and draw an image. using (PdfGraphics graphics = processor.CreateGraphics()) { DrawImage(graphics, rect, x, y); graphics.AddToPageForeground(page, dpix, dpiY); } } processor.RemoveFormField(fieldName); processor.SaveDocument("..\\..\\Result.pdf"); } }
private void GenerateReportOfSegment(double BegMeasure, double endMeasure, string outFile) { if (checkBoxRotateMap.Checked) { IFeatureLayer pCenterlineLayer = null; string pCenterlineFileName = comboBoxExCenterline.SelectedItem.ToString(); for (int i = 0; i < pMapcontrol.LayerCount; i++) { if (pCenterlineFileName == pMapcontrol.get_Layer(i).Name) { pCenterlineLayer = pMapcontrol.get_Layer(i) as IFeatureLayer; } } IFeatureClass pCenterlineFC = pCenterlineLayer.FeatureClass; try { IFeatureCursor pCurcor = pCenterlineFC.Search(null, false); IFeature pFeature = pCurcor.NextFeature(); IGeometry pGeo = pFeature.Shape; IMSegmentation pMS = pGeo as IMSegmentation; double minM = pMS.MMin; double maxM = pMS.MMax; BegMeasure = BegMeasure < minM ? minM : BegMeasure; endMeasure = endMeasure > maxM ? maxM : endMeasure; IPoint begPt = pMS.GetPointsAtM(BegMeasure, 0).Geometry[0] as IPoint; IPoint endPt = pMS.GetPointsAtM(endMeasure, 0).Geometry[0] as IPoint; ISpatialReference sourcePrj; sourcePrj = pCenterlineFC.Fields.get_Field(pCenterlineFC.FindField(pCenterlineFC.ShapeFieldName)).GeometryDef.SpatialReference; ISpatialReference targetPrj = pMapcontrol.Map.SpatialReference; IPoint begPtPrj; IPoint endPtPrj; begPt.Project(targetPrj); endPt.Project(targetPrj); begPtPrj = begPt; endPtPrj = endPt; IActiveView pView = this.pMapcontrol.Map as IActiveView; pView.ScreenDisplay.DisplayTransformation.Rotation = 0; double widthHeightR = (((double)pView.ExportFrame.right - pView.ExportFrame.left) / ((double)pView.ExportFrame.bottom - pView.ExportFrame.top)); // Dim fw As New FileWindow(begPt, endPt, widthHeightR) MCenterlineUtil fw = new MCenterlineUtil(begPtPrj, endPtPrj, widthHeightR); fw.FitActiveViewTo(pView, true); System.Runtime.InteropServices.Marshal.ReleaseComObject(pCurcor); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } #region //导出arcmap 地图 export mapfram IPageLayout _PageLayout = this.pPageLayoutControl.PageLayout; SizeF m_PageSize = new SizeF(); double width; double hight; _PageLayout.Page.QuerySize(out width, out hight); m_PageSize.Width = Convert.ToSingle(width); m_PageSize.Height = Convert.ToSingle(hight); IUnitConverter pUConvert = new UnitConverterClass(); esriUnits ut = _PageLayout.Page.Units; float pageUnitToInchUnitScale = (float)pUConvert.ConvertUnits(1, ut, esriUnits.esriInches); var layerOutPageSize = m_PageSize; var outputDPI = 300; int BandHeadWidth = 40; var originalActiveView = _PageLayout as IActiveView; var activeView = _PageLayout as IActiveView; originalActiveView.Refresh(); var avEvents = _PageLayout as IActiveViewEvents; float centerX = layerOutPageSize.Width / 2; float centerY = layerOutPageSize.Height / 2; var backGroundEnv = new Envelope() as IEnvelope; backGroundEnv.PutCoords(0, 0, layerOutPageSize.Width, layerOutPageSize.Height); activeView.Extent = backGroundEnv; //System.Threading.Thread.Sleep(2000); IExport pExporter = new ExportPDF() as IExport; pExporter.ExportFileName = outFile; pExporter.Resolution = outputDPI; var pExPdf = pExporter as IExportPDF; pExPdf.Compressed = true; pExPdf.ImageCompression = esriExportImageCompression.esriExportImageCompressionDeflate; var screenResolution = GetCurrentScreenResolution(); var outputResolution = outputDPI; ESRI.ArcGIS.esriSystem.tagRECT exportRECT; exportRECT.left = 0; exportRECT.top = 0; exportRECT.right = (int)(activeView.ExportFrame.right * outputResolution / screenResolution) + 1; exportRECT.bottom = (int)(activeView.ExportFrame.bottom * outputResolution / screenResolution) + 1; var envelope = new Envelope() as IEnvelope; envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); pExporter.PixelBounds = envelope; var hDC = pExporter.StartExporting(); activeView.Output(hDC, outputDPI, exportRECT, backGroundEnv, null); pExporter.FinishExporting(); pExporter.Cleanup(); // MessageBox.Show("Export finished."); #endregion #region //根据中线图层生成剖面图 System.Drawing.Point LastBottomLeftPoint = new System.Drawing.Point(); int bandwidth = 0; PdfGraphics pGraphics = null; IFeatureLayer pCenterlinePointLayer = null; string centerlinePointnName = cboBoxPointLayer.SelectedItem.ToString(); for (int i = 0; i < pMapcontrol.LayerCount; i++) { if (centerlinePointnName == pMapcontrol.get_Layer(i).Name) { pCenterlinePointLayer = pMapcontrol.get_Layer(i) as IFeatureLayer; } } IQueryFilter pQF = null; DataTable centerlinePointTable = AOFunctions.GDB.ITableUtil.GetDataTableFromITable(pCenterlinePointLayer.FeatureClass as ITable, pQF); chartControl1.Series.Clear(); Series series = new Series("高程", ViewType.Line); Series series2 = new Series("埋深", ViewType.Line); SecondaryAxisY myAxisY = new SecondaryAxisY("埋深"); foreach (DataRow r in centerlinePointTable.Rows) { double m; double z; double underz; m = Convert.ToDouble(r[EvConfig.CenterlineMeasureField]); if (m < BegMeasure || m > endMeasure) { continue; } if (r[EvConfig.CenterlineMeasureField] != DBNull.Value && r[EvConfig.CenterlineZField] != DBNull.Value) { m = Convert.ToDouble(r[EvConfig.CenterlineMeasureField]); z = Convert.ToDouble(r[EvConfig.CenterlineZField]); series.Points.Add(new SeriesPoint(m, z)); } // if (r[EvConfig.CenterlineMeasureField] != DBNull.Value && r["管道埋深("] != DBNull.Value) if (r[EvConfig.CenterlineMeasureField] != DBNull.Value && r[EvConfig.CenterlineBuryDepthField] != DBNull.Value) { m = Convert.ToDouble(r[EvConfig.CenterlineMeasureField]); z = Convert.ToDouble(r[EvConfig.CenterlineBuryDepthField]); series2.Points.Add(new SeriesPoint(m, z)); } } chartControl1.Series.Add(series); chartControl1.Series.Add(series2); ((XYDiagram)chartControl1.Diagram).SecondaryAxesY.Clear(); ((XYDiagram)chartControl1.Diagram).SecondaryAxesX.Clear(); ((XYDiagram)chartControl1.Diagram).SecondaryAxesY.Add(myAxisY); ((LineSeriesView)series2.View).AxisY = myAxisY; ((LineSeriesView)series.View).AxisY = ((XYDiagram)chartControl1.Diagram).AxisY; series.ChangeView(ViewType.Line); ((XYDiagram)chartControl1.Diagram).AxisY.WholeRange.Auto = true; myAxisY.WholeRange.Auto = true; series2.ChangeView(ViewType.Line); myAxisY.Tickmarks.Visible = true; myAxisY.Tickmarks.MinorVisible = false; ((XYDiagram)chartControl1.Diagram).AxisX.WholeRange.AutoSideMargins = false; ((XYDiagram)chartControl1.Diagram).AxisX.WholeRange.SideMarginsValue = 0; ((XYDiagram)chartControl1.Diagram).AxisX.WholeRange.SetMinMaxValues(BegMeasure, endMeasure); series.ArgumentScaleType = ScaleType.Numerical; int ProfileMapHight = 8; #endregion #region export chartcontrol IGraphicsContainer pGC = _PageLayout as IGraphicsContainer; pGC.Reset(); IElement pE = pGC.Next(); while (pE != null) { if (pE is IMapFrame) { // framgeo related to _PageLayout.Page pagesize, 8.5 x 11, inch IEnvelope framgeo = pE.Geometry.Envelope as IEnvelope; float defaultDPI = PdfGraphics.DefaultDpi; double x = defaultDPI * framgeo.LowerLeft.X * pageUnitToInchUnitScale; double y = defaultDPI * (layerOutPageSize.Height - framgeo.LowerLeft.Y) * pageUnitToInchUnitScale + 1; bandwidth = (int)(defaultDPI * (framgeo.Width * pageUnitToInchUnitScale)); LastBottomLeftPoint.X = (int)x; LastBottomLeftPoint.Y = (int)y; chartControl1.Size = new System.Drawing.Size((int)(framgeo.Width * pageUnitToInchUnitScale * defaultDPI), (int)(ProfileMapHight * pageUnitToInchUnitScale * defaultDPI)); Image img = null; Bitmap bmp = null; // Create an image of the chart. ImageFormat format = ImageFormat.Png; using (MemoryStream s = new MemoryStream()) { chartControl1.ExportToImage(s, format); img = Image.FromStream(s); bmp = new Bitmap(img); bmp.SetResolution(300, 300); } using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) { processor.LoadDocument(outFile); PdfDocument pd = processor.Document; PdfPage page = pd.Pages[0]; PdfRectangle pdfrec = page.MediaBox; // RectangleF rf = new RectangleF(0, 0, Convert.ToSingle(pdfrec.Width) * 96 / 72, Convert.ToSingle(pdfrec.Height) * 96 / 72); // Create and draw PDF graphics. using (pGraphics = processor.CreateGraphics()) { PdfGraphics graph = pGraphics; Pen defaultPen = new Pen(Color.Black); defaultPen.Width = 1; graph.DrawImage(bmp, new RectangleF((float)x, (float)y, (float)framgeo.Width * pageUnitToInchUnitScale * defaultDPI, (float)ProfileMapHight * pageUnitToInchUnitScale * defaultDPI)); // draw map head. RectangleF MapheadRect = new RectangleF((float)(int)(defaultDPI * framgeo.XMin * pageUnitToInchUnitScale - BandHeadWidth), (float)(int)(defaultDPI * (layerOutPageSize.Height - framgeo.UpperLeft.Y) * pageUnitToInchUnitScale), (float)(int)(BandHeadWidth), (float)(int)(defaultDPI * framgeo.Height * pageUnitToInchUnitScale)); graph.DrawRectangle(defaultPen, MapheadRect); //draw head text RectangleF rF = new RectangleF(0, 0, (float)MapheadRect.Height, (float)BandHeadWidth); PdfStringFormat psf = new PdfStringFormat(PdfStringFormat.GenericDefault); psf.Alignment = PdfStringAlignment.Center; psf.LineAlignment = PdfStringAlignment.Center; graph.SaveGraphicsState(); // head box 的左下角 System.Drawing.Font textfont = new System.Drawing.Font("仿宋", 6F); graph.TranslateTransform((float)(MapheadRect.Left), (float)(MapheadRect.Bottom)); graph.RotateTransform(270); graph.DrawString("地图", textfont, new SolidBrush(Color.Black), rF, psf); graph.RestoreGraphicsState(); // draw chart head RectangleF ChartheadRect = new RectangleF((float)(int)(defaultDPI * framgeo.XMin * pageUnitToInchUnitScale - BandHeadWidth), (float)(int)(defaultDPI * (layerOutPageSize.Height - framgeo.LowerLeft.Y) * pageUnitToInchUnitScale + 1), (float)(int)(BandHeadWidth), (float)(int)(defaultDPI * ProfileMapHight * pageUnitToInchUnitScale)); graph.DrawRectangle(defaultPen, ChartheadRect); //draw head text rF = new RectangleF(0, 0, (float)ChartheadRect.Height, (float)BandHeadWidth); graph.SaveGraphicsState(); // head box 的左下角 graph.TranslateTransform((float)(ChartheadRect.Left), (float)(ChartheadRect.Bottom)); graph.RotateTransform(270); graph.DrawString("剖面图", textfont, new SolidBrush(Color.Black), rF, psf); graph.RestoreGraphicsState(); LastBottomLeftPoint.Y = (int)ChartheadRect.Bottom; //double beginM = -999; double endM = -999; //GetIMUBeginEndMeasure(ref beginM, ref endM); double beginM = BegMeasure; double endM = endMeasure; for (int i = 0; i < listBoxDrawBandFields.Items.Count; i++) { string fieldname = listBoxDrawBandFields.Items[i].ToString(); band b = new band(); b.BandName = fieldname; b.pdfGC = pGraphics; b.BandWidth = (int)bandwidth; b.headFont = labelbiaotou.Font; b.ContentFont = labelNeirong.Font; System.Drawing.Point pt = new System.Drawing.Point(); pt.X = LastBottomLeftPoint.X; pt.Y = LastBottomLeftPoint.Y; b.BandTopLeftLocation = pt; b.BeginM = beginM; b.EndM = endM; //b.bandData = new List<Tuple<double, string>>(); //for (int j = 0; j < 6; j++) //{ // double m = j * 500; // string txt = "异常2"; // Tuple<double, string> t = new Tuple<double, string>(m,txt); // b.bandData.Add(t); //} b.bandData = GetPDFBandData(fieldname); b.Draw(); LastBottomLeftPoint.Y += (int)b.BandHight; } // graph.DrawImage(img, rf); graph.AddToPageForeground(page); // Render a page with graphics. //processor.RenderNewPage(PdfPaperSize.Letter, graph); processor.SaveDocument(outFile); } } break; } pE = pGC.Next(); } #endregion }