Example #1
0
        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);
        }
Example #2
0
        public static void LoadingFontFromStream()
        {
            // ExStart:LoadingFontFromStream
            // The path to the documents directory.
            string dataDir  = RunExamples.GetDataDir_AsposePdf_Text();
            string fontFile = "";

            // Load input PDF file
            Document doc = new Document(dataDir + "input.pdf");
            // Create text builder object for first page of document
            TextBuilder textBuilder = new TextBuilder(doc.Pages[1]);
            // Create text fragment with sample string
            TextFragment textFragment = new TextFragment("Hello world");

            if (fontFile != "")
            {
                // Load the TrueType font into stream object
                using (FileStream fontStream = File.OpenRead(fontFile))
                {
                    // Set the font name for text string
                    textFragment.TextState.Font = FontRepository.OpenFont(fontStream, FontTypes.TTF);
                    // Specify the position for Text Fragment
                    textFragment.Position = new Position(10, 10);
                    // Add the text to TextBuilder so that it can be placed over the PDF file
                    textBuilder.AppendText(textFragment);
                }

                dataDir = dataDir + "LoadingFontFromStream_out.pdf";

                // Save resulting PDF document.
                doc.Save(dataDir);
            }
            // ExEnd:LoadingFontFromStream
            Console.WriteLine("\nFont from stream loaded successfully.\nFile saved at " + dataDir);
        }
        public static void Run()
        {
            // ExStart:1
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_Text();

            string fontFile = dataDir + "HPSimplified.TTF";
            // Load input PDF file
            Document doc = new Document();
            //Create TextFormattingOptions with LineSpacingMode.FullSize
            TextFormattingOptions formattingOptions = new TextFormattingOptions();

            formattingOptions.LineSpacing = TextFormattingOptions.LineSpacingMode.FullSize;

            // Create text builder object for first page of document
            //TextBuilder textBuilder = new TextBuilder(doc.Pages[1]);
            // Create text fragment with sample string
            TextFragment textFragment = new TextFragment("Hello world");

            if (fontFile != "")
            {
                // Load the TrueType font into stream object
                using (FileStream fontStream = System.IO.File.OpenRead(fontFile))
                {
                    // Set the font name for text string
                    textFragment.TextState.Font = FontRepository.OpenFont(fontStream, FontTypes.TTF);
                    // Specify the position for Text Fragment
                    textFragment.Position = new Position(100, 600);
                    //Set TextFormattingOptions of current fragment to predefined(which points to LineSpacingMode.FullSize)
                    textFragment.TextState.FormattingOptions = formattingOptions;
                    // Add the text to TextBuilder so that it can be placed over the PDF file
                    //textBuilder.AppendText(textFragment);
                    var page = doc.Pages.Add();
                    page.Paragraphs.Add(textFragment);
                }

                dataDir = dataDir + "SpecifyLineSpacing_out.pdf";
                // Save resulting PDF document
                doc.Save(dataDir);
            }
            // ExEnd:1
        }