Created
April 12, 2016 05:00
-
-
Save KirillOsenkov/657aac07efbdd4c2f8e7ac5a7d4a8054 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Linq; | |
using PdfSharp.Pdf; | |
using PdfSharp.Pdf.IO; | |
// <?xml version="1.0" encoding="utf-8"?> | |
// <packages> | |
// <package id="iTextSharp" version="5.5.9" targetFramework="net452" /> | |
// <package id="PDFsharp" version="1.32.3057.0" targetFramework="net452" /> | |
// </packages> | |
namespace PdfTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var range = Enumerable.Range(0, 1); | |
var filePath = $@"D:\source.pdf"; | |
var outputFilePath = $@"D:\Destination.pdf"; | |
// use this if PdfSharp throws an exception about unsupported iref streams in newer pdf formats | |
// filePath = WriteCompatiblePdf(filePath); | |
PdfDocument inputDocument = PdfReader.Open(filePath, PdfDocumentOpenMode.Import); | |
PdfDocument outputDocument = new PdfDocument(); | |
outputDocument.Version = inputDocument.Version; | |
outputDocument.Info.Title = inputDocument.Info.Title; | |
outputDocument.Info.Creator = inputDocument.Info.Creator; | |
foreach (var pageIndex in range) | |
{ | |
outputDocument.AddPage(inputDocument.Pages[pageIndex]); | |
} | |
outputDocument.Save(outputFilePath); | |
} | |
/// <summary> | |
/// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf | |
/// </summary> | |
/// <param name="filePath"></param> | |
/// <returns></returns> | |
public static string WriteCompatiblePdf(string filePath) | |
{ | |
string newPdfFilePath = Path.ChangeExtension(filePath, ".new.pdf"); | |
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(filePath); | |
iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1)); | |
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(newPdfFilePath, FileMode.Create)); | |
writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4); | |
document.Open(); | |
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent; | |
iTextSharp.text.pdf.PdfImportedPage page; | |
for (int i = 1; i <= reader.NumberOfPages; i++) | |
{ | |
document.SetPageSize(reader.GetPageSizeWithRotation(i)); | |
document.NewPage(); | |
page = writer.GetImportedPage(reader, i); | |
int rotation = reader.GetPageRotation(i); | |
if (rotation == 90 || rotation == 270) | |
{ | |
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); | |
} | |
else | |
{ | |
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); | |
} | |
} | |
document.Close(); | |
return newPdfFilePath; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment