CSS
CSS

Reputation: 182

ABCPdf change orientation of a specific page

The code below creates a new PDF with landscape orientation. It uses ABCPdf component.

static void Main(string[] args)
{
    var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "da.pdf");

    var theDoc = new Doc();
    //theDoc.Read(filePath);

    // apply a rotation transform
    theDoc.MediaBox.String = "Legal";
    double w = theDoc.MediaBox.Width;
    double h = theDoc.MediaBox.Height;
    double l = theDoc.MediaBox.Left;
    double b = theDoc.MediaBox.Bottom;
    theDoc.Transform.Rotate(90, l, b);
    theDoc.Transform.Translate(w, 0);

    // rotate our rectangle
    theDoc.Rect.Width = h;
    theDoc.Rect.Height = w;

    // add some text
    theDoc.Rect.Inset(50, 50);
    theDoc.FontSize = 96;
    theDoc.AddText("Landscape Orientation");
    theDoc.AddPage();
    theDoc.PageNumber = theDoc.PageCount;
    theDoc.AddText("Page 2");

    // adjust the default rotation and save
    int theID = theDoc.GetInfoInt(theDoc.Root, "Pages");
    theDoc.SetInfo(theID, "/Rotate", "90");
    theDoc.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "out.pdf"));
    theDoc.Clear();
}

Instead of creating new pdf, I would like to open an existing PDF and change the orientation of a specific page to landscape using ABCPdf. like 1st page will be in Portrait and 2nd will be on Landscape.

Thanks

Upvotes: 1

Views: 1736

Answers (1)

Morcilla de Arroz
Morcilla de Arroz

Reputation: 2182

You can do so, or use "AddImageDoc" method, to insert a modified page...

BUT

Abcpdf doesn't allow to overwrite the source file. From "Save Method Documentation":

ABCpdf operates an intelligent just-in-time object loading scheme which ensures that only those objects that are required are loaded into memory. This means that if you are modifying large documents then server load will be kept to a minimum. The original PDF document must be available for as long as the Doc object is being used.

As a result you cannot modify or overwrite a PDF file while it is read into a Doc object. You will need to save your PDF to another location and then swap the two files around after the Doc object's use of the PDF is ended (with a call to Clear, Dispose, or Read with another PDF file).

So you will need always a "new pdf".

Upvotes: 2

Related Questions