/// <summary> /// Creates a link within the current document. /// </summary> /// <param name="rect">The link area in default page coordinates.</param> /// <param name="destinationPage">The one-based destination page number.</param> public static PdfLinkAnnotation CreateDocumentLink(PdfRectangle rect, int destinationPage) { PdfLinkAnnotation link = new PdfLinkAnnotation(); link.linkType = PdfLinkAnnotation.LinkType.Document; link.Rectangle = rect; link.destPage = destinationPage; return link; }
private void AddFields(PdfRectangle rect) { float offset = (float)XUnit.FromPoint(rect.X1).Millimeter; XPdfFontOptions o = new XPdfFontOptions(PdfFontEncoding.Unicode); XFont font = new XFont("Times New Roman", 14, XFontStyle.Regular, o); gfx.DrawString("Бланк фиксации результатов теста", font, XBrushes.Black, new XRect(rect.X1, rect.Y1+XUnit.FromMillimeter(17).Point, rect.X2-rect.X1, 5), XStringFormats.Center); this.DrawLeftString("Подразделение_______________", 10 + offset, 25, 60, 10); this.DrawLeftString("Ф.И.О._____________________________________", 70 + offset, 25, 90, 10); this.DrawLeftString("Дата рождения_______________", 10 + offset, 30, 60, 10); this.DrawLeftString("Образование_________________", 10 + offset, 35, 30, 10); this.DrawLeftString("Дата тестирования__________________________", 70 + offset, 35, 30, 10); this.DrawLeftString("M", 10 + offset, 40, 5, 14); this.DrawRectangle(4, 17 + offset, 42); this.DrawLeftString("Ж", 20 + offset, 40, 5, 14); this.DrawRectangle(4, 27 + offset, 42); this.DrawLeftString("Пример заполнения бланка", 40 + offset, 40, 50, 10); this.DrawRectangle(4, 85 + offset, 42); this.DrawLeftString("Х", 83 + offset, 40, 5, 14); this.DrawLine(0, (int)_page.Width.Millimeter, 45); }
public void Create(string filename, int qcount, int cellsinq, List<string> cellslabels, int rowscount) { s_document = new PdfDocument(); _page = new PdfPage(); _page.Orientation = PageOrientation.Landscape; _page.Size = PageSize.A4; s_document.Pages.Add(_page); gfx = XGraphics.FromPdfPage(_page); DrawCutLine(); PdfRectangle leftpage = new PdfRectangle(new XPoint(0, 0), new XPoint(_page.Width / 2, _page.Height)); PdfRectangle rightpage = new PdfRectangle(new XPoint(_page.Width / 2, 0), new XPoint(_page.Width, _page.Height)); //left part float leftcenter = (float)(_page.Width / 4) * 1f; DrawCenterMarker(7, leftcenter); DrawSideMarker(7, 10, 10); DrawSideMarker(7, XUnit.FromPoint(leftpage.Width).Millimeter-10, 10); DrawSideMarker(7, 10, XUnit.FromPoint(leftpage.Height).Millimeter - 10); DrawSideMarker(7, XUnit.FromPoint(leftpage.Width).Millimeter - 10, XUnit.FromPoint(leftpage.Height).Millimeter - 10); AddFields(leftpage); AddQuestions(qcount, cellsinq, cellslabels, rowscount, leftpage); //right part float rigthcenter = (float)(_page.Width / 4) * 3f; DrawCenterMarker(7, rigthcenter); DrawSideMarker(7, XUnit.FromPoint(rightpage.X1).Millimeter+10, XUnit.FromPoint(rightpage.Y1).Millimeter+10); DrawSideMarker(7, XUnit.FromPoint(rightpage.X2).Millimeter - 10, XUnit.FromPoint(rightpage.Y1).Millimeter + 10); DrawSideMarker(7, XUnit.FromPoint(rightpage.X1).Millimeter + 10, XUnit.FromPoint(rightpage.Y2).Millimeter - 10); DrawSideMarker(7, XUnit.FromPoint(rightpage.X2).Millimeter - 10, XUnit.FromPoint(rightpage.Y2).Millimeter - 10); AddFields(rightpage); AddQuestions(qcount, cellsinq, cellslabels, rowscount, rightpage); // Save the s_document... s_document.Save(filename); // ...and start a viewer Process.Start(filename); }
/// <summary> /// Implements cloning this instance. /// </summary> protected override object Copy() { PdfRectangle rect = (PdfRectangle)base.Copy(); return(rect); }
/// <summary> /// Determines if the rectangular region represented by rect is entirely contained within this PdfRectangle. /// </summary> public bool Contains(PdfRectangle rect) { return(_x1 <= rect._x1 && rect._x2 <= _x2 && _y1 <= rect._y1 && rect._y2 <= _y2); }
/// <summary> /// Determines if the rectangular region represented by rect is entirely contained within this PdfRectangle. /// </summary> public bool Contains(PdfRectangle rect) { return(x1 <= rect.x1 && rect.x2 <= x2 && y1 <= rect.y1 && rect.y2 <= y2); }
/// <summary> /// Determines if the rectangular region represented by rect is entirely contained within this PdfRectangle. /// </summary> public bool Contains(PdfRectangle rect) { return _x1 <= rect._x1 && rect._x2 <= _x2 && _y1 <= rect._y1 && rect._y2 <= _y2; }
public void Write(PdfRectangle rect) { WriteSeparator(CharCat.Delimiter, '/'); WriteRaw(PdfEncoders.Format("[{0:0.###} {1:0.###} {2:0.###} {3:0.###}]", rect.X1, rect.Y1, rect.X2, rect.Y2)); this.lastCat = CharCat.Delimiter; }
/// <summary> /// Adds a link to a file. /// </summary> /// <param name="rect">The rect.</param> /// <param name="fileName">Name of the file.</param> public PdfLinkAnnotation AddFileLink(PdfRectangle rect, string fileName) { PdfLinkAnnotation annotation = PdfLinkAnnotation.CreateFileLink(rect, fileName); Annotations.Add(annotation); return annotation; }
internal override void WriteObject(PdfWriter writer) { // HACK: temporarily flip media box if Landscape PdfRectangle mediaBox = MediaBox; // TODO: Take /Rotate into account if (_orientation == PageOrientation.Landscape) MediaBox = new PdfRectangle(mediaBox.X1, mediaBox.Y1, mediaBox.Y2, mediaBox.X2); #if true // Add transparency group to prevent rendering problems of Adobe viewer. // Update (PDFsharp 1.50 beta 3): Add transparency group only of ColorMode is defined. // Rgb is the default for the ColorMode, but if user sets it to Undefined then // we respect this and skip the transparency group. TransparencyUsed = true; // TODO: check XObjects if (TransparencyUsed && !Elements.ContainsKey(Keys.Group) && _document.Options.ColorMode != PdfColorMode.Undefined) { PdfDictionary group = new PdfDictionary(); _elements["/Group"] = group; if (_document.Options.ColorMode != PdfColorMode.Cmyk) group.Elements.SetName("/CS", "/DeviceRGB"); else group.Elements.SetName("/CS", "/DeviceCMYK"); group.Elements.SetName("/S", "/Transparency"); //False is default: group.Elements["/I"] = new PdfBoolean(false); //False is default: group.Elements["/K"] = new PdfBoolean(false); } #endif #if DEBUG_ PdfItem item = Elements["/MediaBox"]; if (item != null) item.GetType(); #endif base.WriteObject(writer); if (_orientation == PageOrientation.Landscape) MediaBox = mediaBox; }
/// <summary> /// Adds an intra document link. /// </summary> /// <param name="rect">The rect.</param> /// <param name="destinationPage">The destination page.</param> public PdfLinkAnnotation AddDocumentLink(PdfRectangle rect, int destinationPage) { PdfLinkAnnotation annotation = PdfLinkAnnotation.CreateDocumentLink(rect, destinationPage); Annotations.Add(annotation); return annotation; }
/// <summary> /// Adds a link to the Web. /// </summary> /// <param name="rect">The rect.</param> /// <param name="url">The URL.</param> public PdfLinkAnnotation AddWebLink(PdfRectangle rect, string url) { PdfLinkAnnotation annotation = PdfLinkAnnotation.CreateWebLink(rect, url); Annotations.Add(annotation); return annotation; }
/// <summary> /// Converts the specified value to PdfRectangle. /// If the value not exists, the function returns an empty rectangle. /// If the value is not convertible, the function throws an InvalidCastException. /// </summary> public PdfRectangle GetRectangle(string key, bool create) { PdfRectangle value = new PdfRectangle(); object obj = this[key]; if (obj == null) { if (create) this[key] = value = new PdfRectangle(); return value; } if (obj is PdfReference) obj = ((PdfReference)obj).Value; PdfArray array = obj as PdfArray; if (array != null && array.Elements.Count == 4) { value = new PdfRectangle(array.Elements.GetReal(0), array.Elements.GetReal(1), array.Elements.GetReal(2), array.Elements.GetReal(3)); this[key] = value; } else value = (PdfRectangle)obj; return value; }
/// <summary> /// Sets the entry to a direct rectangle value, represented by an array with four values. /// </summary> public void SetRectangle(string key, PdfRectangle rect) { this.elements[key] = rect; }
/// <summary> /// Initializes a new instance of the XForm class such that it can be drawn on the specifed graphics /// object. /// </summary> /// <param name="gfx">The graphics object that later is used to draw this form.</param> /// <param name="size">The size in points of the form.</param> public XForm(XGraphics gfx, XSize size) { if (gfx == null) throw new ArgumentNullException("gfx"); if (size.width < 1 || size.height < 1) throw new ArgumentNullException("size", "The size of the XPdfForm is to small."); this.formState = FormState.Created; //this.templateSize = size; this.viewBox.width = size.width; this.viewBox.height = size.height; // If gfx belongs to a PdfPage also create the PdfFormXObject if (gfx.PdfPage != null) { this.document = gfx.PdfPage.Owner; this.pdfForm = new PdfFormXObject(this.document, this); PdfRectangle rect = new PdfRectangle(new XPoint(), size); this.pdfForm.Elements.SetRectangle(PdfFormXObject.Keys.BBox, rect); } }
private void AddQuestions(int qcount, int cellsinq, List<string> cellslabels, int rowscnt,PdfRectangle rect) { int rowhight = 7; int rowscount = rowscnt;//Convert.ToInt32((_page.Height.Millimeter - 80d) / 7d); int colscount = qcount / rowscount; int mincolwidth = cellsinq * (4 + 3); // int colsinterval = Convert.ToInt32(((_page.Width.Millimeter - 40d) - (mincolwidth * colscount)) / colscount); int colsinterval = Convert.ToInt32(((XUnit.FromPoint(rect.X2-rect.X1).Millimeter - 40d) - (mincolwidth * colscount)) / colscount); colsinterval = colsinterval < 8 ? 8 : colsinterval; int colwidgth = cellsinq * (4 + 3) + colsinterval; int maxy = 0; int initx = 15 + (int)XUnit.FromPoint(rect.X1).Millimeter; int inity = 55; int x = initx; int number = 1; int rr = 0; int y = inity; for (int question = 0; question < qcount; question++) { if (rr != 0) { DrawQuestion(number, cellsinq, x, y); } else { DrawQuestion(number, cellsinq, x, y, cellslabels); } number++; rr++; y = y + rowhight; if (y > maxy) { maxy = y; } if (rr == rowscount) { rr = 0; x += colwidgth; y = inity; } } this.DrawLine(0, (float)_page.Width.Millimeter, maxy); this.DrawLeftString("Добровольность тестирования и достоверность результатов подтверждаю:_______________", initx-10, maxy + 1, 60, 10); }
/// <summary> /// Creates a link to the web. /// </summary> public static PdfLinkAnnotation CreateWebLink(PdfRectangle rect, string url) { PdfLinkAnnotation link = new PdfLinkAnnotation(); link.linkType = PdfLinkAnnotation.LinkType.Web; link.Rectangle = rect; link.url = url; return link; }
/// <summary> /// Initializes a new instance of the <see cref="XForm"/> class that represents a page of a PDF document. /// </summary> /// <param name="document">The PDF document.</param> /// <param name="viewBox">The view box of the page.</param> public XForm(PdfDocument document, XRect viewBox) { if (viewBox.width < 1 || viewBox.height < 1) throw new ArgumentNullException("viewBox", "The size of the XPdfForm is to small."); // I must tie the XPdfForm to a document immediately, because otherwise I would have no place where // to store the resources. if (document == null) throw new ArgumentNullException("document", "An XPdfForm template must be associated with a document at creation time."); this.formState = FormState.Created; this.document = document; this.pdfForm = new PdfFormXObject(document, this); //this.templateSize = size; this.viewBox = viewBox; PdfRectangle rect = new PdfRectangle(viewBox); this.pdfForm.Elements.SetRectangle(PdfFormXObject.Keys.BBox, rect); }
/// <summary> /// Creates a link to a file. /// </summary> public static PdfLinkAnnotation CreateFileLink(PdfRectangle rect, string fileName) { PdfLinkAnnotation link = new PdfLinkAnnotation(); link.linkType = PdfLinkAnnotation.LinkType.File; link.Rectangle = rect; link.url = fileName; return link; }
/// <summary> /// Creates a link to a file. /// </summary> public static PdfLinkAnnotation CreateFileLink(PdfRectangle rect, string fileName) { PdfLinkAnnotation link = new PdfLinkAnnotation(); link.linkType = PdfLinkAnnotation.LinkType.File; // TODO: Adjust bleed box here (if possible) link.Rectangle = rect; link.url = fileName; return link; }
/// <summary> /// Determines if the rectangular region represented by rect is entirely contained within this PdfRectangle. /// </summary> public bool Contains(PdfRectangle rect) { return this.x1 <= rect.x1 && rect.x2 <= this.x2 && this.y1 <= rect.y1 && rect.y2 <= this.y2; }
internal override void PrepareForSave() { if (_trimMargins.AreSet) { // These are the values InDesign set for an A4 page with 3mm crop margin at each edge. // (recall that PDF rect are two points and NOT a point and a width) // /MediaBox[0.0 0.0 612.283 858.898] 216 302.7 // /CropBox[0.0 0.0 612.283 858.898] // /BleedBox[0.0 0.0 612.283 858.898] // /ArtBox[8.50394 8.50394 603.78 850.394] 3 3 213 300 // /TrimBox[8.50394 8.50394 603.78 850.394] double width = _trimMargins.Left.Point + Width.Point + _trimMargins.Right.Point; double height = _trimMargins.Top.Point + Height.Point + _trimMargins.Bottom.Point; MediaBox = new PdfRectangle(0, 0, width, height); CropBox = new PdfRectangle(0, 0, width, height); BleedBox = new PdfRectangle(0, 0, width, height); PdfRectangle rect = new PdfRectangle(_trimMargins.Left.Point, _trimMargins.Top.Point, width - _trimMargins.Right.Point, height - _trimMargins.Bottom.Point); TrimBox = rect; ArtBox = rect.Clone(); } }
internal override void WriteObject(PdfWriter writer) { // HACK: temporarily flip media box if Landscape PdfRectangle mediaBox = MediaBox; // TODO: Take /Rotate into account if (this.orientation == PageOrientation.Landscape) MediaBox = new PdfRectangle(mediaBox.X1, mediaBox.Y1, mediaBox.Y2, mediaBox.X2); #if true // Add transparency group to prevent rendering problems of Adobe viewer this.transparencyUsed = true; // TODO: check XObjects if (this.transparencyUsed && !Elements.Contains(Keys.Group)) { PdfDictionary group = new PdfDictionary(); this.elements["/Group"] = group; if (this.document.Options.ColorMode != PdfColorMode.Cmyk) group.Elements.SetName("/CS", "/DeviceRGB"); else group.Elements.SetName("/CS", "/DeviceCMYK"); group.Elements.SetName("/S", "/Transparency"); group.Elements["/I"] = new PdfBoolean(false); group.Elements["/K"] = new PdfBoolean(false); } #endif #if DEBUG_ PdfItem item = Elements["/MediaBox"]; if (item != null) item.GetType(); #endif base.WriteObject(writer); if (this.orientation == PageOrientation.Landscape) MediaBox = mediaBox; }
/// <summary> /// Save the PDF document(s) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".pdf"; dlg.Filter = "PDF documents (.pdf)|*.pdf"; dlg.RestoreDirectory = true; if (dlg.ShowDialog() == true) { string filename = dlg.FileName; string outPath = System.IO.Path.GetDirectoryName(filename); for (int i = 0; i < configuration.distributors.Count; i++) { PdfDocument document = CompatiblePdfReader.Open(sourcePDF, PdfDocumentOpenMode.Modify); foreach (PdfPage page in document.Pages) { PdfDictionary resources = page.Elements.GetDictionary("/Resources"); if (resources != null) { PdfDictionary xObjects = resources.Elements.GetDictionary("/XObject"); if (xObjects != null) { ICollection<PdfItem> items = xObjects.Elements.Values; foreach (PdfItem item in items) { PdfReference reference = item as PdfReference; if (reference != null) { PdfDictionary xObject = reference.Value as PdfDictionary; if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image" && xObject.Elements.GetName("/Filter") == "/DCTDecode") { for (int j = 0; j < images.Count; j++) { IStructuralEquatable eqa1 = xObject.Stream.Value; if (images[j].replace && eqa1.Equals(images[j].sourceImg, StructuralComparisons.StructuralEqualityComparer)) { byte[] sourceImage = xObject.Stream.Value; MemoryStream sourceMemStream = new MemoryStream(); sourceMemStream.Write(sourceImage, 0, sourceImage.Length); System.Drawing.Image sourceImageInst = System.Drawing.Image.FromStream(sourceMemStream); // Read image from file if (!File.Exists(configuration.distributors[i].logo)) { MessageBox.Show("Error - I can't find the log file from the config file: " + configuration.distributors[i].logo, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } else { using (FileStream fs = new FileStream(configuration.distributors[i].logo, FileMode.Open, FileAccess.Read)) { BinaryReader br = new BinaryReader(fs); byte[] imageBytes = br.ReadBytes((int)fs.Length); br.Close(); // Create a resized image MemoryStream memStream = new MemoryStream(); memStream.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image = System.Drawing.Image.FromStream(memStream); System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); System.Drawing.Image thumb = image.GetThumbnailImage(sourceImageInst.Width, sourceImageInst.Height, myCallback, IntPtr.Zero); memStream.Close(); // Convert back to a stream from an image object MemoryStream ms = new MemoryStream(); thumb.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); xObject.Stream.Value = ms.ToArray(); ms.Close(); } } sourceMemStream.Close(); } } } } } } } } // Add hyperlinks to the images at the bottom of the first page for (int j = 0; j < configuration.imageLinks.Count; j++) { using (var pdfGfx = XGraphics.FromPdfPage(document.Pages[0])) { XRect rect = new XRect(new PdfSharp.Drawing.XPoint(configuration.imageLinks[j].xStart, configuration.imageLinks[j].yStart), new PdfSharp.Drawing.XPoint(configuration.imageLinks[j].xStart + configuration.imageLinks[j].width, configuration.imageLinks[j].yStart + configuration.imageLinks[j].height)); XRect tRect = pdfGfx.Transformer.WorldToDefaultPage(rect); PdfRectangle rc = new PdfSharp.Pdf.PdfRectangle(tRect); document.Pages[0].AddWebLink(rc, configuration.imageLinks[j].url); if (configuration.imageLinks[j].drawRect) { XRect rect1 = new XRect(new PdfSharp.Drawing.XPoint(configuration.imageLinks[j].xStart, configuration.imageLinks[j].yStart), new PdfSharp.Drawing.XPoint(configuration.imageLinks[j].xStart + configuration.imageLinks[j].width, configuration.imageLinks[j].yStart + configuration.imageLinks[j].height)); pdfGfx.DrawRectangle(XBrushes.Pink, rect1); } } } // Add contact information to the first page XFont nameFont = new XFont("Times New Roman", 12, XFontStyle.Bold); XFont contactFont = new XFont("Times New Roman", 10, XFontStyle.Regular); int targetCenter = 400; using (var pdfGfx = XGraphics.FromPdfPage(document.Pages[0])) { XSize nameStringSize = pdfGfx.MeasureString(configuration.distributors[i].contactName, nameFont); XSize contactStringSize1 = pdfGfx.MeasureString(configuration.distributors[i].contactContact1, contactFont); XSize contactStringSize2 = pdfGfx.MeasureString(configuration.distributors[i].contactContact2, contactFont); int nameStart = targetCenter - (int)Math.Round((double)nameStringSize.Width / 2); int contactStart1 = targetCenter - (int)Math.Round((double)contactStringSize1.Width / 2); int contactStart2 = targetCenter - (int)Math.Round((double)contactStringSize2.Width / 2); //XBrush fontBrush = (XBrush)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("PdfSharp.Drawing.XBrushes." + configuration.contactColor); XBrush fontBrush = new XSolidBrush(XColor.FromName(configuration.contactColor)); pdfGfx.DrawString(configuration.distributors[i].contactName, nameFont, fontBrush, nameStart, 150 + nameStringSize.Height * 0.6, XStringFormats.Default); pdfGfx.DrawString(configuration.distributors[i].contactContact1, contactFont, fontBrush, contactStart1, 162 + contactStringSize1.Height * 0.6, XStringFormats.Default); pdfGfx.DrawString(configuration.distributors[i].contactContact2, contactFont, fontBrush, contactStart2, 174 + contactStringSize2.Height * 0.6, XStringFormats.Default); } document.Save(filename.Replace(".pdf", "") + " - " + configuration.distributors[i].name + ".pdf"); } MessageBox.Show("PDF file(s) successfully saved!", "Success", MessageBoxButton.OK, MessageBoxImage.Information); } }
/// <summary> /// Builds a PdfFormXObject from the specified brush. /// </summary> PdfFormXObject BuildForm(VisualBrush brush) { //<< // /BBox [0 100 100 0] // /Length 65 // /Matrix [1 0 0 1 0 0] // /Resources // << // /ColorSpace // << // /CS0 15 0 R // >> // /ExtGState // << // /GS0 10 0 R // >> // /ProcSet [/PDF /ImageC /ImageI] // /XObject // << // /Im0 16 0 R // >> // >> // /Subtype /Form //>> //stream // q // 0 0 100 100 re // W n // q // /GS0 gs // 100 0 0 -100 0 100 cm // /Im0 Do // Q //Q //endstream PdfFormXObject pdfForm = Context.PdfDocument.Internals.CreateIndirectObject<PdfFormXObject>(); pdfForm.DpiX = 96; pdfForm.DpiY = 96; // view box var box = new PdfRectangle(brush.Viewbox.X, brush.Viewbox.Y + brush.Viewbox.Height - 1, brush.Viewbox.X + brush.Viewbox.Width - 1, brush.Viewbox.Y); pdfForm.Elements.SetRectangle(PdfFormXObject.Keys.BBox, box); pdfForm.Elements.SetMatrix(PdfFormXObject.Keys.Matrix, new XMatrix()); PdfContentWriter writer = new PdfContentWriter(Context, pdfForm); pdfForm.Elements.SetMatrix(PdfFormXObject.Keys.Matrix, new XMatrix()); writer.BeginContentRaw(); writer.WriteLiteral("-100 Tz\n"); writer.WriteLiteral("q\n"); writer.WriteVisual(brush.Visual); writer.WriteLiteral("Q\n"); #if DEBUG if (DevHelper.BorderPatterns) writer.WriteLiteral("1 1 1 rg 0 0 m {0:0.###} 0 l {0:0.###} {1:0.###} l 0 {1:0.###} l h s\n", brush.Viewbox.Width, brush.Viewbox.Height); #endif writer.EndContent(); return pdfForm; }