/// <summary> /// The below method is an example from https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/AddWatermarkText.java?revision=1873147&view=markup /// </summary> /// <param name="doc"></param> /// <param name="page"></param> /// <param name="font"></param> /// <param name="text"></param> static void addWatermarkText(PDDocument doc, PDPage page, PDFont font, string text) { using (PDPageContentStream cs = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true)) { float fontHeight = 100; // arbitrary for short text float width = page.getMediaBox().getWidth(); float height = page.getMediaBox().getHeight(); float stringWidth = font.getStringWidth(text) / 1000 * fontHeight; float diagonalLength = (float)System.Math.Sqrt(width * width + height * height); float angle = (float)System.Math.Atan2(height, width); float x = (diagonalLength - stringWidth) / 2; // "horizontal" position in rotated world float y = -fontHeight / 4; // 4 is a trial-and-error thing, this lowers the text a bit cs.transform(Matrix.getRotateInstance(angle, 0, 0)); cs.setFont(font, fontHeight); // cs.setRenderingMode(RenderingMode.STROKE) // for "hollow" effect PDExtendedGraphicsState gs = new PDExtendedGraphicsState(); gs.setNonStrokingAlphaConstant(new Float(0.2f)); gs.setStrokingAlphaConstant(new Float(0.2f)); gs.setBlendMode(BlendMode.MULTIPLY); gs.setLineWidth(new Float(3f)); cs.setGraphicsStateParameters(gs); cs.setNonStrokingColor(Color.red); cs.setStrokingColor(Color.red); cs.beginText(); cs.newLineAtOffset(x, y); cs.showText(text); cs.endText(); } }
internal static PDDocument AddTrialStampIfNecessary(PDDocument pdfFile, bool addJavaScript) { if (PDFHelper.AddStamp) { PDFont pDFont = PDTrueTypeFont.load(pdfFile, new java.io.File("C:\\Windows\\Fonts\\Arial.ttf"), new WinAnsiEncoding()); foreach (PDPage page in pdfFile.getPages()) { PDPageContentStream pDPageContentStream = new PDPageContentStream(pdfFile, page, PDPageContentStream.AppendMode.APPEND, true); pDPageContentStream.setFont(pDFont, 14f); pDPageContentStream.beginText(); pDPageContentStream.newLineAtOffset(100f, page.getMediaBox().getHeight() - 100f); pDPageContentStream.showText("Created with a trial copy of Aquaforest PDF Toolkit"); pDPageContentStream.endText(); pDPageContentStream.close(); } } return(pdfFile); }
private void DrawTable() { PDPage pDPage; PDDocument pDDocument = new PDDocument(); pDPage = (this.pageSettings.size != null ? new PDPage(this.pageSettings.size.PDFBoxRectangle) : new PDPage()); PDRectangle mediaBox = pDPage.getMediaBox(); int num = this.contents.Count <List <string> >(); int num1 = this.contents.ElementAt <List <string> >(0).Count <string>(); float width = mediaBox.getWidth() - (this.pageSettings.marginLeft + this.pageSettings.marginRight); float single = width / (float)num1; float rowHeight = this.tableSettings.RowHeight; float height = mediaBox.getHeight() - this.pageSettings.marginTop; float single1 = height - this.pageSettings.marginBottom; float single2 = rowHeight * (float)num; int num2 = num; int num3 = 1; int num4 = 0; if (single2 > single1) { num2 = (int)Math.Floor((double)(single1 / rowHeight)); num3 = (int)Math.Ceiling((double)num / (double)num2); single2 = (float)num2 * rowHeight; num4 = num % num2; } int num5 = 0; for (int i = 0; i < num3; i++) { pDPage = (this.pageSettings.size != null ? new PDPage(this.pageSettings.size.PDFBoxRectangle) : new PDPage()); pDDocument.addPage(pDPage); PDPageContentStream pDPageContentStream = new PDPageContentStream(pDDocument, pDPage); if ((i != num3 - 1 ? false : num4 > 0)) { num2 = num4; single2 = rowHeight * (float)num2; } float single3 = height; for (int j = 0; j <= num2; j++) { pDPageContentStream.drawLine(this.pageSettings.marginLeft, single3, this.pageSettings.marginLeft + width, single3); single3 = single3 - rowHeight; } float single4 = this.pageSettings.marginLeft; for (int k = 0; k <= num1; k++) { pDPageContentStream.drawLine(single4, height, single4, height - single2); single4 = single4 + single; } pDPageContentStream.setFont(this.pageSettings.Font.PDFBoxFont, this.pageSettings.FontSize); float cellMargin = this.pageSettings.marginLeft + this.tableSettings.CellMargin; float single5 = height - 15f; for (int l = 0; l < num2; l++) { foreach (string item in this.contents[num5]) { pDPageContentStream.beginText(); pDPageContentStream.newLineAtOffset(cellMargin, single5); pDPageContentStream.showText(item); pDPageContentStream.endText(); cellMargin = cellMargin + single; } single5 = single5 - rowHeight; cellMargin = this.pageSettings.marginLeft + this.tableSettings.CellMargin; num5++; } pDPageContentStream.close(); } pDDocument = PDFHelper.AddTrialStampIfNecessary(pDDocument); pDDocument.save(this.outputPDF); }