public virtual void CreatePdf(String dest) { //Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); PdfDocument sourcePdf = new PdfDocument(new PdfReader(SRC)); //Original page PdfPage origPage = sourcePdf.GetPage(1); //Original page size Rectangle orig = origPage.GetPageSize(); PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf); //N-up page PageSize nUpPageSize = PageSize.A4.Rotate(); PdfPage page = pdf.AddNewPage(nUpPageSize); PdfCanvas canvas = new PdfCanvas(page); //Scale page AffineTransform transformationMatrix = AffineTransform.GetScaleInstance(nUpPageSize.GetWidth() / orig.GetWidth () / 2f, nUpPageSize.GetHeight() / orig.GetHeight() / 2f); canvas.ConcatMatrix(transformationMatrix); //Add pages to N-up page canvas.AddXObject(pageCopy, 0, orig.GetHeight()); canvas.AddXObject(pageCopy, orig.GetWidth(), orig.GetHeight()); canvas.AddXObject(pageCopy, 0, 0); canvas.AddXObject(pageCopy, orig.GetWidth(), 0); pdf.Close(); sourcePdf.Close(); }
private void ApplyUserSpaceScaling(SvgDrawContext context) { if (!this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.MARKER_UNITS) || SvgConstants.Values.STROKEWIDTH .Equals(this.attributesAndStyles.Get(SvgConstants.Attributes.MARKER_UNITS))) { String parentValue = this.GetParent().GetAttribute(SvgConstants.Attributes.STROKE_WIDTH); if (parentValue != null) { float strokeWidthScale; if (CssUtils.IsPercentageValue(parentValue)) { // If stroke width is a percentage value is always computed as a percentage of the normalized viewBox diagonal length. double rootViewPortHeight = context.GetRootViewPort().GetHeight(); double rootViewPortWidth = context.GetRootViewPort().GetWidth(); double viewBoxDiagonalLength = Math.Sqrt(rootViewPortHeight * rootViewPortHeight + rootViewPortWidth * rootViewPortWidth ); strokeWidthScale = CssUtils.ParseRelativeValue(parentValue, (float)viewBoxDiagonalLength); } else { strokeWidthScale = SvgCssUtils.ConvertPtsToPx(ParseFontRelativeOrAbsoluteLengthOnMarker(parentValue)); } context.GetCurrentCanvas().ConcatMatrix(AffineTransform.GetScaleInstance(strokeWidthScale, strokeWidthScale )); } } }
public virtual void CreatePdf(String src, String dest) { //Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); PdfDocument origPdf = new PdfDocument(new PdfReader(src)); //Original page size PdfPage origPage = origPdf.GetPage(1); Rectangle orig = origPage.GetPageSizeWithRotation(); //Add A4 page PdfPage page = pdf.AddNewPage(PageSize.A4.Rotate()); //Shrink original page content using transformation matrix PdfCanvas canvas = new PdfCanvas(page); AffineTransform transformationMatrix = AffineTransform.GetScaleInstance(page.GetPageSize().GetWidth() / orig .GetWidth(), page.GetPageSize().GetHeight() / orig.GetHeight()); canvas.ConcatMatrix(transformationMatrix); PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf); canvas.AddXObject(pageCopy, 0, 0); //Add page with original size pdf.AddPage(origPage.CopyTo(pdf)); //Add A2 page page = pdf.AddNewPage(PageSize.A2.Rotate()); //Scale original page content using transformation matrix canvas = new PdfCanvas(page); transformationMatrix = AffineTransform.GetScaleInstance(page.GetPageSize().GetWidth() / orig.GetWidth(), page .GetPageSize().GetHeight() / orig.GetHeight()); canvas.ConcatMatrix(transformationMatrix); canvas.AddXObject(pageCopy, 0, 0); pdf.Close(); origPdf.Close(); }
public virtual void NormalScaleTest() { AffineTransform expected = AffineTransform.GetScaleInstance(10d, 20d); AffineTransform actual = TransformUtils.ParseTransform("scale(10, 20)"); NUnit.Framework.Assert.AreEqual(expected, actual); }
public virtual void NegativeScaleValuesTest() { AffineTransform expected = AffineTransform.GetScaleInstance(-2, -3); AffineTransform actual = TransformUtils.ParseTransform("scale(-2, -3)"); NUnit.Framework.Assert.AreEqual(expected, actual); }
public static void CreatePdf(string src, string dest, int row, int col) { //Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); PdfDocument sourcePdf = new PdfDocument(new PdfReader(src)); //Original page PdfPage origPage = sourcePdf.GetPage(1); //Original page size Rectangle orig = origPage.GetPageSize(); PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf); //N-up page PageSize nUpPageSize = PageSize.LETTER.Rotate(); PdfPage page = pdf.AddNewPage(nUpPageSize); PdfCanvas canvas = new PdfCanvas(page); //Scale page var width = nUpPageSize.GetWidth(); var height = nUpPageSize.GetHeight(); var origWidth = orig.GetWidth(); var origHeight = orig.GetHeight(); var transformationMatrix = AffineTransform.GetScaleInstance(width / origWidth / col, height / origHeight / row); canvas.ConcatMatrix(transformationMatrix); //Add pages to N-up page for (var i = 0; i < col; i++) { for (var j = 0; j < row; j++) { canvas.AddXObject(pageCopy, i * origWidth, j * origHeight); } } pdf.Close(); sourcePdf.Close(); }
// transformation already happened in AbstractSvgNodeRenderer, so no need to do a transformation here /// <summary>Applies a transformation based on a viewBox for a given branch node.</summary> /// <param name="context">current svg draw context</param> internal virtual void ApplyViewBox(SvgDrawContext context) { if (this.attributesAndStyles != null && this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.VIEWBOX )) { //Parse aspect ratio related stuff String viewBoxValues = attributesAndStyles.Get(SvgConstants.Attributes.VIEWBOX); IList <String> valueStrings = SvgCssUtils.SplitValueList(viewBoxValues); float[] values = new float[valueStrings.Count]; for (int i = 0; i < values.Length; i++) { values[i] = CssUtils.ParseAbsoluteLength(valueStrings[i]); } Rectangle currentViewPort = context.GetCurrentViewPort(); String[] alignAndMeet = RetrieveAlignAndMeet(); String align = alignAndMeet[0]; String meetOrSlice = alignAndMeet[1]; float scaleWidth = currentViewPort.GetWidth() / values[2]; float scaleHeight = currentViewPort.GetHeight() / values[3]; bool forceUniformScaling = !(SvgConstants.Values.NONE.Equals(align)); if (forceUniformScaling) { //Scaling should preserve aspect ratio if (SvgConstants.Values.MEET.Equals(meetOrSlice)) { scaleWidth = Math.Min(scaleWidth, scaleHeight); } else { scaleWidth = Math.Max(scaleWidth, scaleHeight); } scaleHeight = scaleWidth; } AffineTransform scale = AffineTransform.GetScaleInstance(scaleWidth, scaleHeight); float[] scaledViewBoxValues = ScaleViewBoxValues(values, scaleWidth, scaleHeight); AffineTransform transform = ProcessAspectRatioPosition(context, scaledViewBoxValues, align, scaleWidth, scaleHeight ); if (!scale.IsIdentity()) { context.GetCurrentCanvas().ConcatMatrix(scale); //Inverse scaling needs to be applied to viewport dimensions context.GetCurrentViewPort().SetWidth(currentViewPort.GetWidth() / scaleWidth).SetX(currentViewPort.GetX() / scaleWidth).SetHeight(currentViewPort.GetHeight() / scaleHeight).SetY(currentViewPort.GetY() / scaleHeight ); } if (!transform.IsIdentity()) { context.GetCurrentCanvas().ConcatMatrix(transform); //Apply inverse translation to viewport to make it line up nicely context.GetCurrentViewPort().SetX(currentViewPort.GetX() + -1 * (float)transform.GetTranslateX()).SetY(currentViewPort .GetY() + -1 * (float)transform.GetTranslateY()); } } }
/// <summary>Creates a scale transformation.</summary> /// <param name="values">values of the transformation</param> /// <returns>AffineTransform for the scale operation</returns> private static AffineTransform CreateScaleTransformation(IList <String> values) { if (values.Count == 0 || values.Count > 2) { throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES); } float scaleX = CssUtils.ParseRelativeValue(values[0], 1); float scaleY = values.Count == 2 ? CssUtils.ParseRelativeValue(values[1], 1) : scaleX; return(AffineTransform.GetScaleInstance(scaleX, scaleY)); }
/// <summary> /// Creates a RenderedImage instance of this image with width w, and /// height h in pixels. The RenderContext is built automatically /// with an appropriate usr2dev transform and an area of interest /// of the full image. All the rendering hints come from hints /// passed in. /// /// <para> If w == 0, it will be taken to equal /// Math.round(h*(getWidth()/getHeight())). /// Similarly, if h == 0, it will be taken to equal /// Math.round(w*(getHeight()/getWidth())). One of /// w or h must be non-zero or else an IllegalArgumentException /// will be thrown. /// /// </para> /// <para> The created RenderedImage may have a property identified /// by the String HINTS_OBSERVED to indicate which RenderingHints /// were used to create the image. In addition any RenderedImages /// that are obtained via the getSources() method on the created /// RenderedImage may have such a property. /// /// </para> /// </summary> /// <param name="w"> the width of rendered image in pixels, or 0. </param> /// <param name="h"> the height of rendered image in pixels, or 0. </param> /// <param name="hints"> a RenderingHints object containing hints. </param> /// <returns> a RenderedImage containing the rendered data. </returns> public virtual RenderedImage CreateScaledRendering(int w, int h, RenderingHints hints) { // DSR -- code to try to get a unit scale double sx = (double)w / Width; double sy = (double)h / Height; if (System.Math.Abs(sx / sy - 1.0) < 0.01) { sx = sy; } AffineTransform usr2dev = AffineTransform.GetScaleInstance(sx, sy); RenderContext newRC = new RenderContext(usr2dev, hints); return(CreateRendering(newRC)); }
private byte[] CreatePDF(string html) { byte[] binData; using (var workStream = new MemoryStream()) { var pdfWriter = new PdfWriter(workStream); var pdf = new PdfDocument(pdfWriter); PdfDocument sourcePdf = new PdfDocument(new PdfReader(new MemoryStream(CreateDocument(html)))); //Original page PdfPage origPage = sourcePdf.GetPage(1); PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf); //Original page size Rectangle orig = origPage.GetPageSizeWithRotation(); //Просто сжимание в A4 Rectangle tileSize = PageSize.A4.Rotate(); // Transformation matrix//30 пунктов - это 20px справа и слева, 40 * 0,75 AffineTransform transformationMatrix = new AffineTransform(); if (tileSize.GetHeight() / orig.GetHeight() > 0.5) { transformationMatrix = AffineTransform.GetScaleInstance((tileSize.GetWidth() - 30) / orig.GetWidth(), 0.5); } else { transformationMatrix = AffineTransform.GetScaleInstance((tileSize.GetWidth() - 30) / orig.GetWidth(), (tileSize.GetHeight() - 30) / orig.GetHeight()); } PdfPage transformPage = pdf.AddNewPage(new PageSize(tileSize)); PdfCanvas transformCanvas = new PdfCanvas(transformPage); transformCanvas.ConcatMatrix(transformationMatrix); if (tileSize.GetHeight() / orig.GetHeight() > 0.5) { //20 пунктов сверху отступ transformCanvas.AddXObject(pageCopy, 30, tileSize.GetHeight() + (tileSize.GetHeight() - orig.GetHeight()) - 20); } else { transformCanvas.AddXObject(pageCopy, 30, 20); } pdf.Close(); pdfWriter.Close(); binData = workStream.ToArray(); } return(binData); }
internal virtual void CalculateAndApplyViewBox(SvgDrawContext context, float[] values, Rectangle currentViewPort ) { String[] alignAndMeet = RetrieveAlignAndMeet(); String align = alignAndMeet[0]; String meetOrSlice = alignAndMeet[1]; float scaleWidth = currentViewPort.GetWidth() / values[2]; float scaleHeight = currentViewPort.GetHeight() / values[3]; bool forceUniformScaling = !(SvgConstants.Values.NONE.Equals(align)); if (forceUniformScaling) { //Scaling should preserve aspect ratio if (SvgConstants.Values.MEET.Equals(meetOrSlice)) { scaleWidth = Math.Min(scaleWidth, scaleHeight); } else { scaleWidth = Math.Max(scaleWidth, scaleHeight); } scaleHeight = scaleWidth; } AffineTransform scale = AffineTransform.GetScaleInstance(scaleWidth, scaleHeight); float[] scaledViewBoxValues = ScaleViewBoxValues(values, scaleWidth, scaleHeight); AffineTransform transform = ProcessAspectRatioPosition(context, scaledViewBoxValues, align, scaleWidth, scaleHeight ); if (!scale.IsIdentity()) { context.GetCurrentCanvas().ConcatMatrix(scale); //Inverse scaling needs to be applied to viewport dimensions context.GetCurrentViewPort().SetWidth(currentViewPort.GetWidth() / scaleWidth).SetX(currentViewPort.GetX() / scaleWidth).SetHeight(currentViewPort.GetHeight() / scaleHeight).SetY(currentViewPort.GetY() / scaleHeight ); } if (!transform.IsIdentity()) { context.GetCurrentCanvas().ConcatMatrix(transform); //Apply inverse translation to viewport to make it line up nicely context.GetCurrentViewPort().SetX(currentViewPort.GetX() + -1 * (float)transform.GetTranslateX()).SetY(currentViewPort .GetY() + -1 * (float)transform.GetTranslateY()); } }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); ImageData image = ImageDataFactory.Create(IMG); // Translation defines the position of the image on the page and scale transformation sets image dimensions // Please also note that image without scaling is drawn in 1x1 rectangle. And here we draw image on page using // its original size in pixels. AffineTransform affineTransform = AffineTransform.GetTranslateInstance(36, 300); // Make sure that the image is visible by concatenating a scale transformation affineTransform.Concatenate(AffineTransform.GetScaleInstance(image.GetWidth(), image.GetHeight())); PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage()); float[] matrix = new float[6]; affineTransform.GetMatrix(matrix); canvas.AddImageWithTransformationMatrix(image, matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]); pdfDoc.Close(); }
private AffineTransform CreateTransform(PdfImportedPage page, int rotation, Rectangle initialPageSize, bool autoResize) { var docWidth = _docContent.Right - _docContent.Left; var docHeight = _docContent.Top - _docContent.Bottom; AffineTransform rotate = AffineTransform.GetRotateInstance((Math.PI * (360 - rotation)) / 180, 0, 0); AffineTransform scale = AffineTransform.GetScaleInstance(docWidth / page.Width, docHeight / page.Height); AffineTransform translateMargin = AffineTransform.GetTranslateInstance(_docContent.LeftMargin - page.BoundingBox.Left + initialPageSize.Left, _docContent.BottomMargin - page.BoundingBox.Bottom + initialPageSize.Bottom); AffineTransform translateRotate = AffineTransform.GetTranslateInstance(0, 0); if (rotation % 180 == 90) { scale = AffineTransform.GetScaleInstance(docWidth / page.Height, docHeight / page.Width); translateRotate = AffineTransform.GetTranslateInstance((rotation % 360 == 270) ? page.Height : 0, (rotation % 360 == 90) ? page.Width : 0); } else if (rotation % 360 == 180) { translateRotate = AffineTransform.GetTranslateInstance(page.Width, page.Height);; } AffineTransform transform = AffineTransform.GetTranslateInstance(0, 0); if (autoResize) { transform.preConcatenate(translateMargin); } transform.preConcatenate(rotate); transform.preConcatenate(translateRotate); if (autoResize) { transform.preConcatenate(scale); } return(transform); }
public virtual void CreatePdf(String src, String dest) { //Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); PdfDocument sourcePdf = new PdfDocument(new PdfReader(src)); //Original page PdfPage origPage = sourcePdf.GetPage(1); PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf); //Original page size Rectangle orig = origPage.GetPageSize(); //Tile size Rectangle tileSize = PageSize.A4.Rotate(); // Transformation matrix AffineTransform transformationMatrix = AffineTransform.GetScaleInstance(tileSize.GetWidth() / orig.GetWidth () * 2f, tileSize.GetHeight() / orig.GetHeight() * 2f); //The first tile PdfPage page = pdf.AddNewPage(PageSize.A4.Rotate()); PdfCanvas canvas = new PdfCanvas(page); canvas.ConcatMatrix(transformationMatrix); canvas.AddXObject(pageCopy, 0, -orig.GetHeight() / 2f); //The second tile page = pdf.AddNewPage(PageSize.A4.Rotate()); canvas = new PdfCanvas(page); canvas.ConcatMatrix(transformationMatrix); canvas.AddXObject(pageCopy, -orig.GetWidth() / 2f, -orig.GetHeight() / 2f); //The third tile page = pdf.AddNewPage(PageSize.A4.Rotate()); canvas = new PdfCanvas(page); canvas.ConcatMatrix(transformationMatrix); canvas.AddXObject(pageCopy, 0, 0); //The fourth tile page = pdf.AddNewPage(PageSize.A4.Rotate()); canvas = new PdfCanvas(page); canvas.ConcatMatrix(transformationMatrix); canvas.AddXObject(pageCopy, -orig.GetWidth() / 2f, 0); pdf.Close(); sourcePdf.Close(); }
// transformation already happened in AbstractSvgNodeRenderer, so no need to do a transformation here /// <summary>Applies a transformation based on a viewBox for a given branch node.</summary> /// <param name="context">current svg draw context</param> private void ApplyViewBox(SvgDrawContext context) { if (this.attributesAndStyles != null) { if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.VIEWBOX)) { //Parse aspect ratio related stuff String viewBoxValues = attributesAndStyles.Get(SvgConstants.Attributes.VIEWBOX); IList <String> valueStrings = SvgCssUtils.SplitValueList(viewBoxValues); float[] values = new float[valueStrings.Count]; for (int i = 0; i < values.Length; i++) { values[i] = CssUtils.ParseAbsoluteLength(valueStrings[i]); } Rectangle currentViewPort = context.GetCurrentViewPort(); float scaleWidth = currentViewPort.GetWidth() / values[2]; float scaleHeight = currentViewPort.GetHeight() / values[3]; AffineTransform scale = AffineTransform.GetScaleInstance(scaleWidth, scaleHeight); if (!scale.IsIdentity()) { context.GetCurrentCanvas().ConcatMatrix(scale); //Inverse scaling needs to be applied to viewport dimensions context.GetCurrentViewPort().SetWidth(currentViewPort.GetWidth() / scaleWidth); context.GetCurrentViewPort().SetX(currentViewPort.GetX() / scaleWidth); context.GetCurrentViewPort().SetHeight(currentViewPort.GetHeight() / scaleHeight); context.GetCurrentViewPort().SetY(currentViewPort.GetY() / scaleHeight); } AffineTransform transform = ProcessAspectRatio(context, values); if (!transform.IsIdentity()) { //TODO (RND-876) context.GetCurrentCanvas().WriteLiteral("% applying viewbox aspect ratio correction (not correct) \n"); } } } }
private void createClicked(object sender, EventArgs e) { //Figure out the file name startDate = dateTimePicker1.Value; endDate = startDate.AddDays(7); destFileName = destPath.Text + "/"; destFileName += startDate.ToString("yyyy MMMM d"); destFileName += " - "; if (endDate.Year != startDate.Year) { destFileName += endDate.ToString("yyyy") + " "; } if (endDate.Month != startDate.Month) { destFileName += endDate.ToString("MMMM") + " "; } destFileName += endDate.Day; destFileName += " Murlis"; destFileName += ".pdf"; createButton.Text = "Creating..."; createButton.Refresh(); //Create the pdf writer = new PdfWriter(destFileName); pdf = new PdfDocument(writer); //Loop through each day for (int i = 0; i < 14; i += 2) { //Reset togetherHeight togetherHeight = 0; //Crop the images for (int j = 0; j < 2; j++) { //Rectangle for part of image we want cropRectangle = new System.Drawing.Rectangle(croppedImageX, croppedImageYs[i + j], croppedImageWidth, croppedImageHeights[i + j]); //Get bitmap from already gotten image croppedImage = new Bitmap(images[i + j]); //Crop the image croppedImage = croppedImage.Clone(cropRectangle, croppedImage.PixelFormat); //Add the height onto the togetherHeight togetherHeight += croppedImage.Height; //Convert to ImageData imageDatas[i + j] = ImageDataFactory.Create((byte[])imageConverter.ConvertTo(croppedImage, typeof(byte[]))); } //Add a page and canvas to the pdf page = pdf.AddNewPage(PageSize.A4.Rotate()); canvas = new PdfCanvas(page); //Calculate ratios pageImageWidthRatio = page.GetPageSize().GetWidth() / croppedImage.Width; pageImageHeightRatio = page.GetPageSize().GetHeight() / togetherHeight; smallerRatio = Math.Min(pageImageWidthRatio, pageImageHeightRatio); bottomSpace = page.GetPageSize().GetHeight() - togetherHeight * smallerRatio; //Set transform canvas.ConcatMatrix(AffineTransform.GetScaleInstance(smallerRatio, smallerRatio)); //Add the images onto the canvas canvas.AddImage(imageDatas[i], 0, imageDatas[i + 1].GetHeight() + bottomSpace, true); canvas.AddImage(imageDatas[i + 1], 0, bottomSpace, true); } //Save the pdf pdf.Close(); openButton.Enabled = true; closeButton.Enabled = true; MessageBox.Show("PDF Successfully Created"); createButton.Text = "Create"; }