public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); // Create a page in the document object Aspose.Pdf.Page page = doc.Pages.Add(); // Create Header Section of the document Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter(); // Set the header for the PDF file page.Header = header; // Create an image object in the page Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(); // Set the path of image file image1.File = dataDir + "aspose-logo.jpg"; // Add image to Header page of the Pdf file header.Paragraphs.Add(image1); // Create a Footer Section of the document Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter(); // Set the footer of the PDF file page.Footer = footer; // Create a Text object Aspose.Pdf.Text.TextFragment txt = new Aspose.Pdf.Text.TextFragment("Page: ($p of $P ) "); // Add text to Header section of the Pdf file footer.Paragraphs.Add(txt); // Save the Pdf file doc.Save(dataDir + "ImageAndPageNumberInHeaderFooter_out.pdf"); // ExEnd:1 }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Instantiate Document instance by calling empty constructor Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(); // Create a page in the pdf document Aspose.Pdf.Page page = pdfDocument.Pages.Add(); // Create a Header Section of the PDF file Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter(); // Set the Odd Header for the PDF file page.Header = header; // Set the top margin for the header section header.Margin.Top = 20; // Instantiate a table object Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table(); // Add the table in paragraphs collection of the desired section header.Paragraphs.Add(tab1); // Set default cell border using BorderInfo object tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F); // Set with column widths of the table tab1.ColumnWidths = "60 300"; Aspose.Pdf.Image img = new Aspose.Pdf.Image(); img.File = dataDir + "aspose-logo.jpg"; // Create rows in the table and then cells in the rows Aspose.Pdf.Row row1 = tab1.Rows.Add(); row1.Cells.Add("Table in Header Section"); row1.BackgroundColor = Color.Gray; // Set the row span value for first row as 2 tab1.Rows[0].Cells[0].ColSpan = 2; tab1.Rows[0].Cells[0].DefaultCellTextState.ForegroundColor = Color.Cyan; tab1.Rows[0].Cells[0].DefaultCellTextState.Font = FontRepository.FindFont("Helvetica"); // Create rows in the table and then cells in the rows Aspose.Pdf.Row row2 = tab1.Rows.Add(); // Set the background color for Row2 row2.BackgroundColor = Color.White; // Add the cell which holds the image Aspose.Pdf.Cell cell2 = row2.Cells.Add(); // Set the image width to 60 img.FixWidth = 60; // Add the image to the table cell cell2.Paragraphs.Add(img); row2.Cells.Add("Logo is looking fine !"); row2.Cells[1].DefaultCellTextState.Font = FontRepository.FindFont("Helvetica"); // Set the vertical allignment of the text as center alligned row2.Cells[1].VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center; row2.Cells[1].Alignment = Aspose.Pdf.HorizontalAlignment.Center; // Save the Pdf file pdfDocument.Save(dataDir + "TableInHeaderFooterSection_out.pdf"); // ExEnd:1 }
public static void Run() { // ExStart:SetImageSize // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Instantiate Document object Document doc = new Document(); // add page to pages collection of PDF file Aspose.Pdf.Page page = doc.Pages.Add(); // Create an image instance Aspose.Pdf.Image img = new Aspose.Pdf.Image(); // Set Image Width and Height in Points img.FixWidth = 100; img.FixHeight = 100; // Set image type as SVG img.FileType = Aspose.Pdf.ImageFileType.Unknown; // Path for source file img.File = dataDir + "aspose-logo.jpg"; page.Paragraphs.Add(img); //Set page properties page.PageInfo.Width = 800; page.PageInfo.Height = 800; dataDir = dataDir + "SetImageSize_out.pdf"; // save resultant PDF file doc.Save(dataDir); // ExEnd:SetImageSize Console.WriteLine("\nImage size added successfully.\nFile saved at " + dataDir); }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Create new document object Aspose.Pdf.Document document = new Aspose.Pdf.Document(); Aspose.Pdf.Page page = document.Pages.Add(); Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog."); // Initilize TextFormattingOptions for the text fragment and specify SubsequentLinesIndent value text.TextState.FormattingOptions = new Aspose.Pdf.Text.TextFormattingOptions() { SubsequentLinesIndent = 20 }; page.Paragraphs.Add(text); text = new Aspose.Pdf.Text.TextFragment("Line2"); page.Paragraphs.Add(text); text = new Aspose.Pdf.Text.TextFragment("Line3"); page.Paragraphs.Add(text); text = new Aspose.Pdf.Text.TextFragment("Line4"); page.Paragraphs.Add(text); text = new Aspose.Pdf.Text.TextFragment("Line5"); page.Paragraphs.Add(text); document.Save(dataDir + "SubsequentIndent_out.pdf", Aspose.Pdf.SaveFormat.Pdf); // ExEnd:1 }
public static byte[] Tif2PDF(byte[] fileBuffer) { Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); // Add a page to pages collection of document MemoryStream mystream = new MemoryStream(fileBuffer); Image tiffImage = Image.FromStream(mystream); int frameCount = tiffImage.GetFrameCount(FrameDimension.Page); MemoryStream[] images = new MemoryStream[frameCount]; Guid objGuid = tiffImage.FrameDimensionsList[0]; FrameDimension objDimension = new FrameDimension(objGuid); for (int i = 0; i < frameCount; i++) { tiffImage.SelectActiveFrame(objDimension, i); using (MemoryStream ms = new MemoryStream()) { tiffImage.Save(ms, ImageFormat.Bmp); Aspose.Pdf.Page page = doc.Pages.Add(); Bitmap b = new Bitmap(ms); page.Resources.Images.Add(ms); page.PageInfo.Margin.Bottom = 0; page.PageInfo.Margin.Top = 0; page.PageInfo.Margin.Left = 0; page.PageInfo.Margin.Right = 0; page.SetPageSize(b.Width, b.Height); // Using GSave operator: this operator saves current graphics state page.Contents.Add(new Aspose.Pdf.Operator.GSave()); // Create Rectangle and Matrix objects Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height); Aspose.Pdf.DOM.Matrix matrix = new Aspose.Pdf.DOM.Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY }); // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed page.Contents.Add(new Aspose.Pdf.Operator.ConcatenateMatrix(matrix)); Aspose.Pdf.XImage ximage = page.Resources.Images[page.Resources.Images.Count]; // Using Do operator: this operator draws image page.Contents.Add(new Aspose.Pdf.Operator.Do(ximage.Name)); // Using GRestore operator: this operator restores graphics state page.Contents.Add(new Aspose.Pdf.Operator.GRestore()); } } var pdfStream = new MemoryStream(); doc.Save(pdfStream); // Close memoryStream object var buffer = pdfStream.ToArray(); pdfStream.Close(); mystream.Close(); return(buffer); }
public static void Run() { // ExStart:ConvertImageStreamtoPDF // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Instantiate Document instance by calling its empty constructor Aspose.Pdf.Document pdf1 = new Aspose.Pdf.Document(); // Add a Page into the pdf document Aspose.Pdf.Page sec = pdf1.Pages.Add(); // Create a FileStream object to read the imag file FileStream fs = File.OpenRead(dataDir + "aspose.jpg"); // Read the image into Byte array byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); // Create a MemoryStream object from image Byte array MemoryStream ms = new MemoryStream(data); // Create an image object Aspose.Pdf.Image imageht = new Aspose.Pdf.Image(); // Specify the image source as MemoryStream imageht.ImageStream = ms; // Add image object into the Paragraphs collection of the section sec.Paragraphs.Add(imageht); // Save the Pdf pdf1.Save(dataDir + "ConvertMemoryStreamImageToPdf_out.pdf"); // Close the MemoryStream Object ms.Close(); // ExEnd:ConvertImageStreamtoPDF }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Instantiate Document instance Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(); // Add a Page into the pdf document Aspose.Pdf.Page page = pdf.Pages.Add(); // Initializes a new instance of the FloatingBox class Aspose.Pdf.FloatingBox box1 = new Aspose.Pdf.FloatingBox(140, 80); // Float value that indicates left position of the paragraph box1.Left = 2; // Float value that indicates top position of the paragraph box1.Top = 10; // Add the macros to the paragraphs collection of the FloatingBox box1.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Page: ($p/ $P )")); // Add a floatingBox to the page page.Paragraphs.Add(box1); // Save the document pdf.Save(dataDir + "PageNumberinHeaderFooterUsingFloatingBox_out.pdf"); // ExEnd:1 }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(); Aspose.Pdf.Page sec1 = pdf.Pages.Add(); Aspose.Pdf.Text.TextFragment t1 = new Aspose.Pdf.Text.TextFragment("paragraph 3 segment"); sec1.Paragraphs.Add(t1); Aspose.Pdf.Text.TextSegment seg1 = new Aspose.Pdf.Text.TextSegment(); t1.Text = "paragraph 3 segment 1"; t1.TextState.ForegroundColor = Color.Red; t1.TextState.FontSize = 12; Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(); image1.File = dataDir + "test_image.png"; Aspose.Pdf.FloatingBox box1 = new Aspose.Pdf.FloatingBox(117, 21); sec1.Paragraphs.Add(box1); box1.Left = -4; box1.Top = -4; box1.Paragraphs.Add(image1); pdf.Save(dataDir + "CreateMultiLayerPdf_out.pdf"); // ExEnd:1 }
public void EscapeUri(string uri, string result, bool isEscaped) { //ExStart //ExFor:PdfSaveOptions.EscapeUri //ExFor:PdfSaveOptions.OpenHyperlinksInNewWindow //ExSummary: Shows how to escape hyperlinks or not in the document. DocumentBuilder builder = new DocumentBuilder(); builder.InsertHyperlink("Testlink", uri, false); // Set this property to false if you are sure that hyperlinks in document's model are already escaped PdfSaveOptions options = new PdfSaveOptions(); options.EscapeUri = isEscaped; options.OpenHyperlinksInNewWindow = true; builder.Document.Save(ArtifactsDir + "PdfSaveOptions.EscapedUri.pdf", options); //ExEnd #if !(__MOBILE__ || MAC) Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(ArtifactsDir + "PdfSaveOptions.EscapedUri.pdf"); // Get first page Aspose.Pdf.Page page = pdfDocument.Pages[1]; // Get the first link annotation LinkAnnotation linkAnnot = (LinkAnnotation)page.Annotations[1]; JavascriptAction action = (JavascriptAction)linkAnnot.Action; string uriText = action.Script; Assert.AreEqual(result, uriText); #endif }
public static void Run() { // ExStart:CreatePDFwithTaggedImage // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); Document doc = new Document(); Aspose.Pdf.Page page1 = doc.Pages.Add(); // Create image Image image = new Image(); // Assign image file image.File = dataDir + @"aspose-logo.jpg"; // Create BBox element Rectangle BBox = new Rectangle(30, 70, 300, 720); // Create tagged figure element TaggedPdfFigureElement figureElement = new TaggedPdfFigureElement(doc, image, BBox); Rectangle BBox1 = new Rectangle(550, 570, 300, 720); TaggedPdfFigureElement figureElement1 = new TaggedPdfFigureElement(doc, image, BBox1); // Add tagged figure element into content page1.TaggedPdfContent.Add(figureElement); page1.TaggedPdfContent.Add(figureElement1); // Save PDF Document doc.Save(dataDir + "PDFwithTaggedImage.pdf"); // ExEnd:CreatePDFwithTaggedImage }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); // Get a list of tiff image files string[] files = System.IO.Directory.GetFiles(dataDir, "*.tif"); // Instantiate a Document object Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); // Navigate through the files and them in the pdf file foreach (string myFile in files) { // Load all tiff files in byte array FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read); byte[] tmpBytes = new byte[fs.Length]; fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length)); MemoryStream mystream = new MemoryStream(tmpBytes); Bitmap b = new Bitmap(mystream); // Create a new Page in the Pdf document Aspose.Pdf.Page currpage = doc.Pages.Add(); // Set margins so image will fit, etc. currpage.PageInfo.Margin.Top = 5; currpage.PageInfo.Margin.Bottom = 5; currpage.PageInfo.Margin.Left = 5; currpage.PageInfo.Margin.Right = 5; currpage.PageInfo.Width = (b.Width / b.HorizontalResolution) * 72; currpage.PageInfo.Height = (b.Height / b.VerticalResolution) * 72; // Create an image object Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(); // Add the image into paragraphs collection of the page currpage.Paragraphs.Add(image1); // Set IsBlackWhite property to true for performance improvement image1.IsBlackWhite = true; // Set the ImageStream to a MemoryStream object image1.ImageStream = mystream; // Set desired image scale image1.ImageScale = 0.95F; } // Save the Pdf doc.Save(dataDir + "PerformaceImprovement_out.pdf"); // ExEnd:1 }
public static byte[] Img2PDF(byte[] fileBuffer) { Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); // Add a page to pages collection of document Aspose.Pdf.Page page = doc.Pages.Add(); MemoryStream mystream = new MemoryStream(fileBuffer); // Instantiate BitMap object with loaded image stream Bitmap b = new Bitmap(mystream); // 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.SetPageSize(b.Width, b.Height); //page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height); page.Resources.Images.Add(mystream); // Using GSave operator: this operator saves current graphics state page.Contents.Add(new Aspose.Pdf.Operator.GSave()); // Create Rectangle and Matrix objects Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height); Aspose.Pdf.DOM.Matrix matrix = new Aspose.Pdf.DOM.Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY }); // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed page.Contents.Add(new Aspose.Pdf.Operator.ConcatenateMatrix(matrix)); Aspose.Pdf.XImage ximage = page.Resources.Images[page.Resources.Images.Count]; // Using Do operator: this operator draws image page.Contents.Add(new Aspose.Pdf.Operator.Do(ximage.Name)); // Using GRestore operator: this operator restores graphics state page.Contents.Add(new Aspose.Pdf.Operator.GRestore()); var pdfStream = new MemoryStream(); doc.Save(pdfStream); // Close memoryStream object var buffer = pdfStream.ToArray(); pdfStream.Close(); mystream.Close(); return(buffer); }
public static void Run() { //ExStart: CustomizePageNumbesWhileAddingTOC //Sometimes when we add TOC into PDF document it is required to customize the page numbering in the TOC. //For exapmle we need add some prefix before page number like "P1, P2, P3 ...". // For such case Aspose.PDF for .NET provides PageNumbersPrefix property of TocInfo class. //Code snippet below shows how to use this feature. string inFile = RunExamples.GetDataDir_AsposePdf_WorkingDocuments() + "42824.pdf"; string outFile = RunExamples.GetDataDir_AsposePdf_WorkingDocuments() + "42824_out.pdf"; // Load an existing PDF files Document doc = new Document(inFile); // Get access to first page of PDF file Aspose.Pdf.Page tocPage = doc.Pages.Insert(1); // Create object to represent TOC information TocInfo tocInfo = new TocInfo(); TextFragment title = new TextFragment("Table Of Contents"); title.TextState.FontSize = 20; title.TextState.FontStyle = FontStyles.Bold; // Set the title for TOC tocInfo.Title = title; tocInfo.PageNumbersPrefix = "P"; tocPage.TocInfo = tocInfo; for (int i = 1; i < doc.Pages.Count; i++) { // Create Heading object Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1); TextSegment segment2 = new TextSegment(); heading2.TocPage = tocPage; heading2.Segments.Add(segment2); // Specify the destination page for heading object heading2.DestinationPage = doc.Pages[i + 1]; // Destination page heading2.Top = doc.Pages[i + 1].Rect.Height; // Destination coordinate segment2.Text = "Page " + i.ToString(); // Add heading to page containing TOC tocPage.Paragraphs.Add(heading2); } // Save the updated document doc.Save(outFile); //ExEnd: CustomizePageNumbesWhileAddingTOC }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Instantiate a Document object by calling its empty constructor Aspose.Pdf.Document pdf1 = new Aspose.Pdf.Document(); // Create a page in the Pdf object Aspose.Pdf.Page page = pdf1.Pages.Add(); // Create Header Section of the document Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter(); // Set the header for the PDF file page.Header = header; // Create a Text object Aspose.Pdf.Text.TextFragment txt1 = new Aspose.Pdf.Text.TextFragment("Aspose.Pdf is a Robust component by"); // Specify the color txt1.TextState.ForegroundColor = Color.Blue; txt1.IsInLineParagraph = true; // Create an image object in the section Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(); // Set the path of image file image1.File = dataDir + "aspose-logo.jpg"; // Set the image width Information image1.FixWidth = 50; image1.FixHeight = 20; // Indicate seg1's InlineParagraph is a image. image1.IsInLineParagraph = true; Aspose.Pdf.Text.TextFragment txt2 = new Aspose.Pdf.Text.TextFragment(" Pty Ltd."); txt2.IsInLineParagraph = true; txt2.TextState.ForegroundColor = Color.Maroon; header.Paragraphs.Add(txt1); header.Paragraphs.Add(image1); header.Paragraphs.Add(txt2); // Save the Pdf pdf1.Save(dataDir + "ImageAndPageNumberInHeaderFooter_UsingInlineParagraph_out.pdf"); // ExEnd:1 }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); // Retrieve names of all the JPG files in a particular Directory string[] fileEntries = Directory.GetFiles(dataDir, "*.JPG"); int counter; for (counter = 0; counter < fileEntries.Length - 1; counter++) { // Create a page object Aspose.Pdf.Page page = doc.Pages.Add(); // Creat an image object Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(); image1.File = fileEntries[counter]; // Create a BitMap object in order to get the information of image file Bitmap myimage = new Bitmap(fileEntries[counter]); // Check if the width of the image file is greater than Page width or not if (myimage.Width > page.PageInfo.Width) { // If the Image width is greater than page width, then set the page orientation to Landscape page.PageInfo.IsLandscape = true; } else { // If the Image width is less than page width, then set the page orientation to Portrait page.PageInfo.IsLandscape = false; } // Add the image to paragraphs collection of the PDF document page.Paragraphs.Add(image1); } // Save the Pdf file doc.Save(dataDir + "SetPageOrientation_out.pdf"); // ExEnd:1 }
public static void Run() { //ExStart: CustomizePageNumbesWhileAddingTOC string inFile = RunExamples.GetDataDir_AsposePdf_WorkingDocuments() + "42824.pdf"; string outFile = RunExamples.GetDataDir_AsposePdf_WorkingDocuments() + "42824_out.pdf"; // Load an existing PDF files Document doc = new Document(inFile); // Get access to first page of PDF file Aspose.Pdf.Page tocPage = doc.Pages.Insert(1); // Create object to represent TOC information TocInfo tocInfo = new TocInfo(); TextFragment title = new TextFragment("Table Of Contents"); title.TextState.FontSize = 20; title.TextState.FontStyle = FontStyles.Bold; // Set the title for TOC tocInfo.Title = title; tocInfo.PageNumbersPrefix = "P"; tocPage.TocInfo = tocInfo; for (int i = 1; i < doc.Pages.Count; i++) { // Create Heading object Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1); TextSegment segment2 = new TextSegment(); heading2.TocPage = tocPage; heading2.Segments.Add(segment2); // Specify the destination page for heading object heading2.DestinationPage = doc.Pages[i + 1]; // Destination page heading2.Top = doc.Pages[i + 1].Rect.Height; // Destination coordinate segment2.Text = "Page " + i.ToString(); // Add heading to page containing TOC tocPage.Paragraphs.Add(heading2); } // Save the updated document doc.Save(outFile); //ExEnd: CustomizePageNumbesWhileAddingTOC }
public static void Run() { // ExStart:CreatePDFwithTaggedText // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); Document doc = new Document(); Aspose.Pdf.Page page1 = doc.Pages.Add(); Aspose.Pdf.Page page2 = doc.Pages.Add(); Aspose.Pdf.Page page3 = doc.Pages.Add(); // Create TextState and configure it Aspose.Pdf.Text.TextState ts = new Aspose.Pdf.Text.TextState(); ts.Font = FontRepository.FindFont("Arial"); // Creating tagged text element i.e. Supported tags P, H,H1-H6 TaggedPdfBlockLevelTextElement textElement1 = new TaggedPdfBlockLevelTextElement(doc, "P", "text", ts); TaggedPdfBlockLevelTextElement textElement2 = new TaggedPdfBlockLevelTextElement(doc, "P", "test1", ts); TaggedPdfBlockLevelTextElement textElement3 = new TaggedPdfBlockLevelTextElement(doc, "P", "test2", ts); TaggedPdfBlockLevelTextElement textElement4 = new TaggedPdfBlockLevelTextElement(doc, "P", "test3", ts); TaggedPdfBlockLevelTextElement textElement5 = new TaggedPdfBlockLevelTextElement(doc, "P", "test4", ts); TaggedPdfBlockLevelTextElement textElement6 = new TaggedPdfBlockLevelTextElement(doc, "P", "test5", ts); TaggedPdfBlockLevelTextElement textElement7 = new TaggedPdfBlockLevelTextElement(doc, "P", "test6", ts); TaggedPdfBlockLevelTextElement textElement8 = new TaggedPdfBlockLevelTextElement(doc, "P", "test7", ts); // Add tagged text element to content page1.TaggedPdfContent.Add(textElement1); page1.TaggedPdfContent.Add(textElement2); page1.TaggedPdfContent.Add(textElement3); page2.TaggedPdfContent.Add(textElement4); page2.TaggedPdfContent.Add(textElement5); page3.TaggedPdfContent.Add(textElement6); page3.TaggedPdfContent.Add(textElement7); page3.TaggedPdfContent.Add(textElement8); // Save PDF Document doc.Save(dataDir + "PDFwithTaggedText.pdf"); // ExEnd:CreatePDFwithTaggedText }
public static void Run() { // ExStart:2 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); int alpha = 10; int green = 0; int red = 100; int blue = 0; Color alphaColor = Color.FromArgb(alpha, red, green, blue); Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); Aspose.Pdf.Page page = doc.Pages.Add(); Aspose.Pdf.Text.TextFragment t1 = new Aspose.Pdf.Text.TextFragment("paragraph 3 segment"); t1.TextState.ForegroundColor = Color.Red; t1.IsInLineParagraph = true; t1.TextState.FontSize = 12; Aspose.Pdf.FloatingBox TextFloatingBox1 = new Aspose.Pdf.FloatingBox(117, 21); TextFloatingBox1.ZIndex = 1; TextFloatingBox1.Left = -4; TextFloatingBox1.Top = -4; page.Paragraphs.Add(TextFloatingBox1); TextFloatingBox1.Paragraphs.Add(t1); Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(); image1.File = dataDir + "test_image.png"; Aspose.Pdf.FloatingBox ImageFloatingBox = new Aspose.Pdf.FloatingBox(117, 21); page.Paragraphs.Add(ImageFloatingBox); ImageFloatingBox.Left = -4; ImageFloatingBox.Top = -4; ImageFloatingBox.ZIndex = 2; ImageFloatingBox.Paragraphs.Add(image1); doc.Save(dataDir + @"Multilayer-2ndApproach_out.pdf"); // ExEnd:2 }
public static void Run() { // ExStart:ExportExcelWorksheetDataToTable // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(new FileStream(dataDir + "newBook1.xlsx", FileMode.Open)); // Accessing the first worksheet in the Excel file Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0]; // Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable DataTable dataTable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, true); // Instantiate a Document instanc Aspose.Pdf.Document pdf1 = new Aspose.Pdf.Document(); // Create a page in the document instance Aspose.Pdf.Page sec1 = pdf1.Pages.Add(); // Create a Table object Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table(); // Add the Table object in the paragraphs collection of the section sec1.Paragraphs.Add(tab1); // Set column widths of the table. We need to specify the ColumnCount manually. // As the curent excel worksheet has three columsn, so we are specifying the same count tab1.ColumnWidths = "40 100 100"; // Set default cell border of the table using BorderInfo object tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F); // Import data into the Table object from the DataTable created above tab1.ImportDataTable(dataTable, true, 0, 0, dataTable.Rows.Count + 1, dataTable.Columns.Count); // Get 1st row from the table Aspose.Pdf.Row row1 = tab1.Rows[0]; // Iterate through all cells in the 1st row and set their background color to blue foreach (Aspose.Pdf.Cell curCell in row1.Cells) { // Set the background of all the cells in 1st row of the table. curCell.BackgroundColor = Color.Blue; // Set the font face for the cells of 1st row in the table. curCell.DefaultCellTextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Helvetica-Oblique"); // Set the font Color of all the cells in 1st row of the table. curCell.DefaultCellTextState.ForegroundColor = Color.Yellow; // Set the text allignment for the cells of 1st row as Center. curCell.DefaultCellTextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center; } for (int All_Rows = 1; All_Rows <= dataTable.Rows.Count; All_Rows++) { // Iterate through all cells in the 1st row and set their background color to blue foreach (Aspose.Pdf.Cell curCell in tab1.Rows[All_Rows].Cells) { // Set the background color of all the cells except of the 1st row. curCell.BackgroundColor = Color.Gray; // Set the Text color of all the cells except the 1st row. curCell.DefaultCellTextState.ForegroundColor = Color.White; } } // Save the Pdf pdf1.Save(dataDir + @"Exceldata_toPdf_table.pdf"); // ExEnd:ExportExcelWorksheetDataToTable }
public void GeneratePDF(SendLetter sendLetter, Guid LetterTemplate) { MemoryStream stream; if (sendLetter.sendLetterFiles == null || sendLetter.sendLetterFiles.Count == 0) { //Create Template and Print letter to Database TemplateManager templateManager = new TemplateManager(); Template template = templateManager.Read(LetterTemplate); stream = new MemoryStream(template.File.Content); if (template != null) { Document doc = new Document(stream); ReportingEngine engine = new ReportingEngine { Options = ReportBuildOptions.AllowMissingMembers }; PrintableSendLetter printableSendLetter = new PrintableSendLetter { DateTime = sendLetter.LetterDateTime.ToPersianTime().toPersianNumber(), Appendage = (sendLetter.Appendages != null && sendLetter.Appendages.Count > 0) ? "دارد" : "ندارد", Number = sendLetter.RowNumber.toPersianNumber(), Body = HttpUtility.HtmlDecode(sendLetter.Body).toPersianNumber(), SenderName = (sendLetter.SenderEmployee?.Employee != null) ? sendLetter.SenderEmployee?.Employee.FullName() : "", //DepartMentName = (sendLetter.SenderEmployee?.Post != null ? sendLetter.SenderEmployee.Post.Name : "") + " " + (sendLetter.SenderEmployee?.Department != null ? sendLetter.SenderEmployee.Department.Name : "") DepartMentName = sendLetter.SenderEmployee?.Post != null?sendLetter.SenderEmployee.Post.Name:"" }; if (sendLetter.Recievers != null && sendLetter.Recievers.Count > 0 && sendLetter.Recievers.First()?.Person != null) { StringBuilder subtitleStbBuilder = new StringBuilder(); subtitleStbBuilder.Append(sendLetter.Recievers.First().Person.Gender == 0 ? "سرکار خانم " : "جناب آقاي "); subtitleStbBuilder.Append(sendLetter.Recievers.First().Person.FullName()); printableSendLetter.SubTitle = subtitleStbBuilder.ToString(); } if (sendLetter.Recievers != null && sendLetter.Recievers.Count > 0 && sendLetter.Recievers.First().Organization != null) { StringBuilder titleStbBuilder = new StringBuilder(); titleStbBuilder.Append(""); if (sendLetter.Recievers.First().Organization != null) { if (sendLetter.Recievers.First().Post != null) { titleStbBuilder.Append(sendLetter.Recievers.First().Post.Name + " محترم "); titleStbBuilder.Append(sendLetter.Recievers.First().Organization.GetFullOrganizationPath()); } printableSendLetter.Title = titleStbBuilder.ToString(); } } // Execute the build report. engine.BuildReport(doc, printableSendLetter, "s"); //Save letter without Appendage as pdf stream = new MemoryStream(); doc.Save(stream, SaveFormat.Pdf); sendLetter.LetterFileContent = stream.ToArray(); } else { //Should send error to User throw new Exception("قالب انتخاب شده یافت نگردید"); } } else { //Render Letter Content With Files Aspose.Pdf.Document contentPdf = new Aspose.Pdf.Document(); foreach (var file in sendLetter.sendLetterFiles) { 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); sendLetter.LetterFileContent = stream.ToArray(); } //Debug //Aspose.Pdf.Document letterFile = new Aspose.Pdf.Document(stream); //letterFile.Save(Server.MapPath("~/LetterOnly.pdf")); //Save letter with Appendage if (sendLetter.Appendages != null && sendLetter.Appendages.Count > 0) { Aspose.Pdf.Document appendagePdf = new Aspose.Pdf.Document(); foreach (var file in sendLetter.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 Aspose.Pdf.Document letterPdf = new Aspose.Pdf.Document(stream); stream = new MemoryStream(); appendagePdf.Save(stream); sendLetter.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")); sendLetter.LetterFileAppendageContent = stream.ToArray(); } }
public static void Run() { // ExStart:PlacingTextAroundImage // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Instantiate document object Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); // Create a page in the Pdf Aspose.Pdf.Page page = doc.Pages.Add(); // Instantiate a table object Aspose.Pdf.Table table1 = new Aspose.Pdf.Table(); // Add the table in paragraphs collection of the desired section page.Paragraphs.Add(table1); // Set with column widths of the table table1.ColumnWidths = "120 270"; // 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; // Create rows in the table and then cells in the rows Aspose.Pdf.Row row1 = table1.Rows.Add(); // Create an image object Aspose.Pdf.Image logo = new Aspose.Pdf.Image(); // Specify the image file path logo.File = dataDir + "aspose-logo.jpg"; // Specify the image Fixed Height logo.FixHeight = 120; // Specify the image Fixed Width logo.FixWidth = 110; row1.Cells.Add(); // Add the image to paragraphs collection of the table cell row1.Cells[0].Paragraphs.Add(logo); // Create string variables with text containing html tags string TitleString = "<font face=\"Arial\" size=6 color=\"#101090\"><b> Aspose.Pdf for .NET</b></font>"; string BodyString1 = "<font face=\"Arial\" size=2><br/>Aspose.Pdf for .NET is a non-graphical PDF� document reporting component that enables .NET applications to <b> create PDF documents from scratch </b> without utilizing Adobe Acrobat�. Aspose.Pdf for .NET is very affordably priced and offers a wealth of strong features including: compression, tables, graphs, images, hyperlinks, security and custom fonts. </font>"; // Create a text object to be added to the right of image Aspose.Pdf.HtmlFragment TitleText = new Aspose.Pdf.HtmlFragment(TitleString + BodyString1); row1.Cells.Add(); // Add the text paragraphs containing HTML text to the table cell row1.Cells[1].Paragraphs.Add(TitleText); // Set the vertical alignment of the row contents as Top row1.Cells[1].VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top; // Create rows in the table and then cells in the rows Aspose.Pdf.Row SecondRow = table1.Rows.Add(); SecondRow.Cells.Add(); // Set the row span value for Second row as 2 SecondRow.Cells[0].ColSpan = 2; // Set the vertical alignment of the second row as Top SecondRow.Cells[0].VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top; string SecondRowString = "<font face=\"Arial\" size=2>Aspose.Pdf for .NET supports the creation of PDF files through API and XML or XSL-FO templates. Aspose.Pdf for .NET is very easy to use and is provided with 14 fully featured demos written in both C# and Visual Basic.</font>"; Aspose.Pdf.HtmlFragment SecondRowText = new Aspose.Pdf.HtmlFragment(SecondRowString); // Add the text paragraphs containing HTML text to the table cell SecondRow.Cells[0].Paragraphs.Add(SecondRowText); // Save the Pdf file doc.Save(dataDir + "PlacingTextAroundImage_out.pdf"); // ExEnd:PlacingTextAroundImage }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); string outFile = dataDir + "AddRepeatingColumn_out.pdf"; // Create a new document Document doc = new Document(); Aspose.Pdf.Page page = doc.Pages.Add(); // Instantiate an outer table that takes up the entire page Aspose.Pdf.Table outerTable = new Aspose.Pdf.Table(); outerTable.ColumnWidths = "100%"; outerTable.HorizontalAlignment = HorizontalAlignment.Left; // Instantiate a table object that will be nested inside outerTable that will break inside the same page Aspose.Pdf.Table mytable = new Aspose.Pdf.Table(); mytable.Broken = TableBroken.VerticalInSamePage; mytable.ColumnAdjustment = ColumnAdjustment.AutoFitToContent; // Add the outerTable to the page paragraphs // Add mytable to outerTable page.Paragraphs.Add(outerTable); var bodyRow = outerTable.Rows.Add(); var bodyCell = bodyRow.Cells.Add(); bodyCell.Paragraphs.Add(mytable); mytable.RepeatingColumnsCount = 5; page.Paragraphs.Add(mytable); // Add header Row Aspose.Pdf.Row row = mytable.Rows.Add(); row.Cells.Add("header 1"); row.Cells.Add("header 2"); row.Cells.Add("header 3"); row.Cells.Add("header 4"); row.Cells.Add("header 5"); row.Cells.Add("header 6"); row.Cells.Add("header 7"); row.Cells.Add("header 11"); row.Cells.Add("header 12"); row.Cells.Add("header 13"); row.Cells.Add("header 14"); row.Cells.Add("header 15"); row.Cells.Add("header 16"); row.Cells.Add("header 17"); for (int RowCounter = 0; RowCounter <= 5; RowCounter++) { // Create rows in the table and then cells in the rows Aspose.Pdf.Row row1 = mytable.Rows.Add(); row1.Cells.Add("col " + RowCounter.ToString() + ", 1"); row1.Cells.Add("col " + RowCounter.ToString() + ", 2"); row1.Cells.Add("col " + RowCounter.ToString() + ", 3"); row1.Cells.Add("col " + RowCounter.ToString() + ", 4"); row1.Cells.Add("col " + RowCounter.ToString() + ", 5"); row1.Cells.Add("col " + RowCounter.ToString() + ", 6"); row1.Cells.Add("col " + RowCounter.ToString() + ", 7"); row1.Cells.Add("col " + RowCounter.ToString() + ", 11"); row1.Cells.Add("col " + RowCounter.ToString() + ", 12"); row1.Cells.Add("col " + RowCounter.ToString() + ", 13"); row1.Cells.Add("col " + RowCounter.ToString() + ", 14"); row1.Cells.Add("col " + RowCounter.ToString() + ", 15"); row1.Cells.Add("col " + RowCounter.ToString() + ", 16"); row1.Cells.Add("col " + RowCounter.ToString() + ", 17"); } doc.Save(outFile); // ExEnd:1 }
public void GeneratePDF(SendLetter sendLetter) { MemoryStream stream = null; if (sendLetter.sendLetterFiles != null && sendLetter.sendLetterFiles.Count > 0) { //Render Letter Content With Files Aspose.Pdf.Document contentPdf = new Aspose.Pdf.Document(); foreach (var file in sendLetter.sendLetterFiles) { 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); sendLetter.LetterFileContent = stream.ToArray(); } //Debug //Aspose.Pdf.Document letterFile = new Aspose.Pdf.Document(stream); //letterFile.Save(Server.MapPath("~/LetterOnly.pdf")); //Save letter with Appendage if (sendLetter.Appendages != null && sendLetter.Appendages.Count > 0) { Aspose.Pdf.Document appendagePdf = new Aspose.Pdf.Document(); foreach (var file in sendLetter.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) { Aspose.Pdf.Document letterPdf = new Aspose.Pdf.Document(stream); stream = new MemoryStream(); appendagePdf.Save(stream); sendLetter.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")); sendLetter.LetterFileAppendageContent = stream.ToArray(); } else { stream = new MemoryStream(); appendagePdf.Save(stream); sendLetter.AppendageContent = stream.ToArray(); sendLetter.LetterFileAppendageContent = stream.ToArray(); } } }