FileOutputStream equivalent
Asked Answered
G

1

10

I am trying to rotate a pdf 180 degrees and I am using the ITextSharp library to do so. The code below is taken from their site's examples. However, I can't seem to find the right namespace to import to get the "FileOutputStream" to work.

This is a console app, so not sure if Java's "FileOutpuStream" will work.

The PDFStamper() is structured like this:

PdfStamper(PDFReader reader, Stream os)

public void rotatePDF(string inputFile)
        {
            // get input document

         PdfReader reader = new PdfReader(inputFile);         
         PdfName pdfName = new PdfName(inputFile);
         int n = reader.NumberOfPages;
         int rot;
         PdfDictionary pageDict;
         for (int i = 1; i <= n; i++)
         {
             rot = reader.GetPageRotation(i);
             pageDict = reader.GetPageN(i);
             pageDict.Put(PdfName.ROTATE, new PdfNumber(rot + 180));
         }

         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(inputFile));
         stamper.closer();
         reader.Close();


        }
Guiltless answered 25/7, 2013 at 18:53 Comment(1)
try File.Create(outputFile) to start a new file, or File.OpenWrite(inputFile) to overwrite the original PDF.Verily
U
13

Try using a FileStream. It's in System.IO

PdfStamper stamper = new PdfStamper(reader, new FileStream(inputFile, FileMode.Create));
Urissa answered 25/7, 2013 at 18:56 Comment(3)
This seems like it will work...however, once it gets to this line I get a "The process cannot access the file _____.pdf because it is being used by another process." error. Any help with that?Guiltless
Change new PdfReader(inputFile) to new PdfReader(new FileStream(inputFile, FileMode.Open))Urissa
I generally recommend exclusively locking the file for write-only access even: new FileStream(inputFile, FileMode.Create, FileAccess.Write, FileShare.None)Lehet

© 2022 - 2024 — McMap. All rights reserved.