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); }
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); }
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); }
private TextSegment getTextSegment(HNode node, TextState parentTextState) { TextSegment textSegment = null; // Text element if (node is HNodeText) { textSegment = new TextSegment(); textSegment.TextState = parentTextState; textSegment.Text = (node as HNodeText).Text; if (hyperlinkNode != null) { Aspose.Pdf.WebHyperlink pdfHyperlink = new WebHyperlink(hyperlinkNode.GetAttribute("href", "#")); textSegment.Hyperlink = pdfHyperlink; } } // New Line element <br /> if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.br) { /* * //слетают стили!!! * textSegment = new TextSegment(); * //textSegment.TextState = parentTextState; * textSegment.Text = Environment.NewLine; */ pdfNewLine = new TextSegment(); pdfNewLine.Text = Environment.NewLine; } // Hyperlink element <a> if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.a) { PUtil.TextStateUtil.TextState_ModifyForHyperlink(parentTextState); PUtil.TextStateUtil.TextState_ModifyFromHStyles((node as HNodeTag).Styles, parentTextState); } // Bold text element <b> if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.b) { PUtil.TextStateUtil.TextState_ModifyForBold(parentTextState); PUtil.TextStateUtil.TextState_ModifyFromHStyles((node as HNodeTag).Styles, parentTextState); } // Italic text element <i> if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.i) { PUtil.TextStateUtil.TextState_ModifyForItalic(parentTextState); PUtil.TextStateUtil.TextState_ModifyFromHStyles((node as HNodeTag).Styles, parentTextState); } // Image element <img> if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.img) { pdfImage = getImage(node as HNodeTag); } // Form field element <input> if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.input) { pdfFormField = getFormField(node as HNodeTag); } // Button element <button> if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.button) { pdfFormField = getFormFieldButton(node as HNodeTag); } // TODO - other inline tags return(textSegment); }
private void createNode(HNode node, TextState parentTextState) { TextState nodeTextState = new TextState(); nodeTextState.ApplyChangesFrom(parentTextState); if (node is HNodeTag) { PUtil.TextStateUtil.TextState_ModifyFromHStyles((node as HNodeTag).Styles, nodeTextState); } // Block element if ((node is HNodeTag) && HUtil.TagUtil.IsBlockTag((node as HNodeTag).TagType)) { addTextFragmentOnPage(); createTextFragmentByTagType((node as HNodeTag).TagType); } // Inline element or Text element else if ( (node is HNodeTag) && HUtil.TagUtil.IsInlineTag((node as HNodeTag).TagType) || (node is HNodeText) ) { if ((node is HNodeText) && (node as HNodeText).ParentNode != null && ((node as HNodeText).ParentNode is HNodeTag) && (((node as HNodeText).ParentNode as HNodeTag)).TagType == HTagType.button) { // } else { // Create TextSegment for element TextSegment textSegment = getTextSegment(node, nodeTextState); // New Line, <BR /> if (pdfNewLine != null) { double marginTop = 0; double marginBottom = 0; if (pdfTextFragment != null) { marginBottom = pdfTextFragment.Margin.Bottom; pdfTextFragment.Margin.Bottom = 0; } addTextFragmentOnPage(); createTextFragmentByTagType(HTagType.div); if (pdfTextFragment != null) { pdfTextFragment.Margin.Top = marginTop; pdfTextFragment.Margin.Bottom = marginBottom; } pdfNewLine = null; } // Image else if (pdfImage != null) { double imageHeight = pdfImage.FixHeight; MarginInfo margin = new MarginInfo(0, 12, 0, 12); if (pdfTextFragment == null || pdfTextFragment.Segments.Count == 0 || (pdfTextFragment.Segments.Count == 1 && pdfTextFragment.Segments[1].Text == String.Empty)) { } else { pdfTextFragment.Margin.Top += imageHeight; margin = new MarginInfo(0, pdfTextFragment.Margin.Bottom, 0, -1 * imageHeight); } addTextFragmentOnPage(false); pdfImage.IsInLineParagraph = true; pdfImage.Margin = margin; inlineParagraphMargin = margin; if (hyperlinkNode != null) { Aspose.Pdf.WebHyperlink pdfHyperlink = new WebHyperlink(hyperlinkNode.GetAttribute("href", "#")); pdfImage.Hyperlink = pdfHyperlink; } pdfPage.Paragraphs.Add(pdfImage); if (node.NextNode == null) { updateCurrentPage(); } pdfImage = null; } // Form Field Element else if (pdfFormField != null) { // // // double inputHeight = pdfFormField.Height; MarginInfo margin = new MarginInfo(0, 12, 0, 12); if (pdfTextFragment == null || pdfTextFragment.Segments.Count == 0 || (pdfTextFragment.Segments.Count == 1 && pdfTextFragment.Segments[1].Text == String.Empty)) { } else { double textFragmentHeight = pdfTextFragment.Rectangle.Height; margin = pdfTextFragment.Margin; pdfTextFragment.Margin.Bottom = textFragmentHeight - inputHeight; pdfTextFragment.Margin.Top += Math.Max(0, (inputHeight - textFragmentHeight)); pdfTextFragment.Margin.Top += inputHeight; } addTextFragmentOnPage(false); pdfFormField.IsInLineParagraph = true; pdfFormField.Margin = margin; inlineParagraphMargin = new MarginInfo(pdfFormField.Width, margin.Bottom, margin.Right, margin.Top); pdfPage.Paragraphs.Add(pdfFormField); if (node.NextNode == null) { updateCurrentPage(); } pdfFormField = null; } // TextFragment for InLineParagraph mode else if (pdfTextFragment == null) { HTagType tagTypeForTextFragment = HTagType.div; bool isInLineParagraphForTextFragment = false; bool flagPreviousImage = false; bool flagPreviousInput = false; if (node.PrevNode != null && (node.PrevNode is HNodeTag) && (node.PrevNode as HNodeTag).TagType == HTagType.img) { // prev image element if (node.ParentNode != null && (node.ParentNode is HNodeTag) && HUtil.TagUtil.IsBlockTag((node.ParentNode as HNodeTag).TagType)) { tagTypeForTextFragment = (node.ParentNode as HNodeTag).TagType; } isInLineParagraphForTextFragment = true; flagPreviousImage = true; } else if (node.PrevNode != null && (node.PrevNode is HNodeTag) && (node.PrevNode as HNodeTag).TagType == HTagType.input) { // prev input element if (node.ParentNode != null && (node.ParentNode is HNodeTag) && HUtil.TagUtil.IsBlockTag((node.ParentNode as HNodeTag).TagType)) { tagTypeForTextFragment = (node.ParentNode as HNodeTag).TagType; } isInLineParagraphForTextFragment = true; flagPreviousInput = true; } else { } createTextFragmentByTagType(tagTypeForTextFragment); pdfTextFragment.IsInLineParagraph = isInLineParagraphForTextFragment; if ((flagPreviousImage || flagPreviousInput) && inlineParagraphMargin != null) { pdfTextFragment.Margin.Top = -1 * pdfTextFragment.Rectangle.Height - inlineParagraphMargin.Bottom; pdfTextFragment.Margin.Bottom = inlineParagraphMargin.Bottom; pdfTextFragment.Margin.Left = inlineParagraphMargin.Left; inlineParagraphMargin = null; } } if (textSegment != null && pdfTextFragment != null) //if (textSegment != null) { pdfTextFragment.Segments.Add(textSegment); } } } // // Create Nodes recursively with consider the hyperlink // if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.a) { hyperlinkNode = (node as HNodeTag); } if (node is HNodeContainer) { foreach (HNode childNode in (node as HNodeContainer).ChildNodes) { createNode(childNode, nodeTextState); } } if ((node is HNodeTag) && (node as HNodeTag).TagType == HTagType.a) { hyperlinkNode = null; } // // // // // Add Text Fragment on Page (if need) // if ((node is HNodeTag) && HUtil.TagUtil.IsBlockTag((node as HNodeTag).TagType)) { addTextFragmentOnPage(); } // // // }