private bool ConvertTiffToPDF(string imagePath) { try { string[] strArrays = ImageToPDFConverter.SplitTiff(imagePath); for (int i = 0; i < (int)strArrays.Length; i++) { string str = strArrays[i]; PDPage pDPage = new PDPage(); this.pdfFile.addPage(pDPage); PDImageXObject pDImageXObject = PDImageXObject.createFromFile(str, this.pdfFile); PDRectangle pDRectangle = new PDRectangle((float)(pDImageXObject.getWidth() + 40), (float)(pDImageXObject.getHeight() + 40)); pDPage.setMediaBox(pDRectangle); PDPageContentStream pDPageContentStream = new PDPageContentStream(this.pdfFile, pDPage); pDPageContentStream.drawImage(pDImageXObject, 20f, 20f); pDPageContentStream.close(); } } catch (Exception exception1) { Exception exception = exception1; throw new PDFToolkitException(exception.Message, exception); } return(true); }
private bool ConvertOthersToPDF(string imagePath) { try { PDPage pDPage = new PDPage(); string str = "temp.png"; this.pdfFile.addPage(pDPage); if (Path.GetExtension(imagePath).ToLower() == ".bmp") { ImageToPDFConverter.BmpToPng(new Bitmap(Image.FromFile(imagePath)), str); } else if (Path.GetExtension(imagePath).ToLower() == ".png") { str = imagePath; } else { Image.FromFile(imagePath).Save(str, ImageFormat.Png); } PDImageXObject pDImageXObject = PDImageXObject.createFromFile(str, this.pdfFile); PDRectangle pDRectangle = new PDRectangle((float)(pDImageXObject.getWidth() + 40), (float)(pDImageXObject.getHeight() + 40)); pDPage.setMediaBox(pDRectangle); PDPageContentStream pDPageContentStream = new PDPageContentStream(this.pdfFile, pDPage); pDPageContentStream.drawImage(pDImageXObject, 20f, 20f); pDPageContentStream.close(); } catch (Exception exception1) { Exception exception = exception1; throw new PDFToolkitException(exception.Message, exception); } return(true); }
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); }
private bool ConvertJPGToPDF(string imagePath) { try { PDPage pDPage = new PDPage(); this.pdfFile.addPage(pDPage); PDImageXObject pDImageXObject = PDImageXObject.createFromFile(imagePath, this.pdfFile); PDRectangle pDRectangle = new PDRectangle((float)(pDImageXObject.getWidth() + 40), (float)(pDImageXObject.getHeight() + 40)); pDPage.setMediaBox(pDRectangle); PDPageContentStream pDPageContentStream = new PDPageContentStream(this.pdfFile, pDPage); pDPageContentStream.drawImage(pDImageXObject, 20f, 20f); pDPageContentStream.close(); } catch (Exception exception1) { Exception exception = exception1; throw new PDFToolkitException(exception.Message, exception); } return(true); }
public PDFRectangle(PDRectangle pdRect) { this.rectangle = pdRect; }
public PDFRectangle(float width, float height) { this.rectangle = new PDRectangle(width, height); }
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; } }
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); }
public bool AddAnnotation(PDFAnnotation annotation, PDFDocument doc) { bool result; try { List annotations = this.PDFBoxPage.getAnnotations(); PDRectangle rectangle = new PDRectangle(annotation.Position.X, this.MediaBox.Height - annotation.Position.Y - annotation.Position.Height, annotation.Position.Width, annotation.Position.Height); annotation.pdfAnotation.setRectangle(rectangle); bool flag = annotation.type == PDFAnnotationType.PDFAnnotationLine; if (flag) { PDAnnotationLine pDAnnotationLine = (PDAnnotationLine)annotation.pdfAnotation; float[] array = new float[4]; switch (annotation.Direction) { case PDFDirection.DIAGONALLEFTUP: array[2] = annotation.Position.PDFBoxRectangle.getLowerLeftX(); array[3] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getLowerLeftY(); array[0] = annotation.Position.PDFBoxRectangle.getLowerLeftX() + annotation.Position.PDFBoxRectangle.getWidth(); array[1] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getLowerLeftY() - annotation.Position.PDFBoxRectangle.getHeight(); break; case PDFDirection.DIAGONALRIGHTUP: array[0] = annotation.Position.PDFBoxRectangle.getLowerLeftX(); array[1] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getLowerLeftY() - annotation.Position.PDFBoxRectangle.getHeight(); array[2] = annotation.Position.PDFBoxRectangle.getLowerLeftX() + annotation.Position.PDFBoxRectangle.getWidth(); array[3] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getLowerLeftY(); break; case PDFDirection.DIAGONALRIGHTDOWN: array[0] = annotation.Position.PDFBoxRectangle.getLowerLeftX(); array[1] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getLowerLeftY(); array[2] = annotation.Position.PDFBoxRectangle.getLowerLeftX() + annotation.Position.PDFBoxRectangle.getWidth(); array[3] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getLowerLeftY() - annotation.Position.PDFBoxRectangle.getHeight(); break; case PDFDirection.DIAGONALLEFTDOWN: array[2] = annotation.Position.PDFBoxRectangle.getLowerLeftX(); array[3] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getLowerLeftY() - annotation.Position.PDFBoxRectangle.getHeight(); array[0] = annotation.Position.PDFBoxRectangle.getLowerLeftX() + annotation.Position.PDFBoxRectangle.getWidth(); array[1] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getLowerLeftY(); break; } pDAnnotationLine.setLine(array); annotations.add(pDAnnotationLine); } else { bool flag2 = annotation.type == PDFAnnotationType.PDFAnnotationTextMarkup; if (flag2) { PDAnnotationTextMarkup pDAnnotationTextMarkup = (PDAnnotationTextMarkup)annotation.pdfAnotation; float[] array2 = new float[8]; array2[0] = annotation.Position.PDFBoxRectangle.getLowerLeftX(); array2[1] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getUpperRightY() - 2f; array2[2] = annotation.Position.PDFBoxRectangle.getUpperRightX(); array2[3] = array2[1]; array2[4] = array2[0]; array2[5] = this.PDFBoxPage.getCropBox().getHeight() - annotation.Position.PDFBoxRectangle.getLowerLeftY() - 2f; array2[6] = array2[2]; array2[7] = array2[5]; pDAnnotationTextMarkup.setQuadPoints(array2); annotations.add(pDAnnotationTextMarkup); } else { bool flag3 = annotation.type == PDFAnnotationType.PDFAnnotationLink; if (flag3) { PDAnnotationLink pDAnnotationLink = (PDAnnotationLink)annotation.pdfAnotation; bool flag4 = !string.IsNullOrEmpty(annotation.LinkURL); if (flag4) { PDActionURI pDActionURI = new PDActionURI(); pDActionURI.setURI(annotation.LinkURL); pDAnnotationLink.setAction(pDActionURI); } else { Console.WriteLine("The PDFAnnotationLink has no LinkURL property. Set this property for the link to work well."); } annotations.add(pDAnnotationLink); } else { bool flag5 = annotation.type == PDFAnnotationType.PDFAnnotationRubberStamp; if (flag5) { PDAnnotationRubberStamp pDAnnotationRubberStamp = (PDAnnotationRubberStamp)annotation.pdfAnotation; pDAnnotationRubberStamp.setName("TopSecret"); pDAnnotationRubberStamp.setAppearance(PDFHelper.GetRubberStampAppearance(doc, annotation.Position, annotation.SubType)); pDAnnotationRubberStamp.setRectangle(rectangle); annotations.add(pDAnnotationRubberStamp); } else { annotations.add(annotation.pdfAnotation); } } } } result = true; } catch (Exception ex) { throw new PDFToolkitException(ex.Message, ex); } return(result); }