protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); ImageData image = ImageDataFactory.Create(IMAGE); float width = image.GetWidth(); float height = image.GetHeight(); PageSize pageSize = new PageSize(width / 2, height / 2); pdfDoc.SetDefaultPageSize(pageSize); PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage()); canvas.AddImageWithTransformationMatrix(image, width, 0, 0, height, 0, -height / 2, false); canvas = new PdfCanvas(pdfDoc.AddNewPage()); canvas.AddImageWithTransformationMatrix(image, width, 0, 0, height, 0, 0, false); canvas = new PdfCanvas(pdfDoc.AddNewPage()); canvas.AddImageWithTransformationMatrix(image, width, 0, 0, height, -width / 2, -height / 2, false); canvas = new PdfCanvas(pdfDoc.AddNewPage()); canvas.AddImageWithTransformationMatrix(image, width, 0, 0, height, -width / 2, 0, false); pdfDoc.Close(); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); Document doc = new Document(pdfDoc); PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA); Paragraph paragraph = new Paragraph("My watermark (text)") .SetFont(font) .SetFontSize(30); ImageData img = ImageDataFactory.Create(IMG); float w = img.GetWidth(); float h = img.GetHeight(); PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.5f); // Implement transformation matrix usage in order to scale image for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) { PdfPage pdfPage = pdfDoc.GetPage(i); Rectangle pageSize = pdfPage.GetPageSizeWithRotation(); // When "true": in case the page has a rotation, then new content will be automatically rotated in the // opposite direction. On the rotated page this would look as if new content ignores page rotation. pdfPage.SetIgnorePageRotationForContent(true); float x = (pageSize.GetLeft() + pageSize.GetRight()) / 2; float y = (pageSize.GetTop() + pageSize.GetBottom()) / 2; PdfCanvas over = new PdfCanvas(pdfDoc.GetPage(i)); over.SaveState(); over.SetExtGState(gs1); if (i % 2 == 1) { doc.ShowTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, 0); } else { over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 2), false); } over.RestoreState(); } doc.Close(); }
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(); }