/// <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 string GetText(PDDocument pdfDocument, int pageNumber, bool format) { string text; double num; string str = ""; if (format) { try { PDFHelper.DisplayTrialPopupIfNecessary(); PDFTextStripperByArea pDFTextStripperByArea = new PDFTextStripperByArea(); pDFTextStripperByArea.setSortByPosition(true); if (PDFHelper.AddStamp) { str = string.Concat(str, "You are using a trial license of PDF Toolkit, as a result only the first three pages would be extracted."); } PDPage page = pdfDocument.getPage(pageNumber - 1); PDRectangle cropBox = page.getCropBox(); int rotation = page.getRotation(); if (cropBox == null) { cropBox = page.getMediaBox(); } int num1 = 0; int num2 = 0; if (rotation % 180 != 0) { num = Math.Round((double)cropBox.getWidth()); num1 = int.Parse(num.ToString()); num = Math.Round((double)cropBox.getHeight()); num2 = int.Parse(num.ToString()); } else { num = Math.Round((double)cropBox.getHeight()); num1 = int.Parse(num.ToString()); num = Math.Round((double)cropBox.getWidth()); num2 = int.Parse(num.ToString()); } pDFTextStripperByArea.addRegion("class1", new java.awt.Rectangle(0, 0, num2, num1)); pDFTextStripperByArea.extractRegions(page); str = string.Concat(str, pDFTextStripperByArea.getTextForRegion("class1")); } catch (Exception exception1) { Exception exception = exception1; throw new PDFToolkitException(exception.Message, exception.InnerException); } text = str; } else { text = this.GetText(pdfDocument, pageNumber); } return(text); }
internal void createPDFFromText(string pdfFile) { string str; bool flag; PDDocument pDDocument = new PDDocument(); PDFHelper.DisplayTrialPopupIfNecessary(); PDFHelper.CheckOutputFolder(Path.GetDirectoryName(pdfFile)); try { this.pdfFileName = pdfFile; Reader fileReader = new FileReader(this.textFile); int num = 40; if (this.font == null) { this.font = PDFType1Font.TIMES_ROMAN; } float height = this.font.PDFBoxFont.getBoundingBox().getHeight() / 1000f; PDRectangle lETTER = PDRectangle.LETTER; if (this.isLandescape) { lETTER = new PDRectangle(lETTER.getHeight(), lETTER.getWidth()); } height = height * this.fontSize * 1.05f; string[] strArrays = System.IO.File.ReadAllLines(this.textFile); PDPage pDPage = new PDPage(lETTER); PDPageContentStream pDPageContentStream = null; float single = -1f; float width = pDPage.getMediaBox().getWidth() - (float)(2 * num); bool flag1 = true; string[] strArrays1 = strArrays; for (int i = 0; i < (int)strArrays1.Length; i++) { string str1 = strArrays1[i]; flag1 = false; string str2 = Regex.Replace(str1, "\\t|\\n|\\r", ""); string[] strArrays2 = str2.Replace("[\\n\\r]+$", "").Split(new char[] { ' ' }); int num1 = 0; while (num1 < (int)strArrays2.Length) { StringBuilder stringBuilder = new StringBuilder(); float stringWidth = 0f; bool flag2 = false; do { string str3 = ""; int num2 = strArrays2[num1].IndexOf('\f'); if (num2 != -1) { flag2 = true; str = strArrays2[num1].Substring(0, num2); if (num2 < strArrays2[num1].Length) { str3 = strArrays2[num1].Substring(num2 + 1); } } else { str = strArrays2[num1]; } if ((str.Length > 0 ? true : !flag2)) { stringBuilder.Append(str); stringBuilder.Append(" "); } if ((!flag2 ? false : str3.Length != 0)) { strArrays2[num1] = str3; } else { num1++; } if (!flag2) { if (num1 < (int)strArrays2.Length) { string str4 = strArrays2[num1]; num2 = str4.IndexOf('\f'); if (num2 != -1) { str4 = str4.Substring(0, num2); } string str5 = string.Concat(stringBuilder.ToString(), " ", str4); try { stringWidth = this.font.PDFBoxFont.getStringWidth(str5) / 1000f * this.fontSize; } catch (Exception exception) { } } flag = (num1 >= (int)strArrays2.Length ? false : stringWidth < width); } else { break; } }while (flag); if (single < (float)num) { pDPage = new PDPage(lETTER); pDDocument.addPage(pDPage); if (pDPageContentStream != null) { pDPageContentStream.endText(); pDPageContentStream.close(); } pDPageContentStream = new PDPageContentStream(pDDocument, pDPage); pDPageContentStream.setFont(this.font.PDFBoxFont, this.fontSize); pDPageContentStream.beginText(); single = pDPage.getMediaBox().getHeight() - (float)num + height; pDPageContentStream.newLineAtOffset((float)num, single); } if (pDPageContentStream == null) { throw new java.io.IOException("Error:Expected non-null content stream."); } pDPageContentStream.newLineAtOffset(0f, -height); single = single - height; try { pDPageContentStream.showText(stringBuilder.ToString()); if (flag2) { pDPage = new PDPage(lETTER); pDDocument.addPage(pDPage); pDPageContentStream.endText(); pDPageContentStream.close(); pDPageContentStream = new PDPageContentStream(pDDocument, pDPage); pDPageContentStream.setFont(this.font.PDFBoxFont, this.fontSize); pDPageContentStream.beginText(); single = pDPage.getMediaBox().getHeight() - (float)num + height; pDPageContentStream.newLineAtOffset((float)num, single); } } catch (Exception exception1) { } } } if (flag1) { pDDocument.addPage(pDPage); } if (pDPageContentStream != null) { pDPageContentStream.endText(); pDPageContentStream.close(); } if (PDFHelper.AddStamp) { pDDocument = PDFHelper.AddTrialStampIfNecessary(pDDocument); } try { pDDocument.save(pdfFile); } catch (Exception exception3) { Exception exception2 = exception3; throw new PDFToolkitException(exception2.Message, exception2.InnerException); } } catch (Exception exception5) { Exception exception4 = exception5; if (pDDocument != null) { pDDocument.close(); } throw exception4; } }