itextsharp foe word document

itextsharp foe word document

iTextSharp: A Complete Information to Working with Phrase Paperwork

Hey readers,

Welcome to our in-depth information on iTextSharp, the highly effective .NET library for working with Phrase paperwork. On this article, we’ll dive deep into the capabilities of iTextSharp, exploring its options, makes use of, and finest practices.

Part 1: Getting Began with iTextSharp

Getting began with iTextSharp is a breeze. Set up the library utilizing NuGet or the .NET SDK and create a brand new undertaking. To work with Phrase paperwork, you will use the iTextSharp.textual content.Doc object. You may create a brand new Phrase doc or open an current one utilizing the Doc constructor.

Part 2: Including Content material to Your Phrase Doc

iTextSharp gives a variety of choices for including content material to your Phrase doc. You may add textual content, pictures, tables, and extra. So as to add textual content, merely use the Paragraph or Chunk objects. You may as well add pictures and tables utilizing the Picture and Desk objects respectively.

Part 3: Formatting Your Phrase Doc

As soon as you have added content material to your Phrase doc, you possibly can format it to your liking. iTextSharp gives a wide range of formatting choices, reminiscent of setting font model, dimension, and colour. You may as well align textual content, create borders, and add web page numbers.

Part 4: Dealing with Occasions

iTextSharp lets you deal with occasions that happen in the course of the doc creation course of. For instance, you possibly can deal with the OnDocumentOpen occasion to carry out actions when a doc is opened, or the OnDocumentClose occasion to carry out actions when a doc is closed.

Part 5: Exporting and Printing

As soon as you have completed working in your Phrase doc, you possibly can export it to totally different file codecs, together with PDF, HTML, and DOCX. You may as well print the doc instantly from iTextSharp.

Part 6: Superior Options

iTextSharp gives a variety of superior options, permitting you to create complicated Phrase paperwork. These embody assist for customized headers and footers, watermarks, bookmarks, and annotations.

Part 7: Desk: Comparability of iTextSharp Options

Characteristic Description
Doc creation Creates and opens Phrase paperwork
Content material addition Add textual content, pictures, tables, and extra
Formatting Set font kinds, sizes, colours, and alignment
Occasion dealing with Deal with occasions that happen throughout doc creation
Exporting and printing Export paperwork to a number of file codecs and print
Superior options Create complicated paperwork with customized headers, watermarks, and annotations

Conclusion

iTextSharp is a robust and versatile library that makes it straightforward to work with Phrase paperwork in .NET. Whether or not you are creating new paperwork, modifying current ones, or dealing with superior duties, iTextSharp has you coated.

For extra info, remember to try our different articles on iTextSharp:

FAQs about itextsharp for Phrase Paperwork

What’s itextsharp?

iTextSharp is an open-source .NET library for working with PDF paperwork. It permits builders to create, edit, learn, and manipulate PDF recordsdata.

Can itextsharp be used for Phrase paperwork?

Whereas itextsharp primarily works with PDF paperwork, it can be used to transform Phrase (DOCX) paperwork to PDF format.

How do I convert a Phrase doc to PDF utilizing itextsharp?

utilizing iTextSharp.textual content.pdf;
utilizing iTextSharp.textual content.pdf.parser;
utilizing System.IO;

class WordToPdf
{
    public static void Most important(string[] args)
    {
        // Path to the Phrase doc
        string wordFilePath = @"pathtoinput.docx";

        // Path to the output PDF file
        string pdfFilePath = @"pathtooutput.pdf";

        // Convert the Phrase doc to PDF
        utilizing (Stream inputStream = new FileStream(wordFilePath, FileMode.Open, FileAccess.Learn))
        {
            utilizing (Stream outputStream = new FileStream(pdfFilePath, FileMode.Create, FileAccess.Write))
            {
                PdfWriter author = new PdfWriter(outputStream);
                utilizing (PdfDocument doc = new PdfDocument(inputStream))
                {
                    PdfImportedPage web page;

                    for (int i = 1; i <= doc.GetNumberOfPages(); i++)
                    {
                        web page = author.ImportPage(doc.GetPage(i));
                        author.AddPage(web page);
                    }
                }
            }
        }
    }
}

Can itextsharp be used to edit Phrase paperwork?

No, itextsharp can’t be used to instantly edit Phrase paperwork. It primarily works with PDF recordsdata.

Can itextsharp be used to extract textual content from Phrase paperwork?

Sure, itextsharp can be utilized to extract textual content from Phrase paperwork by changing them to PDF first, after which utilizing the itextsharp PDF parser to extract the textual content.

How do I parse textual content from a Phrase doc utilizing itextsharp?

utilizing iTextSharp.textual content.pdf;
utilizing iTextSharp.textual content.pdf.parser;
utilizing System;
utilizing System.IO;

class ParseText
{
    public static void Most important(string[] args)
    {
        // Path to the Phrase doc
        string wordFilePath = @"pathtoinput.docx";

        // Convert the Phrase doc to PDF
        string pdfFilePath = ConvertWordToPdf(wordFilePath);

        // Parse the textual content from the PDF file
        utilizing (Stream inputStream = new FileStream(pdfFilePath, FileMode.Open, FileAccess.Learn))
        {
            utilizing (PdfReader reader = new PdfReader(inputStream))
            {
                StringBuilder textual content = new StringBuilder();

                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    textual content.Append(PdfTextExtractor.GetTextFromPage(reader, i));
                }

                Console.WriteLine(textual content.ToString());
            }
        }

        // Delete the non permanent PDF file
        File.Delete(pdfFilePath);
    }

    personal static string ConvertWordToPdf(string wordFilePath)
    {
        // Generate a singular file title for the PDF
        string pdfFilePath = Path.GetTempFileName() + ".pdf";

        // Convert the Phrase doc to PDF
        utilizing (Stream inputStream = new FileStream(wordFilePath, FileMode.Open, FileAccess.Learn))
        {
            utilizing (Stream outputStream = new FileStream(pdfFilePath, FileMode.Create, FileAccess.Write))
            {
                PdfWriter author = new PdfWriter(outputStream);
                utilizing (PdfDocument doc = new PdfDocument(inputStream))
                {
                    PdfImportedPage web page;

                    for (int i = 1; i <= doc.GetNumberOfPages(); i++)
                    {
                        web page = author.ImportPage(doc.GetPage(i));
                        author.AddPage(web page);
                    }
                }
            }
        }

        return pdfFilePath;
    }
}

Can itextsharp be used to create Phrase paperwork?

No, itextsharp can’t be used to instantly create Phrase paperwork. It primarily works with PDF recordsdata.

What are the constraints of utilizing itextsharp with Phrase paperwork?

itextsharp can solely convert Phrase paperwork to PDF and extract textual content from them. It can’t be used to edit or create Phrase paperwork instantly.

Are there every other libraries that can be utilized to work with Phrase paperwork?

Sure, there are different libraries reminiscent of Aspose.Phrases, DocumentFormat.OpenXml, and GemBox.Doc that can be utilized to work with Phrase paperwork.

Can itextsharp be used for different doc codecs?

Sure, itextsharp can be used to work with different doc codecs reminiscent of HTML, RTF, and ODT.