本文整理匯總了C#中iTextSharp.text.pdf.PdfCopy.FreeReader方法的典型用法代碼示例。如果您正苦於以下問題:C# PdfCopy.FreeReader方法的具體用法?C# PdfCopy.FreeReader怎麽用?C# PdfCopy.FreeReader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類iTextSharp.text.pdf.PdfCopy
的用法示例。
在下文中一共展示了PdfCopy.FreeReader方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Collate
public static bool Collate(string frontSide, string backSide, string saveAs, bool backSideIsInReverseOrder = true)
{
try
{
bool backSideIsAscending = !backSideIsInReverseOrder;
using (FileStream stream = new FileStream(saveAs, FileMode.Create))
using (Document doc = new Document())
using (PdfCopy pdf = new PdfCopy(doc, stream))
{
doc.Open();
int b = 0;
using (PdfReader front = new PdfReader(frontSide))
{
using (PdfReader back = new PdfReader(backSide))
{
for (int p = 1; p <= front.NumberOfPages; p++)
{
pdf.AddPage(pdf.GetImportedPage(front, p));
pdf.FreeReader(front);
if (p <= back.NumberOfPages)
{
if (backSideIsAscending)
b = p;
else
b = back.NumberOfPages - p + 1;
pdf.AddPage(pdf.GetImportedPage(back, b));
pdf.FreeReader(back);
}
}
}
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
return true;
}
示例2: MergeFiles
public static byte[] MergeFiles(List<byte[]> sourceFiles)
{
Document document = new Document();
using (MemoryStream ms = new MemoryStream())
{
PdfCopy copy = new PdfCopy(document, ms);
document.Open();
int documentPageCounter = 0;
// Iterate through all pdf documents
for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
{
// Create pdf reader
PdfReader reader = new PdfReader(sourceFiles[fileCounter]);
int numberOfPages = reader.NumberOfPages;
// Iterate through all pages
for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
{
PdfImportedPage importedPage = copy.GetImportedPage(reader, currentPageIndex);
//***************************************
// Uncomment and alter for watermarking.
//***************************************
//
//documentPageCounter++;
//PdfCopy.PageStamp pageStamp = copy.CreatePageStamp(importedPage);
//
//// Write header
//ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
// new Phrase("PDF Merged with me.AxeyWorks PDFMerge"), importedPage.Width / 2, importedPage.Height - 30,
// importedPage.Width < importedPage.Height ? 0 : 1);
//
//// Write footer
//ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
// new Phrase($"Page {documentPageCounter}"), importedPage.Width / 2, 30,
// importedPage.Width < importedPage.Height ? 0 : 1);
//
//pageStamp.AlterContents();
copy.AddPage(importedPage);
}
copy.FreeReader(reader);
reader.Close();
}
document.Close();
return ms.GetBuffer();
}
}
示例3: Merge
public static void Merge(string file, string OutFile, ref SetupStationaryFields.addresscollection aic)
{
using (FileStream stream = new FileStream(OutFile, FileMode.Create))
{
using (Document doc = new Document())
{
using (PdfCopy pdf = new PdfCopy(doc, stream))
{
doc.Open();
PdfReader reader = null;
PdfImportedPage page = null;
//fixed typo
int ii = 0;
int count = 0;
foreach (SetupStationaryFields.addressitem ai in aic)
{
if ((!ai.ommitted))
{
reader = new PdfReader(file);
PdfReader.unethicalreading = true;
count = reader.NumberOfPages;
for (int i = 0; i <= reader.NumberOfPages - 1; i++)
{
page = pdf.GetImportedPage(reader, i + 1);
pdf.AddPage(page);
}
pdf.FreeReader(reader);
reader.Close();
ai.startpage = ((ii) * count) + 1;
ai.endpage = (ii * count) + count;
ii = ii + 1;
}
}
}
}
stream.Close();
}
}
示例4: Convert
protected override void Convert(ConversionOptions options)
{
if (m_sourceFiles != null)
{
if (m_sourceFiles.Count == 1 && !m_forceConversion)
{
this.OutputFile = m_sourceFiles[0];
}
else
{
try
{
using (MemoryStream ms = new MemoryStream())
{
using (Document doc = new Document(options.Orientation == PageOrientation.Portrait ? PageSize.LETTER : PageSize.LETTER_LANDSCAPE))
{
PdfCopy copy = new PdfCopy(doc, ms);
PdfReader reader;
doc.Open();
int n;
foreach (byte[] file in m_sourceFiles)
{
if (file != null)
{
reader = new PdfReader(file);
n = reader.NumberOfPages;
for (int page = 1; page <= n; page++)
{
copy.AddPage(copy.GetImportedPage(reader, page));
}
copy.FreeReader(reader);
}
}
}
this.OutputFile = ms.ToArray();
}
}
catch (Exception ex)
{
throw new ConversionEngineException("Error when merging Pdf files.", null, "pdf", this, ex);
}
}
}
}
示例5: Write
// ---------------------------------------------------------------------------
public void Write(Stream stream)
{
MovieLinks1 ml = new MovieLinks1();
MovieHistory mh = new MovieHistory();
List<byte[]> pdf = new List<byte[]>()
{
Utility.PdfBytes(ml),
Utility.PdfBytes(mh)
};
string[] names = { ml.ToString(), mh.ToString() };
using (ZipFile zip = new ZipFile())
{
using (MemoryStream ms = new MemoryStream())
{
// step 1
using (Document document = new Document())
{
// step 2
using (PdfCopy copy = new PdfCopy(document, ms))
{
// step 3
document.Open();
// step 4
for (int i = 0; i < pdf.Count; ++i)
{
zip.AddEntry(Utility.ResultFileName(names[i] + ".pdf"), pdf[i]);
PdfReader reader = new PdfReader(pdf[i]);
// loop over the pages in that document
int n = reader.NumberOfPages;
for (int page = 0; page < n; )
{
copy.AddPage(copy.GetImportedPage(reader, ++page));
}
copy.FreeReader(reader);
reader.Close();
}
}
}
zip.AddEntry(RESULT, ms.ToArray());
}
zip.Save(stream);
}
}
示例6: AddDataSheets
// ---------------------------------------------------------------------------
/**
* Fills out a data sheet, flattens it, and adds it to a combined PDF.
* @param copy the PdfCopy instance (can also be PdfSmartCopy)
*/
public void AddDataSheets(PdfCopy copy)
{
IEnumerable<Movie> movies = PojoFactory.GetMovies();
// Loop over all the movies and fill out the data sheet
foreach (Movie movie in movies)
{
PdfReader reader = new PdfReader(DATASHEET_PATH);
using (var ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms))
{
Fill(stamper.AcroFields, movie);
stamper.FormFlattening = true;
}
reader.Close();
reader = new PdfReader(ms.ToArray());
copy.AddPage(copy.GetImportedPage(reader, 1));
copy.FreeReader(reader);
}
reader.Close();
}
}
示例7: InsertDestinationsIntoDocument
private void InsertDestinationsIntoDocument(string document, IEnumerable<INamedDestination> nameddests)
{
if (!File.Exists(document))
throw new FileNotFoundException("Document not found.", document);
var extensionIndex = document.IndexOf(".pdf");
var tempDoc = document.Substring(0, extensionIndex) + "-2.pdf";
var doc = new Document();
var copy = new PdfCopy(doc, new FileStream(tempDoc, FileMode.Create));
doc.Open();
var reader = new PdfReader(document);
copy.Outlines = GetBookmarksFromDocument(document).ToList();
for (int page = 0; page < reader.NumberOfPages; )
{
copy.AddPage(copy.GetImportedPage(reader, ++page));
}
foreach (var destination in nameddests)
{
copy.AddNamedDestination(destination.Name, destination.Page, destination.Destination);
}
copy.FreeReader(reader);
reader.Close();
doc.Close();
// TODO: Uniqueness tests?
}
示例8: Merge
//99% of credit goes to this person: http://stackoverflow.com/a/20491695
public static bool Merge(List<string> fullFilePaths, string saveAs)
{
try
{
using (FileStream stream = new FileStream(saveAs, FileMode.Create))
using (Document doc = new Document())
using (PdfCopy pdf = new PdfCopy(doc, stream))
{
doc.Open();
PdfReader reader = null;
PdfImportedPage page = null;
fullFilePaths.ForEach(file =>
{
using (reader = new PdfReader(file))
{
for (int i = 0; i < reader.NumberOfPages; i++)
{
page = pdf.GetImportedPage(reader, i + 1);
pdf.AddPage(page);
}
pdf.FreeReader(reader);
}
});
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
return true;
}
示例9: EvenOddMerge
/// <summary>
/// Takes pages from two pdf files, and produces an output file
/// whose odd pages are the contents of the first, and
/// even pages are the contents of the second. Useful for
/// merging front/back output from single sided scanners.
/// </summary>
/// <param name="oddPageFile">The file supplying odd numbered pages</param>
/// <param name="evenPageFile">The file supplying even numbered pages</param>
/// <param name="outputFile">The output file containing the merged pages</param>
/// <param name="skipExtraPages">Set to true to skip any extra pages if
/// one file is longer than the other</param>
public void EvenOddMerge(String oddPageFile, String evenPageFile,
String outputFile, bool skipExtraPages)
{
if (!String.IsNullOrEmpty(oddPageFile) && !String.IsNullOrWhiteSpace(oddPageFile) &&
!String.IsNullOrEmpty(evenPageFile) && !String.IsNullOrWhiteSpace(evenPageFile) &&
!String.IsNullOrEmpty(outputFile) && !String.IsNullOrWhiteSpace(outputFile))
{
var oddPageDocument = new iTextSharpPDF.PdfReader(oddPageFile);
var evenPageDocument = new iTextSharpPDF.PdfReader(evenPageFile);
try
{
iTextSharpText.Document mergedOutputDocument = null;
iTextSharpPDF.PdfCopy mergedOutputFile = null;
int lastPage = 0;
switch (skipExtraPages)
{
case (false):
lastPage = oddPageDocument.NumberOfPages;
if (evenPageDocument.NumberOfPages > oddPageDocument.NumberOfPages)
lastPage = evenPageDocument.NumberOfPages;
else
lastPage = oddPageDocument.NumberOfPages;
break;
case (true):
if (evenPageDocument.NumberOfPages < oddPageDocument.NumberOfPages)
lastPage = evenPageDocument.NumberOfPages;
else
lastPage = oddPageDocument.NumberOfPages;
break;
}
try
{
mergedOutputDocument = new iTextSharpText.Document();
mergedOutputFile = new iTextSharpPDF.PdfCopy(mergedOutputDocument, new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite));
mergedOutputDocument.Open();
for (int loop = 1; loop <= lastPage; loop++)
{
// Extract and merge odd page
if (loop <= oddPageDocument.NumberOfPages)
{
mergedOutputDocument.SetPageSize(oddPageDocument.GetPageSizeWithRotation(loop));
mergedOutputFile.AddPage(mergedOutputFile.GetImportedPage(oddPageDocument, loop));
}
// Extract and merge even page
if (loop <= evenPageDocument.NumberOfPages)
{
mergedOutputDocument.SetPageSize(evenPageDocument.GetPageSizeWithRotation(loop));
mergedOutputFile.AddPage(mergedOutputFile.GetImportedPage(evenPageDocument, loop));
}
}
}
finally
{
if (mergedOutputDocument != null && mergedOutputDocument.IsOpen()) mergedOutputDocument.Close();
if (mergedOutputFile != null)
{
mergedOutputFile.Close();
mergedOutputFile.FreeReader(oddPageDocument);
mergedOutputFile.FreeReader(evenPageDocument);
}
}
}
catch
{
try
{
File.Delete(outputFile);
}
catch { }
throw;
}
finally
{
try
{
if (oddPageDocument != null) oddPageDocument.Close();
oddPageDocument = null;
}
catch { }
try
{
if (evenPageDocument != null) evenPageDocument.Close();
evenPageDocument = null;
}
catch { }
}
}
//.........這裏部分代碼省略.........
示例10: SplitPDF
/// <summary>
///
/// </summary>
/// <param name="inputFile">The PDF file to split</param>
/// <param name="splitStartPages"></param>
public void SplitPDF(String inputFile, SortedList<int, String> splitStartPages)
{
if (!String.IsNullOrEmpty(inputFile) &&
!String.IsNullOrWhiteSpace(inputFile) &&
splitStartPages != null &&
splitStartPages.Count >= 2)
{
var inputDocument = new iTextSharpPDF.PdfReader(inputFile);
// First split must begin with page 1
// Last split must not be higher than last page
if (splitStartPages.Keys[0] == 1 &&
splitStartPages.Keys[splitStartPages.Count - 1] <= inputDocument.NumberOfPages)
{
int currentPage = 1;
int firstPageOfSplit;
int lastPageOfSplit;
try
{
for (int splitPoint = 0; splitPoint <= (splitStartPages.Count - 1); splitPoint++)
{
firstPageOfSplit = currentPage;
if (splitPoint < (splitStartPages.Count - 1))
{
lastPageOfSplit = splitStartPages.Keys[splitPoint + 1] - 1;
}
else
{
lastPageOfSplit = inputDocument.NumberOfPages;
}
iTextSharpText.Document splitDocument = null;
iTextSharpPDF.PdfCopy splitOutputFile = null;
try
{
splitDocument = new iTextSharpText.Document();
splitOutputFile = new iTextSharpPDF.PdfCopy(splitDocument, new FileStream(splitStartPages.Values[splitPoint], FileMode.Create, FileAccess.ReadWrite));
splitDocument.Open();
for (int outputPage = firstPageOfSplit; outputPage <= lastPageOfSplit; outputPage++)
{
splitDocument.SetPageSize(inputDocument.GetPageSizeWithRotation(currentPage));
splitOutputFile.AddPage(splitOutputFile.GetImportedPage(inputDocument, currentPage));
currentPage++;
}
}
finally
{
if (splitDocument != null && splitDocument.IsOpen()) splitDocument.Close();
if (splitOutputFile != null)
{
splitOutputFile.Close();
splitOutputFile.FreeReader(inputDocument);
}
splitDocument = null;
splitOutputFile = null;
}
}
}
catch
{
// Cleanup any files that may have
// been written
foreach (KeyValuePair<int, String> split in splitStartPages)
{
try
{
File.Delete(split.Value);
}
catch { }
}
throw;
}
finally
{
if (inputDocument != null) inputDocument.Close();
}
}
else
{
if (splitStartPages.Keys[splitStartPages.Count - 1] > inputDocument.NumberOfPages) throw new ArgumentOutOfRangeException("splitStartPages", String.Format("Final page number must be less than the number of pages ({0}). Passed value is {1}.", inputDocument.NumberOfPages, splitStartPages.Keys[splitStartPages.Count - 1]));
throw new ArgumentOutOfRangeException("splitStartPages", "First page number must be 1.");
}
}
else
{
if (inputFile == null) throw new ArgumentNullException("inputFile", exceptionArgumentNullOrEmptyString);
if (splitStartPages == null) throw new ArgumentNullException("splitStartPages", exceptionArgumentNullOrEmptyString);
throw new ArgumentOutOfRangeException("splitStartPages", "Must contain at least two KeyValue pairs.");
}
}
示例11: ExtractPDFPages
/// <summary>
/// Extracts a range of pages from a PDF file,
/// and writes them to a new file.
/// </summary>
/// <param name="inputFile">The PDF to extract pages from.</param>
/// <param name="outputFile">The new file to write the extracted pages to.</param>
/// <param name="firstPage">The first page to extract.</param>
/// <param name="lastPage">The last page to extract.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <remarks><see cref="FileStream"/> constructor exceptions may also be thrown.</remarks>
public void ExtractPDFPages(String inputFile, String outputFile, int firstPage, int lastPage)
{
if (!String.IsNullOrEmpty(inputFile) && !String.IsNullOrWhiteSpace(inputFile) &&
!String.IsNullOrEmpty(outputFile) && !String.IsNullOrWhiteSpace(outputFile) &&
firstPage > 0 && lastPage > 0 &&
lastPage >= firstPage)
{
var inputDocument = new iTextSharpPDF.PdfReader(inputFile);
try
{
// Page numbers specified must not be greater
// than the number of pages in the document
if (firstPage <= inputDocument.NumberOfPages &&
lastPage <= inputDocument.NumberOfPages)
{
iTextSharpText.Document extractOutputDocument = null;
iTextSharpPDF.PdfCopy extractOutputFile = null;
try
{
extractOutputDocument = new iTextSharpText.Document();
extractOutputFile = new iTextSharpPDF.PdfCopy(extractOutputDocument, new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite));
extractOutputDocument.Open();
for (int loop = firstPage; loop <= lastPage; loop++)
{
extractOutputDocument.SetPageSize(inputDocument.GetPageSizeWithRotation(loop));
extractOutputFile.AddPage(extractOutputFile.GetImportedPage(inputDocument, loop));
}
}
finally
{
if (extractOutputDocument != null && extractOutputDocument.IsOpen()) extractOutputDocument.Close();
if (extractOutputFile != null)
{
extractOutputFile.Close();
extractOutputFile.FreeReader(inputDocument);
}
}
}
else
{
if (firstPage > inputDocument.NumberOfPages) throw new ArgumentOutOfRangeException("firstPage", String.Format(exceptionParameterCannotBeGreaterThan,"firstPage", "the number of pages in the document."));
throw new ArgumentOutOfRangeException("lastPage", String.Format(exceptionParameterCannotBeGreaterThan,"firstPage", "the number of pages in the document."));
}
}
catch
{
try
{
File.Delete(outputFile);
}
catch { }
throw;
}
finally
{
if (inputDocument != null) inputDocument.Close();
inputDocument = null;
}
}
else
{
if (String.IsNullOrEmpty(inputFile) || String.IsNullOrWhiteSpace(inputFile)) throw new ArgumentNullException("inputFile", exceptionArgumentNullOrEmptyString);
if (String.IsNullOrEmpty(outputFile) || String.IsNullOrWhiteSpace(outputFile)) throw new ArgumentNullException("outputFile", exceptionArgumentNullOrEmptyString);
if (firstPage < 1) throw new ArgumentOutOfRangeException("firstPage", exceptionArgumentZeroOrNegative);
if (lastPage < 1) throw new ArgumentOutOfRangeException("lastPage", exceptionArgumentZeroOrNegative);
if (lastPage < firstPage) throw new ArgumentOutOfRangeException("lastPage", String.Format(exceptionParameterCannotBeLessThan, "lastPage", "firstPage"));
}
}
示例12: Merge
public static bool Merge(List<PDFFiles> InFiles, string OutFile)
{
try
{
for(int i=0; i<InFiles.Count; i++)
{
PdfReader reader = null;
reader = new PdfReader(InFiles[i].filePath);
if (!reader.IsOpenedWithFullPermissions)
{
string newUnlockedFile = unlockPDF(InFiles[i].filePath); //throw new System.IO.FileLoadException("Cannot merge because \"" + file.fileName + "\" is Locked for editing");
InFiles.Remove(InFiles[i]);
InFiles.Insert(i, new PDFFiles(newUnlockedFile));
}
}
}
catch (FileNotFoundException ex)
{
throw ex;
}
catch (System.IO.FileLoadException)
{
throw;
}
catch
{
return false;
}
try
{
using (FileStream stream = new FileStream(OutFile, FileMode.Create))
using (Document doc = new Document(PageSize.A4))
using (PdfCopy pdf = new PdfCopy(doc, stream))
{
doc.Open();
PdfReader reader = null;
PdfImportedPage page = null;
//fixed typo
InFiles.ForEach(file =>
{
reader = new PdfReader(file.filePath);
for (int i = 0; i < reader.NumberOfPages; i++)
{
page = pdf.GetImportedPage(reader, i + 1);
//doc.SetPageSize(page.Width <= page.Height ? PageSize.A4 : PageSize.A4.Rotate());
pdf.AddPage(page);
}
pdf.FreeReader(reader);
reader.Close();
});
}
}
catch
{
return false;
}
ScaleToA4(OutFile, OutFile);
return true;
}
示例13: ConcatenatePDFFiles
/// <summary>
/// Concatenates two or more PDF files into one file.
/// </summary>
/// <param name="inputFiles">A string array containing the names of the pdf files to concatenate</param>
/// <param name="outputFile">Name of the concatenated file.</param>
public void ConcatenatePDFFiles(String[] inputFiles, String outputFile)
{
if (inputFiles != null && inputFiles.Length > 0)
{
if (!String.IsNullOrEmpty(outputFile) && !String.IsNullOrWhiteSpace(outputFile))
{
var concatDocument = new iTextSharpText.Document();
var outputCopy = new iTextSharpPDF.PdfCopy(concatDocument, new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite));
concatDocument.Open();
try
{
for (int loop = 0; loop <= inputFiles.GetUpperBound(0); loop++)
{
var inputDocument = new iTextSharpPDF.PdfReader(inputFiles[loop]);
for (int pageLoop = 1; pageLoop <= inputDocument.NumberOfPages; pageLoop++)
{
concatDocument.SetPageSize(inputDocument.GetPageSizeWithRotation(pageLoop));
outputCopy.AddPage(outputCopy.GetImportedPage(inputDocument, pageLoop));
}
inputDocument.Close();
outputCopy.FreeReader(inputDocument);
inputDocument = null;
}
concatDocument.Close();
outputCopy.Close();
}
catch
{
if (concatDocument != null && concatDocument.IsOpen()) concatDocument.Close();
if (outputCopy != null) outputCopy.Close();
if (File.Exists(outputFile))
{
try
{
File.Delete(outputFile);
}
catch { }
}
throw;
}
}
else
{
throw new ArgumentNullException("outputFile", exceptionArgumentNullOrEmptyString);
}
}
else
{
throw new ArgumentNullException("inputFiles", exceptionArgumentNullOrEmptyString);
}
}