का उपयोग कर HTML स्वरूपित पाठ के साथ पीडीएफ टेम्पलेट एक्रोफील्ड भरें PDF
टेम्पलेट भरने के लिए मैं iTextSharp
का उपयोग कर रहा हूं। मैं जिस डेटा का उपयोग कर रहा हूं उसे डेटाबेस में रखा गया है और HTML
स्वरूपित है। मेरी समस्या यह है कि जब मैं इस पाठ के साथ AcroField
लोड करता हूं तो मुझे लाइन ब्रेक करने के लिए मिलता है, लेकिन बोल्ड और न ही इटालिसिसिंग करता है। मैंने पहले से ही HtmlWorker
का उपयोग करने का प्रयास किया है, लेकिन ऑनलाइन सभी उदाहरण दिखाते हैं कि इसे HTML
को PDF
में परिवर्तित करने के लिए उपयोग किया जा रहा है लेकिन मैं एक पीडीएफ टेम्पलेट में AcroField
सेट करने का प्रयास कर रहा हूं। किसी भी सहायता की सराहना की जाएगी।iTextSharp
उत्तर
फ़ोरम और iTextsharp स्रोत कोड के माध्यम से दिन व्यतीत करने के बाद मुझे एक समाधान मिला। एचटीएमएल स्वरूपित पाठ के साथ एक्रोफील्ड भरने के बजाय, मैंने कॉलमटेक्स्ट का उपयोग किया। मैं एचटीएमएल पाठ को पार्स करता हूं और आईलेमेंट्स को पैराग्राफ में लोड करता हूं। फिर कॉलमटेक्स्ट में पैराग्राफ जोड़ें। फिर मैंने क्षेत्र के निर्देशांक का उपयोग करके Acrofield होना चाहिए, जहां शीर्ष पर ColumnText overlaid।
public void AddHTMLToContent(String htmlText,PdfContentByte contentBtye,IList<AcroFields.FieldPosition> pos)
{
Paragraph par = new Paragraph();
ColumnText c1 = new ColumnText(contentBtye);
try
{
List<IElement> elements = HTMLWorker.ParseToList(new StringReader(htmlText),null);
foreach (IElement element in elements)
{
par.Add(element);
}
c1.AddElement(par);
c1.SetSimpleColumn(pos[0].position.Left, pos[0].position.Bottom, pos[0].position.Right, pos[0].position.Top);
c1.Go(); //very important!!!
}
catch (Exception ex)
{
throw;
}
}
यहां इस फ़ंक्शन के लिए कॉल का एक उदाहरण दिया गया है।
string htmlText ="<b>Hello</b><br /><i>World</i>";
IList<AcroFields.FieldPosition> pos = form.GetFieldPositions("Field1");
//Field1 is the name of the field in the PDF Template you are trying to fill/overlay
AddHTMLToContent(htmlText, stamp.GetOverContent(pos[0].page), pos);
//stamp is the PdfStamper in this example
ऐसा करने के दौरान मैंने एक चीज में भाग लिया यह तथ्य है कि मेरे एक्रोफील्ड के पास एक पूर्वनिर्धारित फ़ॉन्ट आकार था। चूंकि इस फ़ंक्शन ने फ़ील्ड पर कॉलमटेक्स्ट को शीर्ष पर सेट किया है, इसलिए फ़ंक्शन में कोई भी फ़ॉन्ट परिवर्तन करना होगा। यहां फ़ॉन्ट आकार बदलने का एक उदाहरण दिया गया है:
public void AddHTMLToContent(String htmlText,PdfContentByte contentBtye,IList<AcroFields.FieldPosition> pos)
{
Paragraph par = new Paragraph();
ColumnText c1 = new ColumnText(contentBtye);
try
{
List<IElement> elements = HTMLWorker.ParseToList(new StringReader(htmlText),null);
foreach (IElement element in elements)
{
foreach (Chunk chunk in element.Chunks)
{
chunk.Font.Size = 14;
}
}
par.Add(elements[0]);
c1.AddElement(par);
c1.SetSimpleColumn(pos[0].position.Left, pos[0].position.Bottom, pos[0].position.Right, pos[0].position.Top);
c1.Go();//very important!!!
}
catch (Exception ex)
{
throw;
}
}
मुझे आशा है कि यह भविष्य में किसी को कुछ समय और ऊर्जा बचाएगा।
तुम्हें यकीन मदद की ... यह कुछ हद तक जटिल दृष्टिकोण काम का उपयोग कर के बारे में यह पोस्ट देखें। अपने निष्कर्ष पोस्ट करने के लिए धन्यवाद! यह एक शर्म की बात है कि 'फ़ील्ड.SetFieldRichValue' काम नहीं करता है। मैं अभी iTextSharp 5.4.2 का उपयोग कर रहा हूं। –
तो, मुझे पिछले कुछ महीनों में इस कोड को थोड़ा सा ट्विक करना पड़ा, और मुझे ऐसा करने के तरीके के बारे में बेहतर/कम दौर मिला।
public void Final(string text,string fieldName,string filename)
{
iTextSharp.text.pdf.PdfReader reader = null;
iTextSharp.text.pdf.PdfStamper stamp = null;
reader = new PdfReader(file path to template);
stamp = new PdfStamper(reader, new FileStream(path to new file, FileMode.CreateNew));
AcroFields form = stamp.AcroFields;
//get the position of the field
IList<AcroFields.FieldPosition> pos = form.GetFieldPositions(fieldName);
//tell itextSharp to overlay this content
PdfContentByte contentBtye = stamp.GetOverContent(pos[0].page);
//create a new paragraph
Paragraph par = new Paragraph();
//parse html
List<IElement> elements = HTMLWorker.ParseToList(new StringReader(text), null);
for (int k = 0; k < elements.Count; k++)
{
par.Add((IElement)elements[k]);
}
//create a ColumnText to hold the paragraph and set position to the position of the field
ColumnText ct = new ColumnText(contentBtye);
ct.SetSimpleColumn(pos[0].position.Left, pos[0].position.Bottom, pos[0].position.Right, pos[0].position.Top);
ct.AddElement(par);
ct.Go();
stamp.Close();
reader.Close();
}
- 1. iTextSharp #
- 2. iTextSharp
- 3. iTextSharp
- 4. iTextSharp
- 5. iTextSharp
- 6. iTextSharp
- 7. iTExtsharp
- 8. itextsharp
- 9. iTextSharp
- 10. iTextSharp
- 11. iTextSharp
- 12. iTextSharp
- 13. itextsharp
- 14. Itextsharp
- 15. iTextSharp
- 16. iTextSharp
- 17. iTextSharp
- 18. iTextSharp
- 19. ITextSharp
- 20. ITextSharp
- 21. iTextSharp
- 22. iTextSharp
- 23. iTextSharp
- 24. itextsharp
- 25. iTextSharp
- 26. itextsharp
- 27. iTextSharp
- 28. iTextSharp
- 29. itextsharp
- 30. iTextSharp
रिच टेक्स्ट फ़ील्ड http://stackoverflow.com/a/4412527/231316 –