我使用以下函数将pdf分成两部分.
虽然它正在分割pdf,但内容却显得颠倒了.如何将其旋转180度.
请帮忙.下面是相同的代码
private static void ExtractPages(string inputFile, string outputFile,
int start, int end)
{
// get input document
PdfReader inputPdf = new PdfReader(inputFile);
// retrieve the total number of pages
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount)
{
end = pageCount;
}
// load the input document
Document inputDoc =
new Document(inputPdf.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
// create the output writer
PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
inputDoc.Open();
PdfContentByte cb1 = outputWriter.DirectContent;
// copy pages from input to output document
for (int i = start; i <= end; i++)
{
inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(1));
inputDoc.NewPage();
PdfImportedPage page =
outputWriter.GetImportedPage(inputPdf, i);
int rotation = inputPdf.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
inputPdf.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
inputDoc.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
Tim*_*imS 24
我发现上述答案不能正确旋转所有4个主旋转.
下面是我正确处理0,90,180和270的代码.已经使用在这些方向中的每个方向上旋转的PDF进行了测试.
var pageRotation = reader.GetPageRotation(currentPageIndex);
var pageWidth = reader.GetPageSizeWithRotation(currentPageIndex).Width;
var pageHeight = reader.GetPageSizeWithRotation(currentPageIndex).Height;
switch (pageRotation)
{
case 0:
writer.DirectContent.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
break;
case 90:
writer.DirectContent.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, pageHeight);
break;
case 180:
writer.DirectContent.AddTemplate(importedPage, -1f, 0, 0, -1f, pageWidth, pageHeight);
break;
case 270:
writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0);
break;
default:
throw new InvalidOperationException(string.Format("Unexpected page rotation: [{0}].", pageRotation));
}
Run Code Online (Sandbox Code Playgroud)
小智 6
你应该试试这个.它对我有用:
if (rotation == 90 || rotation == 270)
{
if (rotation == 90)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height);
}
if (rotation == 270)
{
cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(pagenumber).Width, 0);
}
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
@TimS
' 答案非常接近完美,并为 提供了正确的参数AddTemplate
,但我需要添加一些内容以允许 PDF 旋转 90、180、270,其中页面已经旋转 0、90、180 或 270:
假设参数RotateFlipType rotateFlipType
传递给函数以指定旋转(来自 GDI+ RotateFlip 调用的方便的枚举):
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
iTextSharp.text.pdf.PdfImportedPage page;
int rotation;
int i = 0;
while (i < pageCount)
{
i++;
var pageSize = reader.GetPageSizeWithRotation(i);
// Pull in the page from the reader
page = writer.GetImportedPage(reader, i);
// Get current page rotation in degrees
rotation = pageSize.Rotation;
// Default to the current page size
iTextSharp.text.Rectangle newPageSize = null;
// Apply our additional requested rotation (switch height and width as required)
switch (rotateFlipType)
{
case RotateFlipType.RotateNoneFlipNone:
newPageSize = new iTextSharp.text.Rectangle(pageSize);
break;
case RotateFlipType.Rotate90FlipNone:
rotation += 90;
newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation);
break;
case RotateFlipType.Rotate180FlipNone:
rotation += 180;
newPageSize = new iTextSharp.text.Rectangle(pageSize.Width, pageSize.Height, rotation);
break;
case RotateFlipType.Rotate270FlipNone:
rotation += 270;
newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation);
break;
}
// Cap rotation into the 0-359 range for subsequent check
rotation %= 360;
document.SetPageSize(newPageSize);
document.NewPage();
// based on the rotation write out the page dimensions
switch (rotation)
{
case 0:
cb.AddTemplate(page, 0, 0);
break;
case 90:
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, newPageSize.Height);
break;
case 180:
cb.AddTemplate(page, -1f, 0, 0, -1f, newPageSize.Width, newPageSize.Height);
break;
case 270:
cb.AddTemplate(page, 0, 1f, -1f, 0, newPageSize.Width, 0);
break;
default:
throw new System.Exception(string.Format("Unexpected rotation of {0} degrees", rotation));
break;
}
}
Run Code Online (Sandbox Code Playgroud)
希望这将有助于其他希望纠正传入 PDF 旋转的人。我花了 2 天的时间来完善它。
我尝试了你的代码,它对我来说效果很好;分页保持其原始方向。
解决方法可能是明确地将页面旋转 180 度。
代替:
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
Run Code Online (Sandbox Code Playgroud)
和:
cb1.AddTemplate(page, -1f, 0, 0, -1f,
inputPdf.GetPageSizeWithRotation(i).Width,
inputPdf.GetPageSizeWithRotation(i).Height);
Run Code Online (Sandbox Code Playgroud)
如果您的调用返回 180,那么您可以在后面的语句inputPdf.GetPageRotation(i)
中处理这个问题(使用我建议的旋转代码 == 180)。if
归档时间: |
|
查看次数: |
31634 次 |
最近记录: |