You are on page 1of 3

using iTextSharp.text.

pdf;
using PdfiumViewer;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.Reflection.Metadata;

namespace Abrir_PDF
{
public partial class Form1 : Form
{
PdfiumViewer.PdfViewer pdf;
private PdfSharp.Pdf.PdfDocument pdfDocument;

public Form1()
{
InitializeComponent();
pdf = new PdfiumViewer.PdfViewer();
pdf.Width = this.Width - 20;
pdf.Height = this.Height - 40;
this.Controls.Add(pdf);
}

private void button1_Click(object sender, EventArgs e)


{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
opnefile(dialog.FileName);

}
}
public void opnefile(string filepath)
{
byte[] bytes = System.IO.File.ReadAllBytes(filepath);
var steam = new System.IO.MemoryStream(bytes);
PdfiumViewer.PdfDocument pdfDocument =
PdfiumViewer.PdfDocument.Load(steam);
pdf.Document = pdfDocument;

private void Form1_Load(object sender, EventArgs e)


{
pdf.ZoomMode = PdfViewerZoomMode.FitWidth;
}

private void Form1_Resize(object sender, EventArgs e)


{
pdf.Width = this.Width - 20;
pdf.Height = this.Height - 40;
}

public void RotatePdf(string pdfFilePath, string outputPath, int


rotateDegree)
{
iTextSharp.text.pdf.PdfReader reader = new
iTextSharp.text.pdf.PdfReader(pdfFilePath);
int pagesCount = reader.NumberOfPages;

for (int n = 1; n <= pagesCount; n++)


{
iTextSharp.text.pdf.PdfDictionary page = reader.GetPageN(n);
iTextSharp.text.pdf.PdfNumber rotate =
page.GetAsNumber(iTextSharp.text.pdf.PdfName.ROTATE);
int rotation =
rotate == null ? rotateDegree : (rotate.IntValue +
rotateDegree) % 360;

page.Put(iTextSharp.text.pdf.PdfName.ROTATE, new
iTextSharp.text.pdf.PdfNumber(rotation));
}

PdfStamper stamper = new PdfStamper(reader, new FileStream(outputPath,


FileMode.Create));
stamper.Close();
reader.Close();
}
private void RotateLeft_Click(object sender, EventArgs e)
{
string entrada = "E:\\12.pdf";
string saida = "C:\\Users\\User\\Documents\\teste.pdf";
RotatePdf(entrada, saida, -90);
}

private void RotateRight_Click(object sender, EventArgs e)


{
string entrada = "E:\\12.pdf";
string saida = "C:\\Users\\User\\Documents\\teste.pdf";
RotatePdf(entrada, saida, 90);
}

private void Save_Click(object sender, EventArgs e)


{
// if (pdfDocument == null)
// return;

/* SaveFileDialog saveFileDialog = new SaveFileDialog();


saveFileDialog.Filter = "Arquivos PDF|*.pdf";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
pdf.Document.Save(saveFileDialog.FileName);
MessageBox.Show("PDF salvo com sucesso!");
}*/

string entrada = "E:\\12.pdf";


string saida = "C:\\Users\\User\\Documents\\teste.pdf";
RotatePdf(entrada, saida, 180);
}

private void button1_Click_1(object sender, EventArgs e)


{
string entrada = "E:\\12.pdf";
string saida = "C:\\Users\\User\\Documents\\teste.pdf";
RotatePdf(entrada, saida, 270);
}
}
}

You might also like