任何人都知道如何使用C#
中的itextsharp库识别pdf方向,无论是横向还是纵向。
以下是我的代码,它正在检索PDF
流并旋转图像,但我的问题是我们如何识别方向?
public static string ReadPdfFile(string fileName)
{
StringBuilder text = new StringBuilder();
if (File.Exists(fileName))
{
byte[] bytes = System.IO.File.ReadAllBytes(fileName);
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
PdfReader reader;
reader = new PdfReader(bytes);
int pages = reader.NumberOfPages;
// loop over document pages
for (int i = 1; i <= pages; i++)
{
page = writer.GetImportedPage(reader, i);
//Rectangle pagesize = reader.GetPageSizeWithRotation(0);
AffineTransform scale = new AffineTransform(0, 1.0F, -1.0F, 0, 500, 500);
cb.AddTemplate(page,scale);
}
doc.Close();
var rotatedFile = ms.GetBuffer();
ms.Flush();
ms.Dispose();
string filepath = @"D:\test2.pdf";
File.Delete(filepath);
using (FileStream Writer = new System.IO.FileStream(filepath, FileMode.Create, FileAccess.Write))
{
Writer.Write(rotatedFile, 0, rotatedFile.Length);
string actualFilePath = "test2.pdf";
filepath = actualFilePath;
}
}
请帮助我,提前致谢!
答案 0 :(得分:3)
我评估页面的矩形大小以找出它。要注意的事项......您可以在同一个文件中拥有多个方向,因此您无法仅根据第一页来确定文件是否为横向。如果所有页面都是横向的,则文件是横向的,否则它是混合的方向文件。
require(shiny)
datas <- data.frame(directory = c(puntaje_2016_1$estu_reside_depto), file = c(puntaje_2016_1$estu_reside_mcpio) , cole = c(puntaje_2016_1$cole_nombre_sede) )
runApp(list(
ui = basicPage(
sidebarPanel(
selectInput("directory", "Select a directory", choices = levels(datas$directory), selected = levels(datas$directory)[1]),
tags$hr(),
selectInput("files", "Select files", choices = datas$file[datas$directory == levels(datas$directory)[1]], multiple = FALSE)
## tags$hr()
##selectInput("cole", "Select files", choices = datas$cole[datas$file == levels(datas$file)[1]], multiple = FALSE)
)
),
server = function(input, output, session) {
observe({
directory <- input$directory
updateSelectInput(session, "files", choices = datas$file[datas$directory == directory])
})
##observe({
##files<-input$files
##updateSelectInput(session, "coles", choice = datas$cole[datas$file == ##files])
## })
}
))