PDF z Mieszaną Orientacją Drukuje Każdą Stronę w Krajobrazie
IronPrint 2026.1.5 nie zachowuje orientacji na poziomie strony podczas drukowania PDF, który miesza strony w orientacji pionowej i poziomej. Każda strona dociera do drukarki w orientacji poziomej, niezależnie od jej oryginalnej orientacji.
To wpływa na 2026.1.5 w Windows 10 i Windows Server 2022 pod .NET 8. Nie istnieje skorygowana wersja dla tej wersji.
Silnik stosuje jedną orientację dla całego dokumentu zamiast odczytywać wymiary każdej strony. Podzielenie pliku na serie stron o tej samej orientacji i drukowanie każdej serii oddzielnie przywraca prawidłowy wynik.
Rozwiązanie
Zalecane: Drukuj PDF w seriach zachowujących zgodność orientacji zamiast jako pojedyncze zlecenie.
1. Wykryj orientację każdej strony
Otwórz PDF i porównaj szerokość każdej strony do jej wysokości. Strona szersza niż wyższa jest w orientacji poziomej; wszystko inne jest w orientacji pionowej.
2. Grupuj kolejne strony o tej samej orientacji
Przejdź stronami w kolejności i rozpocznij nową serię, gdy tylko zmienia się orientacja. Każda seria kończy się jako ciągły zakres stron z jedną orientacją.
3. Wydrukuj każdą serię jako osobne zlecenie
Wysłanie każdego zakresu osobno pozwala drukarce zastosować orientację tego zakresu zamiast wymuszać orientację poziomą dla całego dokumentu.
4. Obsługuj ręcznie wiele kopii
Ponieważ dokument jest teraz podzielony na kilka zleceń, wykonaj petlę raz na każdą kopię i drukuj pełną sekwencję serii za każdym razem. To utrzymuje prawidłowy porządek stron w każdej kopii.
Pełna implementacja:
using IronPrint;
using IronPdf;
await PrintDocumentByOrientationBatchesAsync(
documentPath: documentPath,
printerName: printerName,
numberOfCopies: effectiveCopies);
async Task PrintDocumentByOrientationBatchesAsync(
string documentPath,
string printerName,
int numberOfCopies)
{
var tempFolder = Path.Combine(
Path.GetTempPath(),
"ironprint-orientation-workaround",
Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tempFolder);
try
{
using var pdf = PdfDocument.FromFile(documentPath);
var batches = GetConsecutiveOrientationBatches(pdf);
// Since the document is printed in multiple jobs, copies are handled manually
// to preserve the full document order per copy.
for (var copy = 1; copy <= numberOfCopies; copy++)
{
foreach (var batch in batches)
{
var batchPath = Path.Combine(
tempFolder,
$"copy_{copy}_pages_{batch.StartPageNumber}_{batch.EndPageNumber}_{batch.Orientation}.pdf");
using var batchPdf = pdf.CopyPages(batch.StartIndex, batch.EndIndex);
batchPdf.SaveAs(batchPath);
var printSettings = new PrintSettings
{
PrinterName = printerName,
NumberOfCopies = 1,
PaperOrientation = batch.Orientation == DetectedPageOrientation.Landscape
? PaperOrientation.Landscape
: PaperOrientation.Portrait,
PaperSize = PaperSize.PrinterDefault
};
await Printer.PrintAsync(batchPath, printSettings);
// Optional delay to help ensure print jobs are queued in order.
await Task.Delay(500);
}
}
}
finally
{
try
{
Directory.Delete(tempFolder, recursive: true);
}
catch
{
// Ignore cleanup errors in case the print spooler is still accessing the files.
}
}
}
List<PageOrientationBatch> GetConsecutiveOrientationBatches(PdfDocument pdf)
{
var batches = new List<PageOrientationBatch>();
if (pdf.PageCount == 0)
{
return batches;
}
var currentOrientation = GetPageOrientation(pdf.Pages[0].Width, pdf.Pages[0].Height);
var batchStartIndex = 0;
for (var pageIndex = 1; pageIndex < pdf.PageCount; pageIndex++)
{
var page = pdf.Pages[pageIndex];
var pageOrientation = GetPageOrientation(page.Width, page.Height);
if (pageOrientation != currentOrientation)
{
batches.Add(new PageOrientationBatch(
StartIndex: batchStartIndex,
EndIndex: pageIndex - 1,
Orientation: currentOrientation));
batchStartIndex = pageIndex;
currentOrientation = pageOrientation;
}
}
batches.Add(new PageOrientationBatch(
StartIndex: batchStartIndex,
EndIndex: pdf.PageCount - 1,
Orientation: currentOrientation));
return batches;
}
DetectedPageOrientation GetPageOrientation(double width, double height)
{
return width > height
? DetectedPageOrientation.Landscape
: DetectedPageOrientation.Portrait;
}
record PageOrientationBatch(
int StartIndex,
int EndIndex,
DetectedPageOrientation Orientation)
{
public int StartPageNumber => StartIndex + 1;
public int EndPageNumber => EndIndex + 1;
}
enum DetectedPageOrientation
{
Portrait,
Landscape
}Imports IronPrint
Imports IronPdf
Await PrintDocumentByOrientationBatchesAsync(documentPath:=documentPath, printerName:=printerName, numberOfCopies:=effectiveCopies)
Async Function PrintDocumentByOrientationBatchesAsync(documentPath As String, printerName As String, numberOfCopies As Integer) As Task
Dim tempFolder = Path.Combine(Path.GetTempPath(), "ironprint-orientation-workaround", Guid.NewGuid().ToString("N"))
Directory.CreateDirectory(tempFolder)
Try
Using pdf = PdfDocument.FromFile(documentPath)
Dim batches = GetConsecutiveOrientationBatches(pdf)
' Since the document is printed in multiple jobs, copies are handled manually
' to preserve the full document order per copy.
For copy = 1 To numberOfCopies
For Each batch In batches
Dim batchPath = Path.Combine(tempFolder, $"copy_{copy}_pages_{batch.StartPageNumber}_{batch.EndPageNumber}_{batch.Orientation}.pdf")
Using batchPdf = pdf.CopyPages(batch.StartIndex, batch.EndIndex)
batchPdf.SaveAs(batchPath)
Dim printSettings As New PrintSettings With {
.PrinterName = printerName,
.NumberOfCopies = 1,
.PaperOrientation = If(batch.Orientation = DetectedPageOrientation.Landscape, PaperOrientation.Landscape, PaperOrientation.Portrait),
.PaperSize = PaperSize.PrinterDefault
}
Await Printer.PrintAsync(batchPath, printSettings)
' Optional delay to help ensure print jobs are queued in order.
Await Task.Delay(500)
End Using
Next
Next
End Using
Finally
Try
Directory.Delete(tempFolder, recursive:=True)
Catch
' Ignore cleanup errors in case the print spooler is still accessing the files.
End Try
End Try
End Function
Function GetConsecutiveOrientationBatches(pdf As PdfDocument) As List(Of PageOrientationBatch)
Dim batches As New List(Of PageOrientationBatch)()
If pdf.PageCount = 0 Then
Return batches
End If
Dim currentOrientation = GetPageOrientation(pdf.Pages(0).Width, pdf.Pages(0).Height)
Dim batchStartIndex = 0
For pageIndex = 1 To pdf.PageCount - 1
Dim page = pdf.Pages(pageIndex)
Dim pageOrientation As DetectedPageOrientation = GetPageOrientation(page.Width, page.Height)
If pageOrientation <> currentOrientation Then
batches.Add(New PageOrientationBatch(StartIndex:=batchStartIndex, EndIndex:=pageIndex - 1, Orientation:=currentOrientation))
batchStartIndex = pageIndex
currentOrientation = pageOrientation
End If
Next
batches.Add(New PageOrientationBatch(StartIndex:=batchStartIndex, EndIndex:=pdf.PageCount - 1, Orientation:=currentOrientation))
Return batches
End Function
Function GetPageOrientation(width As Double, height As Double) As DetectedPageOrientation
Return If(width > height, DetectedPageOrientation.Landscape, DetectedPageOrientation.Portrait)
End Function
Record PageOrientationBatch(
StartIndex As Integer,
EndIndex As Integer,
Orientation As DetectedPageOrientation)
Public ReadOnly Property StartPageNumber As Integer
Get
Return StartIndex + 1
End Get
End Property
Public ReadOnly Property EndPageNumber As Integer
Get
Return EndIndex + 1
End Get
End Property
End Record
Enum DetectedPageOrientation
Portrait
Landscape
End EnumGetConsecutiveOrientationBatches produkuje jedno PageOrientationBatch na ciągły ciąg, a Printer.PrintAsync uruchamia każdy zakres z jego własnym PaperOrientation. Task.Delay(500) między zleceniami pomaga buforowi je zagrać w odpowiedniej kolejności.

Curtis Chau posiada tytuł licencjata z informatyki (Uniwersytet Carleton) i specjalizuje się w front-endowym rozwoju, z ekspertką w Node.js, TypeScript, JavaScript i React. Pasjonuje się tworzeniem intuicyjnych i estetycznie przyjemnych interfejsów użytkownika, Curtis cieszy się pracą z nowoczesnymi frameworkami i tworzeniem dobrze zorganizowanych, atrakcyjnych wizualnie podręczników.