public static void Run() { // ExStart:ControlRectangleZOrder // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs(); // Instantiate Document class object Document doc1 = new Document(); /// Add page to pages collection of PDF file Aspose.Pdf.Page page1 = doc1.Pages.Add(); // Set size of PDF page page1.SetPageSize(375, 300); // Set left margin for page object as 0 page1.PageInfo.Margin.Left = 0; // Set top margin of page object as 0 page1.PageInfo.Margin.Top = 0; // Create a new rectangle with Color as Red, Z-Order as 0 and certain dimensions AddRectangle(page1, 50, 40, 60, 40, Aspose.Pdf.Color.Red, 2); // Create a new rectangle with Color as Blue, Z-Order as 0 and certain dimensions AddRectangle(page1, 20, 20, 30, 30, Aspose.Pdf.Color.Blue, 1); // Create a new rectangle with Color as Green, Z-Order as 0 and certain dimensions AddRectangle(page1, 40, 40, 60, 30, Aspose.Pdf.Color.Green, 0); dataDir = dataDir + "ControlRectangleZOrder_out_.pdf"; // Save resultant PDF file doc1.Save(dataDir); // ExEnd:ControlRectangleZOrder }
public PDocument(string fileFullName, HDocument hDocument) { /* * //DEBUG * PDocument_debug(fileFullName, hDocument); * return; */ bodyNode = hDocument.BodyNode; pageTextState = PUtil.TextStateUtil.TextState_Default(); PUtil.TextStateUtil.TextState_ModifyFromHStyles((bodyNode as HNodeTag).Styles, pageTextState); pageMargin = new MarginInfo(4, 4, 4, 12); pageBackground = Aspose.Pdf.Color.FromRgb(1.00, 1.00, 1.00); pdfDocument = new Document(); pdfPage = null; pdfTextFragment = null; pdfImage = null; hyperlinkNode = null; pdfNewLine = null; inlineParagraphMargin = null; pdfFormField = null; pdfRadioButtonFields = new Dictionary <string, RadioButtonField>(); updateCurrentPage(); createBody(); pdfDocument.Save(fileFullName); }
public static void Run() { // ExStart:CreateRectangleWithAlphaColor // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs(); // Instantiate Document instance Document doc = new Document(); // Add page to pages collection of PDF file Aspose.Pdf.Page page = doc.Pages.Add(); // Create Graph instance Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400); // Create rectangle object with specific dimensions Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 100); // Set graph fill color from System.Drawing.Color structure from a 32-bit ARGB value rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183))); // Add rectangle object to shapes collection of Graph instance canvas.Shapes.Add(rect); // Create second rectangle object Aspose.Pdf.Drawing.Rectangle rect1 = new Aspose.Pdf.Drawing.Rectangle(200, 150, 200, 100); rect1.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(16118015))); canvas.Shapes.Add(rect1); // Add graph instance to paragraph collection of page object page.Paragraphs.Add(canvas); dataDir = dataDir + "CreateRectangleWithAlphaColor_out_.pdf"; // Save PDF file doc.Save(dataDir); // ExEnd:CreateRectangleWithAlphaColor Console.WriteLine("\nRectangle object created successfully with alpha color.\nFile saved at " + dataDir); }
public static void Run() { // ExStart:EmbedFontWhileDocCreation // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); // Instantiate Pdf object by calling its empty constructor Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); // Create a section in the Pdf object Aspose.Pdf.Page page = doc.Pages.Add(); Aspose.Pdf.Text.TextFragment fragment = new Aspose.Pdf.Text.TextFragment(""); Aspose.Pdf.Text.TextSegment segment = new Aspose.Pdf.Text.TextSegment(" This is a sample text using Custom font."); Aspose.Pdf.Text.TextState ts = new Aspose.Pdf.Text.TextState(); ts.Font = FontRepository.FindFont("Arial"); ts.Font.IsEmbedded = true; segment.TextState = ts; fragment.Segments.Add(segment); page.Paragraphs.Add(fragment); dataDir = dataDir + "EmbedFontWhileDocCreation_out_.pdf"; // Save PDF Document doc.Save(dataDir); // ExEnd:EmbedFontWhileDocCreation Console.WriteLine("\nFont embedded successfully in a PDF file.\nFile saved at " + dataDir); }
private void updateCurrentPage() { bool emergedNewPage = false; pdfDocument.ProcessParagraphs(); if (pdfPage == null) { pdfPage = pdfDocument.Pages.Add(); emergedNewPage = true; } else if (pdfDocument.Pages.Count > currentPageNum) { pdfPage = pdfDocument.Pages[pdfDocument.Pages.Count]; emergedNewPage = true; } if (emergedNewPage) { currentPageNum = pdfDocument.Pages.Count; pdfPage.PageInfo.DefaultTextState = pageTextState; pdfPage.PageInfo.Margin = pageMargin; pdfPage.Background = pageBackground; } }
public static void OTFFont() { // ExStart:OTFFont // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Create new document instance Document pdfDocument = new Document(); // Add page to pages collection of PDF file Aspose.Pdf.Page page = pdfDocument.Pages.Add(); // Create TextFragment instnace with sample text TextFragment fragment = new TextFragment("Sample Text in OTF font"); // Find font inside system font directory // Fragment.TextState.Font = FontRepository.FindFont("HelveticaNeueLT Pro 45 Lt"); // Or you can even specify the path of OTF font in system directory fragment.TextState.Font = FontRepository.OpenFont(dataDir + "space age.otf"); // Specify to emend font inside PDF file, so that its displayed properly, // Even if specific font is not installed/present over target machine fragment.TextState.Font.IsEmbedded = true; // Add TextFragment to paragraphs collection of Page instance page.Paragraphs.Add(fragment); dataDir = dataDir + "OTFFont_out.pdf"; // Save resulting PDF document. pdfDocument.Save(dataDir); // ExEnd:OTFFont Console.WriteLine("\nOTF font used successfully.\nFile saved at " + dataDir); }
private void debug_createP(string fileFullName) { Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(); Aspose.Pdf.Page pdfPage = (Aspose.Pdf.Page)pdfDocument.Pages.Add(); Console.WriteLine("pdfPage.Rect = " + pdfPage.Rect); Console.WriteLine("pdfPage.PageInfo: Width - Height - PureHeight = " + pdfPage.PageInfo.Width + " - " + pdfPage.PageInfo.Height + " - " + pdfPage.PageInfo.PureHeight); Console.WriteLine("pdfPage.PageInfo: Margin.Top - Right - Bottom - Left = " + pdfPage.PageInfo.Margin.Top + " - " + pdfPage.PageInfo.Margin.Right + " - " + pdfPage.PageInfo.Margin.Bottom + " - " + pdfPage.PageInfo.Margin.Left); TextParagraph paragraph = new TextParagraph(); // append string lines paragraph.AppendLine("the quick brown fox jumps over the lazy dog"); paragraph.AppendLine("line2"); paragraph.AppendLine("line3"); Console.WriteLine("paragraph.Rectangle = " + paragraph.Rectangle); Console.WriteLine("paragraph.TextRectangle = " + paragraph.TextRectangle); // Create Graph instance Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)pdfPage.Rect.Width, (float)pdfPage.Rect.Height); // Add graph object to paragraphs collection of page instance pdfPage.Paragraphs.Add(graph); // Create Rectangle instance //Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 120); Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle((float)paragraph.Rectangle.LLX, (float)paragraph.Rectangle.LLY, (float)paragraph.Rectangle.Width, (float)paragraph.Rectangle.Height); //Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle((float)paragraph.TextRectangle.LLX, (float)paragraph.TextRectangle.LLY, (float)paragraph.TextRectangle.Width, (float)paragraph.TextRectangle.Height); // Specify fill color for Graph object //rect.GraphInfo.FillColor = Aspose.Pdf.Color.Red; rect.GraphInfo.LineWidth = 2; rect.GraphInfo.Color = Aspose.Pdf.Color.Brown; // Add rectangle object to shapes collection of Graph object graph.Shapes.Add(rect); TextBuilder textBuilder = new TextBuilder(pdfPage); textBuilder.AppendParagraph(paragraph); pdfDocument.Save(fileFullName); }
/// <summary> /// 浮水印跟頁面一樣大 /// </summary> /// <param name="pdfPage"></param> /// <param name="arg"></param> private static void AddWatermarkFitPage(Aspose.Pdf.Page pdfPage, WatermarkArg arg) { //var text = new FormattedText(arg.Watermark); var defaultTextState = new TextState("MingLiU"); var stamp = new TextStamp(arg.Watermark, defaultTextState); stamp.RotateAngle = arg.RotateAngle; stamp.XIndent = arg.WatermarkHorizontalSpace; stamp.YIndent = arg.WatermarkVerticalSpace; stamp.Opacity = arg.Opacity; stamp.Width = pdfPage.CropBox.Width; stamp.Height = pdfPage.CropBox.Height; pdfPage.AddStamp(stamp); }
public static void AddNewLineFeed() { try { // ExStart:AddNewLineFeed // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); Aspose.Pdf.Document pdfApplicationDoc = new Aspose.Pdf.Document(); Aspose.Pdf.Page applicationFirstPage = (Aspose.Pdf.Page)pdfApplicationDoc.Pages.Add(); // Initialize new TextFragment with text containing required newline markers Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment("Applicant Name: " + Environment.NewLine + " Joe Smoe"); // Set text fragment properties if necessary textFragment.TextState.FontSize = 12; textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman"); textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.LightGray; textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red; // Create TextParagraph object TextParagraph par = new TextParagraph(); // Add new TextFragment to paragraph par.AppendLine(textFragment); // Set paragraph position par.Position = new Aspose.Pdf.Text.Position(100, 600); // Create TextBuilder object TextBuilder textBuilder = new TextBuilder(applicationFirstPage); // Add the TextParagraph using TextBuilder textBuilder.AppendParagraph(par); dataDir = dataDir + "AddNewLineFeed_out.pdf"; // Save resulting PDF document. pdfApplicationDoc.Save(dataDir); // ExEnd:AddNewLineFeed Console.WriteLine("\nNew line feed added successfully.\nFile saved at " + dataDir); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private Document InsertImage(Document document, string barcodeImagePath) { // get page from Pages collection of PDF file Aspose.Pdf.Page page = document.Pages[1]; // create an image instance Aspose.Pdf.Image img = new Aspose.Pdf.Image(); img.IsInLineParagraph = true; // set Image Width and Height in Points img.FixWidth = 100; img.FixHeight = 100; img.HorizontalAlignment = HorizontalAlignment.Right; img.VerticalAlignment = VerticalAlignment.Top; // set image type as SVG img.FileType = Aspose.Pdf.ImageFileType.Unknown; // path for source barcode image file img.File = barcodeImagePath; page.Paragraphs.Add(img); // return updated PDF document return(document); }
public static void Run() { // ExStart:AddTransparentText // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Create Document instance Document doc = new Document(); // Create page to pages collection of PDF file Aspose.Pdf.Page page = doc.Pages.Add(); // Create Graph object Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400); // Create rectangle instance with certain dimensions Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 400, 400); // Create color object from Alpha color channel rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183))); // Add rectanlge to shapes collection of Graph object canvas.Shapes.Add(rect); // Add graph object to paragraphs collection of page object page.Paragraphs.Add(canvas); // Set value to not change position for graph object canvas.IsChangePosition = false; // Create TextFragment instance with sample value TextFragment text = new TextFragment("transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text "); // Create color object from Alpha channel Aspose.Pdf.Color color = Aspose.Pdf.Color.FromArgb(30, 0, 255, 0); // Set color information for text instance text.TextState.ForegroundColor = color; // Add text to paragraphs collection of page instance page.Paragraphs.Add(text); dataDir = dataDir + "AddTransparentText_out.pdf"; doc.Save(dataDir); // ExEnd:AddTransparentText Console.WriteLine("\nTransparent text added successfully.\nFile saved at " + dataDir); }
public static string[] MovePages(string moveFrom, string moveTo, string[] pageList) { try { int pageFrom = Convert.ToInt32(moveFrom); int pageTo = Convert.ToInt32(moveTo); List <string> str = pageList.ToList(); Document doc = new Document(HttpContext.Current.Server.MapPath("Convert/output.pdf")); Aspose.Pdf.Page page = doc.Pages[pageFrom]; //insert an empty page at the end of a PDF file doc.Pages.Insert(pageTo + 1, page); if (pageFrom > pageTo) { doc.Pages.Delete(pageFrom + 1); str.Insert(pageTo, pageList[pageFrom - 1]); str.RemoveAt(pageFrom); } else { doc.Pages.Delete(pageFrom); str.Insert(pageTo, pageList[pageFrom - 1]); str.RemoveAt(pageFrom - 1); } doc.Save(HttpContext.Current.Server.MapPath("Convert/output.pdf")); pageList = str.ToArray(); } catch (Exception Exp) { // return Exp.Message; } return(pageList); }
/// <summary> /// 依 浮水印 水平地蓋滿整個頁面 /// </summary> /// <param name="pdfPage"></param> /// <param name="arg"></param> private static void AddWatermarkRepeatHorizontal(Aspose.Pdf.Page pdfPage, WatermarkArg arg) { if (arg.WatermarkHeight < minValue) { throw new ArgumentException($"{nameof(arg.WatermarkHeight)} must greater than {minValue}"); } if (arg.WatermarkWidth < minValue) { throw new ArgumentException($"{nameof(arg.WatermarkWidth)} must greater than {minValue}"); } //var text = new FormattedText(arg.Watermark); var yIndent = pdfPage.CropBox.Height - arg.WatermarkHeight; var yLimit = 0 - (arg.WatermarkHeight + arg.WatermarkVerticalSpace); var pageWidth = pdfPage.CropBox.Width; var xIndent = 0d; while (yIndent > yLimit) { while (xIndent < pageWidth) { var defaultTextState = new TextState("MingLiU"); var stamp = new TextStamp(arg.Watermark, defaultTextState); stamp.RotateAngle = arg.RotateAngle; stamp.XIndent = xIndent; stamp.YIndent = yIndent; stamp.Opacity = arg.Opacity; stamp.Width = arg.WatermarkWidth; stamp.Height = arg.WatermarkHeight; pdfPage.AddStamp(stamp); xIndent += (arg.WatermarkWidth + arg.WatermarkHorizontalSpace); } xIndent = 0; var yIdentReduce = (arg.WatermarkHeight + arg.WatermarkVerticalSpace); yIndent -= yIdentReduce; } }
private void AddWatermarkToPage(double pWidth, double pHeight, Aspose.Pdf.Page pg, string sUserName, string sTextDate) { //try{ // DeleteExistingWatermarks(pg); //}catch { //} double alfaR = 30; double c = pWidth / pHeight; double alfa = Math.Atan(c); alfaR = (alfa * 180) / Math.PI; FormattedText wtext = new FormattedText("Confidential", new FontColor(220, 20, 60), new FontColor(255, 255, 255), Aspose.Pdf.Facades.FontStyle.HelveticaBold, EncodingType.Winansi, true, 35); wtext.AddNewLineText("Downloaded by"); wtext.AddNewLineText(sUserName.Length > 55 ? sUserName.Substring(0, 55) : sUserName); wtext.AddNewLineText(sTextDate.Length > 19 ? sTextDate.Substring(0, 19) : sTextDate); TextStamp wStamp = new TextStamp(wtext); wStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red); wStamp.TextState.FontStyle = FontStyles.Bold; wStamp.TextState.FontSize = 35; wStamp.HorizontalAlignment = HorizontalAlignment.Center; wStamp.VerticalAlignment = VerticalAlignment.Center; wStamp.TextAlignment = HorizontalAlignment.Center; wStamp.Opacity = .15F; wStamp.RotateAngle = (float)(alfaR - 90); pg.AddStamp(wStamp); }
public static void Run() { // ExStart:RenderingReplaceableSymbols // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); Aspose.Pdf.Document pdfApplicationDoc = new Aspose.Pdf.Document(); Aspose.Pdf.Page applicationFirstPage = (Aspose.Pdf.Page)pdfApplicationDoc.Pages.Add(); // Initialize new TextFragment with text containing required newline markers Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment("Applicant Name: " + Environment.NewLine + " Joe Smoe"); // Set text fragment properties if necessary textFragment.TextState.FontSize = 12; textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman"); textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.LightGray; textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red; // Create TextParagraph object TextParagraph par = new TextParagraph(); // Add new TextFragment to paragraph par.AppendLine(textFragment); // Set paragraph position par.Position = new Aspose.Pdf.Text.Position(100, 600); // Create TextBuilder object TextBuilder textBuilder = new TextBuilder(applicationFirstPage); // Add the TextParagraph using TextBuilder textBuilder.AppendParagraph(par); dataDir = dataDir + "RenderingReplaceableSymbols_out_.pdf"; pdfApplicationDoc.Save(dataDir); // ExEnd:RenderingReplaceableSymbols Console.WriteLine("\nReplaceable symbols render successfully duing pdf creation.\nFile saved at " + dataDir); }
// ExStart:AddRectangle private static void AddRectangle(Aspose.Pdf.Page page, float x, float y, float width, float height, Aspose.Pdf.Color color, int zindex) { // create graph object with dimensions same as specified for Rectangle object Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(width, height); // can we change the position of graph instance graph.IsChangePosition = false; // set Left coordinate position for Graph instance graph.Left = x; // set Top coordinate position for Graph object graph.Top = y; // Add a rectangle inside the "graph" Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(0, 0, width, height); // set rectangle fill color rect.GraphInfo.FillColor = color; // color of graph object rect.GraphInfo.Color = color; // add rectangle to shapes collection of graph instance graph.Shapes.Add(rect); // set Z-Index for rectangle object graph.ZIndex = zindex; // add graph to paragraphs collection of page object page.Paragraphs.Add(graph); }
private void debug_createText(string fileFullName) { /* * // Create Document instance * Document pdfDocument = new Document(); * // Add page to pages collection of Document * Page page = pdfDocument.Pages.Add(); * // Instantiate TextStamp instance with sample text * * * * TextStamp stamp = new TextStamp("This is text stamp with character spacing"); * // Specify font name for Stamp object * stamp.TextState.Font = FontRepository.FindFont("Arial"); * // Specify Font size for TextStamp * stamp.TextState.FontSize = 12; * // Specify character specing as 1f * stamp.TextState.CharacterSpacing = 1f; * // Set the XIndent for Stamp * stamp.XIndent = 100; * // Set the YIndent for Stamp * stamp.YIndent = 500; * // Add textual stamp to page instance * stamp.Put(page); * * // Save resulting PDF document. * pdfDocument.Save(fileFullName); */ /* * Aspose.Pdf.Document pdfApplicationDoc = new Aspose.Pdf.Document(); * Aspose.Pdf.Page applicationFirstPage = (Aspose.Pdf.Page)pdfApplicationDoc.Pages.Add(); * * // Initialize new TextFragment with text containing required newline markers * Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment("Applicant Name: " + Environment.NewLine + " Joe Smoe"); * * // Set text fragment properties if necessary * textFragment.TextState.FontSize = 12; * textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman"); * textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.LightGray; * textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red; * * // Create TextParagraph object * TextParagraph par = new TextParagraph(); * * // Add new TextFragment to paragraph * par.AppendLine(textFragment); * * // Set paragraph position * par.Position = new Aspose.Pdf.Text.Position(100, 600); * * // Create TextBuilder object * TextBuilder textBuilder = new TextBuilder(applicationFirstPage); * // Add the TextParagraph using TextBuilder * textBuilder.AppendParagraph(par); * * * // Save resulting PDF document. * pdfApplicationDoc.Save(fileFullName); */ /* * Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(); * Aspose.Pdf.Page pdfPage = (Aspose.Pdf.Page)pdfDocument.Pages.Add(); * * // create text paragraph * TextParagraph paragraph = new TextParagraph(); * * // set the paragraph rectangle * //paragraph.Rectangle = new Rectangle(100, 600, 200, 700); * * // set word wrapping options * paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords; * * // append string lines * paragraph.AppendLine("the quick brown fox jumps over the lazy dog"); * paragraph.AppendLine("line2"); * paragraph.AppendLine("line3"); * * // append the paragraph to the Pdf page with the TextBuilder * TextBuilder textBuilder = new TextBuilder(pdfPage); * textBuilder.AppendParagraph(paragraph); * * pdfDocument.Save(fileFullName); */ Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(); Aspose.Pdf.Page pdfPage = (Aspose.Pdf.Page)pdfDocument.Pages.Add(); for (int i = 0; i < 200; i++) { TextFragment line = new TextFragment("Line fragment: " + i); pdfPage.Paragraphs.Add(line); } pdfDocument.Save(fileFullName); }
public static string SearchData(string searchText, string[] pageList) { string name = DateTime.Now.Millisecond.ToString(); System.IO.DirectoryInfo downloadedMessageInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("search/")); foreach (FileInfo file in downloadedMessageInfo.GetFiles()) { file.Delete(); } foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories()) { dir.Delete(true); } System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("search/" + name)); Document doc = new Document(HttpContext.Current.Server.MapPath("Convert/output.pdf")); for (int i = 1; i <= doc.Pages.Count; i++) { string filename = "Input/" + pageList[i - 1]; filename = filename.Replace("image", "image-1"); Bitmap bmp = (Bitmap)Bitmap.FromFile(HttpContext.Current.Server.MapPath(filename)); using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp)) { float scale = 150 / 72f; gr.Transform = new System.Drawing.Drawing2D.Matrix(scale, 0, 0, -scale, 0, bmp.Height); Aspose.Pdf.Page page = doc.Pages[i]; //create TextAbsorber object to find all words TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchText); // textFragmentAbsorber.TextSearchOptions.IsRegularExpressionUsed = true; page.Accept(textFragmentAbsorber); //get the extracted text fragments TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments; Brush brush = new SolidBrush(System.Drawing.Color.FromArgb(50, 255, 255, 0)); //loop through the fragments foreach (TextFragment textFragment in textFragmentCollection) { // if (i == 0) { gr.FillRectangle( // gr.DrawRectangle( brush, (float)(textFragment.Position.XIndent), (float)(textFragment.Position.YIndent), (float)(textFragment.Rectangle.Width), (float)(textFragment.Rectangle.Height)); for (int segNum = 1; segNum <= textFragment.Segments.Count; segNum++) { TextSegment segment = textFragment.Segments[segNum]; gr.DrawRectangle( Pens.Green, (float)segment.Rectangle.LLX, (float)segment.Rectangle.LLY, (float)segment.Rectangle.Width, (float)segment.Rectangle.Height); } } } gr.Dispose(); } bmp.Save(HttpContext.Current.Server.MapPath(filename.Replace("image-1", "image_search")), System.Drawing.Imaging.ImageFormat.Png); bmp.Dispose(); string height = ""; string Aratio = ""; System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(filename.Replace("image-1", "image_search"))); ScaleImage(image, 1138, 760, HttpContext.Current.Server.MapPath("search/" + name + "/" + pageList[i - 1]), out height, out Aratio); image.Dispose(); // System.IO.File.Copy(HttpContext.Current.Server.MapPath("Input/image_search" + i + ".png"), HttpContext.Current.Server.MapPath("Input/image" + i + ".png")); } return(name); }
public static void Run() { // ExStart:DetermineTableBreak // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); // Instantiate an object PDF class Document pdf = new Document(); // Add the section to PDF document sections collection Aspose.Pdf.Page page = pdf.Pages.Add(); // Instantiate a table object Aspose.Pdf.Table table1 = new Aspose.Pdf.Table(); table1.Margin.Top = 300; // Add the table in paragraphs collection of the desired section page.Paragraphs.Add(table1); // Set with column widths of the table table1.ColumnWidths = "100 100 100"; // Set default cell border using BorderInfo object table1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F); // Set table border using another customized BorderInfo object table1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F); // Create MarginInfo object and set its left, bottom, right and top margins Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo(); margin.Top = 5f; margin.Left = 5f; margin.Right = 5f; margin.Bottom = 5f; // Set the default cell padding to the MarginInfo object table1.DefaultCellPadding = margin; // If you increase the counter to 17, table will break // Because it cannot be accommodated any more over this page for (int RowCounter = 0; RowCounter <= 16; RowCounter++) { // Create rows in the table and then cells in the rows Aspose.Pdf.Row row1 = table1.Rows.Add(); row1.Cells.Add("col " + RowCounter.ToString() + ", 1"); row1.Cells.Add("col " + RowCounter.ToString() + ", 2"); row1.Cells.Add("col " + RowCounter.ToString() + ", 3"); } // Get the Page Height information float PageHeight = (float)pdf.PageInfo.Height; // Get the total height information of Page Top & Bottom margin, // Table Top margin and table height. float TotalObjectsHeight = (float)page.PageInfo.Margin.Top + (float)page.PageInfo.Margin.Bottom + (float)table1.Margin.Top + (float)table1.GetHeight(); // Display Page Height, Table Height, table Top margin and Page Top // And Bottom margin information Console.WriteLine("PDF document Height = " + pdf.PageInfo.Height.ToString() + "\nTop Margin Info = " + page.PageInfo.Margin.Top.ToString() + "\nBottom Margin Info = " + page.PageInfo.Margin.Bottom.ToString() + "\n\nTable-Top Margin Info = " + table1.Margin.Top.ToString() + "\nAverage Row Height = " + table1.Rows[0].MinRowHeight.ToString() + " \nTable height " + table1.GetHeight().ToString() + "\n ----------------------------------------" + "\nTotal Page Height =" + PageHeight.ToString() + "\nCummulative height including Table =" + TotalObjectsHeight.ToString()); // Check if we deduct the sume of Page top margin + Page Bottom margin // + Table Top margin and table height from Page height and its less // Than 10 (an average row can be greater than 10) if ((PageHeight - TotalObjectsHeight) <= 10) { // If the value is less than 10, then display the message. // Which shows that another row can not be placed and if we add new // Row, table will break. It depends upon the row height value. Console.WriteLine("Page Height - Objects Height < 10, so table will break"); } dataDir = dataDir + "DetermineTableBreak_out.pdf"; // Save the pdf document pdf.Save(dataDir); // ExEnd:DetermineTableBreak Console.WriteLine("\nTable break determined successfully.\nFile saved at " + dataDir); }
private void button1_Click(object sender, EventArgs e) { CreatePDFWithTable(); return; // ExStart:ApplyNumberStyle // The path to the documents directory. string dataDir = @"..\pdfpages\sample.pdf"; Document pdfDoc = new Document(dataDir); pdfDoc.PageInfo.Width = 612.0; pdfDoc.PageInfo.Height = 792.0; pdfDoc.PageInfo.Margin = new Aspose.Pdf.MarginInfo(); pdfDoc.PageInfo.Margin.Left = 72; pdfDoc.PageInfo.Margin.Right = 72; pdfDoc.PageInfo.Margin.Top = 72; pdfDoc.PageInfo.Margin.Bottom = 72; Aspose.Pdf.Page pdfPage = pdfDoc.Pages.Add(); pdfPage.PageInfo.Width = 612.0; pdfPage.PageInfo.Height = 792.0; pdfPage.PageInfo.Margin = new Aspose.Pdf.MarginInfo(); pdfPage.PageInfo.Margin.Left = 72; pdfPage.PageInfo.Margin.Right = 72; pdfPage.PageInfo.Margin.Top = 2; pdfPage.PageInfo.Margin.Bottom = 72; Aspose.Pdf.FloatingBox floatBox = new Aspose.Pdf.FloatingBox(); floatBox.Margin = pdfPage.PageInfo.Margin; pdfPage.Paragraphs.Add(floatBox); TextFragment textFragment = new TextFragment(); TextSegment segment = new TextSegment(); Aspose.Pdf.Heading heading = new Aspose.Pdf.Heading(1); heading.IsInList = true; heading.StartNumber = 1; heading.Text = "List 1"; heading.Style = NumberingStyle.NumeralsRomanLowercase; heading.IsAutoSequence = true; floatBox.Paragraphs.Add(heading); Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1); heading2.IsInList = true; heading2.StartNumber = 13; heading2.Text = "List 2"; heading2.Style = NumberingStyle.NumeralsRomanLowercase; heading2.IsAutoSequence = true; floatBox.Paragraphs.Add(heading2); Aspose.Pdf.Heading heading3 = new Aspose.Pdf.Heading(2); heading3.IsInList = true; heading3.StartNumber = 1; heading3.Text = "the value, as of the effective date of the plan, of property to be distributed under the plan onaccount of each allowed"; heading3.Style = NumberingStyle.LettersLowercase; heading3.IsAutoSequence = true; floatBox.Paragraphs.Add(heading3); dataDir = dataDir + "9992.pdf"; pdfDoc.Save(dataDir); // ExEnd:ApplyNumberStyle Console.WriteLine("\nNumber style applied successfully in headings.\nFile saved at " + dataDir); }
/// <summary> /// 以角度線性期function來呈現 /// </summary> /// <param name="pdfPage"></param> /// <param name="arg"></param> private static void AddWatermarkRepeatRotateAngle(Aspose.Pdf.Page pdfPage, WatermarkArg arg) { if (arg.WatermarkHeight < minValue) { throw new ArgumentException($"{nameof(arg.WatermarkHeight)} must greater than {minValue}"); } if (arg.WatermarkWidth < minValue) { throw new ArgumentException($"{nameof(arg.WatermarkWidth)} must greater than {minValue}"); } var text = new FormattedText(arg.Watermark); var yIndent = pdfPage.CropBox.Height - arg.WatermarkHeight; var yLimit = 0 - (arg.WatermarkHeight + arg.WatermarkVerticalSpace); var pageWidth = pdfPage.CropBox.Width; var pageHeight = pdfPage.CropBox.Height; var xIndent = 0d; while (yIndent > yLimit) { var y = yIndent; while (xIndent < pageWidth && y < pageHeight) { var stamp = new TextStamp(text); stamp.RotateAngle = arg.RotateAngle; stamp.XIndent = xIndent; stamp.YIndent = y; stamp.Opacity = arg.Opacity; stamp.Width = arg.WatermarkWidth; stamp.Height = arg.WatermarkHeight; pdfPage.AddStamp(stamp); xIndent += (arg.WatermarkWidth + arg.WatermarkHorizontalSpace); xIndent = xIndent + Math.Cos(30) * arg.WatermarkWidth; y = y + Math.Sign(30) * (arg.WatermarkHeight + arg.WatermarkVerticalSpace); } xIndent = 0; var yIdentReduce = (arg.WatermarkHeight + arg.WatermarkVerticalSpace); yIndent -= yIdentReduce; } //到底了,要再連走 X var baseX = 0d; while (baseX < pageWidth) { var y = yIndent; xIndent = baseX; while (xIndent < pageWidth) { var stamp = new TextStamp(text); stamp.RotateAngle = arg.RotateAngle; stamp.XIndent = xIndent; stamp.YIndent = y; stamp.Opacity = arg.Opacity; stamp.Width = arg.WatermarkWidth; stamp.Height = arg.WatermarkHeight; pdfPage.AddStamp(stamp); xIndent += (arg.WatermarkWidth + arg.WatermarkHorizontalSpace); xIndent = xIndent + Math.Cos(30) * arg.WatermarkWidth; y = y + Math.Sign(30) * (arg.WatermarkHeight + arg.WatermarkVerticalSpace); } baseX += (arg.WatermarkWidth + arg.WatermarkHorizontalSpace);; } }
public void GeneratePDF(ReceivedLetter receivedLetter) { MemoryStream stream = null; if (receivedLetter.ReceiveLetterFiles != null && receivedLetter.ReceiveLetterFiles.Count > 0) { //Render Letter Content With Files Aspose.Pdf.Document contentPdf = new Aspose.Pdf.Document(); foreach (var file in receivedLetter.ReceiveLetterFiles) { if (file.File.ContentType.Contains("image")) { Aspose.Pdf.Page page = contentPdf.Pages.Add(); Bitmap b = new Bitmap(new MemoryStream(file.File.Content)); page.PageInfo.Width = b.Width; page.PageInfo.Height = b.Height; // Set margins so image will fit, etc. page.PageInfo.Margin.Bottom = 0; page.PageInfo.Margin.Top = 0; page.PageInfo.Margin.Left = 0; page.PageInfo.Margin.Right = 0; page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height); Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(); // Add the image into paragraphs collection of the section page.Paragraphs.Add(image1); // Set the image file stream image1.ImageStream = new MemoryStream(file.File.Content); } else if (file.File.ContentType.Contains("pdf")) { //Append pdf to appendages Aspose.Pdf.Document thisPDF = new Aspose.Pdf.Document(new MemoryStream(file.File.Content)); contentPdf.Pages.Add(thisPDF.Pages); } else if (file.File.ContentType.Contains("word")) { //Convert to pdf first Document thisDoc = new Document(new MemoryStream(file.File.Content)); MemoryStream temp = new MemoryStream(); thisDoc.Save(temp, SaveFormat.Pdf); //Second append it to appendages Aspose.Pdf.Document thisPDF = new Aspose.Pdf.Document(temp); contentPdf.Pages.Add(thisPDF.Pages); } } stream = new MemoryStream(); contentPdf.Save(stream); receivedLetter.LetterFileContent = stream.ToArray(); } else { receivedLetter.LetterFileContent = new byte[0]; } //Debug //Aspose.Pdf.Document letterFile = new Aspose.Pdf.Document(stream); //letterFile.Save(Server.MapPath("~/LetterOnly.pdf")); //Save letter with Appendage if (receivedLetter.Appendages != null && receivedLetter.Appendages.Count > 0) { Aspose.Pdf.Document appendagePdf = new Aspose.Pdf.Document(); foreach (var file in receivedLetter.Appendages) { if (file.File.ContentType.Contains("image")) { Page page = appendagePdf.Pages.Add(); Bitmap b = new Bitmap(new MemoryStream(file.File.Content)); page.PageInfo.Width = b.Width; page.PageInfo.Height = b.Height; // Set margins so image will fit, etc. page.PageInfo.Margin.Bottom = 0; page.PageInfo.Margin.Top = 0; page.PageInfo.Margin.Left = 0; page.PageInfo.Margin.Right = 0; page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height); Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(); // Add the image into paragraphs collection of the section page.Paragraphs.Add(image1); // Set the image file stream image1.ImageStream = new MemoryStream(file.File.Content); } else if (file.File.ContentType.Contains("pdf")) { //Append pdf to appendages Aspose.Pdf.Document thisPDF = new Aspose.Pdf.Document(new MemoryStream(file.File.Content)); appendagePdf.Pages.Add(thisPDF.Pages); } else if (file.File.ContentType.Contains("word")) { //Convert to pdf first Document thisDoc = new Document(new MemoryStream(file.File.Content)); MemoryStream temp = new MemoryStream(); thisDoc.Save(temp, SaveFormat.Pdf); //Second append it to appendages Aspose.Pdf.Document thisPDF = new Aspose.Pdf.Document(temp); appendagePdf.Pages.Add(thisPDF.Pages); } } //Return PDF from letter if (stream != null && receivedLetter.LetterFileContent.Length > 0) { Aspose.Pdf.Document letterPdf = new Aspose.Pdf.Document(stream); stream = new MemoryStream(); appendagePdf.Save(stream); receivedLetter.AppendageContent = stream.ToArray(); //Debug //appendagePdf.Save(Server.MapPath("~/AppendagesOnly.pdf")); letterPdf.Pages.Add(appendagePdf.Pages); stream = new MemoryStream(); letterPdf.Save(stream); //Debug //letterPdf.Save(Server.MapPath("~/letterWithAppendages.pdf")); receivedLetter.LetterFileAppendageContent = stream.ToArray(); } else { stream = new MemoryStream(); appendagePdf.Save(stream); receivedLetter.AppendageContent = stream.ToArray(); receivedLetter.LetterFileAppendageContent = stream.ToArray(); } } else { receivedLetter.LetterFileAppendageContent = new byte[0]; receivedLetter.AppendageContent = new byte[0]; } }