private Aspose.Pdf.Image getImage(HNodeTag imageNode) { /* * int height = 0; * int width = 0; * * using (WebClient webClient = new WebClient()) * { * //var watch = System.Diagnostics.Stopwatch.StartNew(); * * byte[] data = webClient.DownloadData(imageUrl); * * using (MemoryStream imageStream = new MemoryStream(data)) * { * // Add image to Images collection of Page Resources * pdfPage.Resources.Images.Add(imageStream); * * height = pdfPage.Resources.Images[pdfPage.Resources.Images.Count].Height; * width = pdfPage.Resources.Images[pdfPage.Resources.Images.Count].Width; * } * } * //return pdfPage.Resources.Images[pdfPage.Resources.Images.Count]; */ //pdfImage = getImage(((node as HNodeTag).GetAttribute("src", ""))); Aspose.Pdf.Image image = null; string imageUrl = imageNode.GetAttribute("src", ""); try { using (WebClient webClient = new WebClient()) { byte[] imageData = webClient.DownloadData(imageUrl); using (MemoryStream imageStream = new MemoryStream(imageData)) { System.Drawing.Image img = System.Drawing.Image.FromStream(imageStream); image = new Aspose.Pdf.Image(); image.File = imageUrl; image.FixHeight = img.Height; image.FixWidth = img.Width; } } } catch { } return(image); }
private Aspose.Pdf.Forms.Field getFormField(HNodeTag inputNode) { Aspose.Pdf.Forms.Field field = null; string fieldType = inputNode.GetAttribute("type", "text"); //button|checkbox|file|hidden|image|password|radio|reset|submit|text switch (fieldType) { case "button": case "submit": field = getFormField_Button(); break; case "checkbox": field = getFormField_CheckBox(); break; case "file": case "hidden": case "image": case "password": case "reset": break; case "radio": field = getFormField_RadioBox(); break; case "text": default: field = getFormField_TextBox(); break; } return(field); Aspose.Pdf.Forms.Field getFormField_TextBox() { TextBoxField textBoxField = new TextBoxField(); textBoxField.Height = 16; textBoxField.Width = 160; string name = inputNode.GetAttribute("name", "text_box_field"); textBoxField.PartialName = name; string value = inputNode.GetAttribute("value", ""); if (!String.IsNullOrEmpty(value)) { textBoxField.Value = value; } //Так делать нельзя, падает исключение при pdfDocument.ProcessParagraphs(); //textBoxField.Value = ""; //Border border = new Border(field); //border.Width = 1; //border.Dash = new Dash(1, 1); //field.Border = border; return(textBoxField); } Aspose.Pdf.Forms.Field getFormField_CheckBox() { CheckboxField checkBoxField = new CheckboxField(); checkBoxField.Height = 10; checkBoxField.Width = 10; string name = inputNode.GetAttribute("name", "checkbox_field"); checkBoxField.PartialName = name; //Так делать нельзя, падает исключение при pdfDocument.ProcessParagraphs(); //checkBoxField.Checked = true; return(checkBoxField); } Aspose.Pdf.Forms.Field getFormField_RadioBox() { string name = inputNode.GetAttribute("name", "radio_button_field"); RadioButtonField rbf = getRadioButtonField(name); RadioButtonOptionField opt = new RadioButtonOptionField(); opt.OptionName = name + "_" + (rbf.Count + 1).ToString(); opt.Width = 12; opt.Height = 12; //Так делать нельзя, падает исключение при pdfDocument.ProcessParagraphs(); //opt.Border = new Border(opt); //opt.Border.Width = 1; //opt.Border.Style = BorderStyle.Solid; //opt.Characteristics.Border = System.Drawing.Color.Black; //opt.DefaultAppearance.TextColor = System.Drawing.Color.Red; //opt.Caption = new TextFragment("Item1"); rbf.Add(opt); return(opt); } Aspose.Pdf.Forms.Field getFormField_Button() { ButtonField buttonField = new ButtonField(); string value = inputNode.GetAttribute("value", ""); buttonField.Height = 18; buttonField.Width = 12 + 6 * value.Length; if (!String.IsNullOrEmpty(value)) { buttonField.Value = value; } return(buttonField); } }
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(); } // // // }