简体   繁体   中英

pdfSharp document orientation

I want to change the orientation of a document created in c# wpf with pdfSharp, What should I do if I want to have a landscape document

here is my code

        private void savePDF()
    {

        string date = DateTime.Now.ToString().Replace('/', '-').Replace(':', '-');

        MemoryStream lMemoryStream = new MemoryStream();
        Package package = Package.Open(lMemoryStream, FileMode.Create);
        XpsDocument doc = new XpsDocument(package);
        XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
        writer.Write(rapport);
        doc.Close();
        package.Close();
        var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
        PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, "E:\\"+ date +".pdf", 0);



    }

Firstly Write your rapport to a FixedDocument :

FixedDocument fixedDoc = new FixedDocument();
    PageContent pageContent = new PageContent();
    FixedPage fixedPage = new FixedPage();


      string savedrapport = XamlWriter.Save(rapport);

        StringReader stringReader = new StringReader(savedrapport);
        XmlReader xmlReader = XmlReader.Create(stringReader);
        Grid newrapportInstance = (Grid)XamlReader.Load(xmlReader);

        //Create first page of document
        fixedPage.Children.Add(newrapportInstance);
    ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
    // hard coded for A4
    fixedPage.Width = 11.69 * 96;
    fixedPage.Height = 8.27 * 96;
    fixedDoc.Pages.Add(pageContent);
    //Create any other required pages here

    MemoryStream lMemoryStream = new MemoryStream();
    Package package = Package.Open(lMemoryStream, FileMode.Create);
    XpsDocument doc = new XpsDocument(package);
    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
    writer.Write(fixedDoc);
    doc.Close();
    package.Close();
    var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
    PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, "E:\\"+ date +".pdf", 0);

You can use printdialog,set the standard page size and orientation, the transform the width and height to fixeddocument page size. Don't set the actual number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM