none
XML to PDF Converter along with Table of Contents RRS feed

  • Question

  • Looking for a way to generate XML to PDF file along with Table of contents 

    saikalyan

    Monday, December 16, 2019 10:43 AM

Answers

  • Hi SAI KALYAN,
    You can use

    XmlNodeList xnlNodes = xdc.SelectNodes("nodename");

    More detaisl you can refer to this link.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 20, 2019 9:05 AM

All replies

  • Hi SAI KALYAN, 
    You can read the XML file and print as PDF file.
    Here is a code example you can refer to.

    public Form1()
    {
        this.printPreviewButton = new System.Windows.Forms.Button();
        this.printPreviewButton.Text = "Print Preview";
        this.printPreviewButton.Click += new System.EventHandler(this.Button1_Click);
        this.Controls.Add(this.printPreviewButton);
        printDocument.PrintPage +=
            new PrintPageEventHandler(PrintDocument1_PrintPage);
    }
    private void ReadDocument()
    {
        string docName = "test1.xml";
        string docPath = @"C:\Users\Desktop\";
        printDocument.DocumentName = docName;
        using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
        using (StreamReader reader = new StreamReader(stream))
        {
            documentContents = reader.ReadToEnd();
        }
        stringToPrint = documentContents;
    }
    private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        int charactersOnPage = 0;
        int linesPerPage = 0;
        e.Graphics.MeasureString(stringToPrint, this.Font,
            e.MarginBounds.Size, StringFormat.GenericTypographic,
            out charactersOnPage, out linesPerPage);
        e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
        e.MarginBounds, StringFormat.GenericTypographic);
        stringToPrint = stringToPrint.Substring(charactersOnPage);
        e.HasMorePages = (stringToPrint.Length > 0);
        if (!e.HasMorePages)
            stringToPrint = documentContents;
    }
    private void Button1_Click(object sender, EventArgs e)
    {
        ReadDocument();
        printPreviewDialog.Document = printDocument;
        printPreviewDialog.ShowDialog();
    }

    Besides, what is the relationship between the table of contents and the xml file? and how the table of contents is generated?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, December 17, 2019 8:19 AM
  • This solution directly reads XML but want a solution which only reads data from nodes and not node data

    saikalyan

    Wednesday, December 18, 2019 10:07 AM
  • Hi SAI KALYAN,
    You can use

    XmlNodeList xnlNodes = xdc.SelectNodes("nodename");

    More detaisl you can refer to this link.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 20, 2019 9:05 AM
  • Hi saikalyan,

    The below example is based on a 3rd party library Spire.Doc (you can get it from NuGet.org), if you are ok, you can give it a try:

    using Spire.Doc;
    
    namespace XML2PDf
    {
        class Program
        {
            static void Main(string[] args)
            {
                Document mydoc = new Document();
                mydoc.LoadFromFile(@"..\sample.xml", FileFormat.Xml);
                //mydoc.SaveToFile("Test.doc", FileFormat.Doc);
                mydoc.SaveToFile("test.pdf", FileFormat.PDF);
                System.Diagnostics.Process.Start("Test.pdf");
            }
        }
    }      

    Monday, December 23, 2019 8:10 AM