
Dokumenty XPS i OXPS są podobne do plików PDF i określają układ, wygląd oraz informacje o drukowaniu dokumentu. Mogą wystąpić sytuacje, w których konieczna może być konwersja dokumentów XPS i OXPS do formatów obrazów JPG i PNG. W tym celu w tym artykule dowiesz się, jak konwertować dokumenty XPS i OXPS do formatu obrazu JPG i PNG za pomocą C++.
- C++ API do konwersji plików XPS, OXPS do formatu JPG lub PNG
- Konwertuj dokumenty XPS/OXPS do formatu JPG
- Konwertuj dokumenty XPS / OXPS do formatu PNG
C++ API do konwersji plików XPS, OXPS do formatu JPG lub PNG
Aspose.Page for C++ to biblioteka C++ do renderowania i manipulowania plikami XPS i PostScript. Pozwala tworzyć, czytać i aktualizować dokumenty XPS. Ponadto interfejs API obsługuje konwersję dokumentów XPS i OXPS do formatów obrazów JPG i PNG. Możesz zainstalować API przez NuGet lub pobrać bezpośrednio z sekcji downloads.
PM> Install-Package Aspose.Page.Cpp
Konwertuj dokumenty XPS/OXPS do formatu JPG
Poniżej przedstawiono kroki konwersji dokumentów XPS i OXPS do formatu JPG.
- Zainicjuj strumień ze źródłowym plikiem XPS/OXPS.
- Utwórz instancję klasy XpsDocument przy użyciu strumienia XPS/OXPS.
- Utwórz instancję klasy JpegSaveOptions.
- Określ opcje, takie jak SmoothingMode i Resolution.
- Utwórz instancję klasy ImageDevice.
- Zapisz dokument XPS/OXPS w ImageDevice za pomocą XpsDocument->Save(System::SharedPtr urządzenie, System::SharedPtr opcje) metoda.
- Iteruj przez partycje dokumentu.
- Zainicjuj strumień wyjściowy i zapisz obrazy JPG.
Poniższy przykładowy kod pokazuje, jak konwertować dokumenty XPS/OXPS do formatu JPG przy użyciu języka C++.
// Plik wejściowy
System::String inputFileName = u"SourceDirectory\\sample.xps";
//Plik wyjściowy 
System::String outputFileName = u"OutputDirectory\\XPStoImage_out.jpeg";
// Zainicjuj strumień wejściowy XPS
{
	System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(inputFileName, System::IO::FileMode::Open, System::IO::FileAccess::Read);
	// Czyszczenie zasobów w instrukcji „używanie”.
	System::Details::DisposeGuard<1> __dispose_guard_1({ xpsStream });
	// ------------------------------------------------------------------------
	try
	{
		// Załaduj dokument XPS ze strumienia
		System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
		// lub załaduj dokument XPS bezpośrednio z pliku. Wtedy nie jest potrzebny xpsStream.
		// Dokument XpsDocument = nowy XpsDocument(inputFileName, nowy XpsLoadOptions());
		// Zainicjuj obiekt JpegSaveOptions z niezbędnymi parametrami.
		System::SharedPtr<JpegSaveOptions> options = [&] { 
			auto tmp_0 = System::MakeObject<JpegSaveOptions>(); 
			tmp_0->set_SmoothingMode(System::Drawing::Drawing2D::SmoothingMode::HighQuality); 
			tmp_0->set_Resolution(300); 
			tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 2, 6 })); 
			return tmp_0; 
		}();
		// Utwórz urządzenie renderujące dla formatu JPG
		System::SharedPtr<ImageDevice> device = System::MakeObject<ImageDevice>();
		document->Save(device, options);
		// Iteruj przez partycje dokumentów (stałe dokumenty, w kategoriach XPS)
		for (int32_t i = 0; i < device->get_Result()->get_Length(); i++)
		{
			for (int32_t j = 0; j < device->get_Result()[i]->get_Length(); j++)
			{
				// Zainicjuj strumień wyjściowy obrazu
				{
					System::SharedPtr<System::IO::Stream> imageStream = System::IO::File::Open(System::IO::Path::GetDirectoryName(outputFileName) + u"\\" + System::IO::Path::GetFileNameWithoutExtension(outputFileName) + u"_" + (i + 1) + u"_" + (j + 1) + System::IO::Path::GetExtension(outputFileName), System::IO::FileMode::Create, System::IO::FileAccess::Write);
					// Czyszczenie zasobów w instrukcji „używanie”.
					System::Details::DisposeGuard<1> __dispose_guard_0({ imageStream });
					// ------------------------------------------------------------------------
					try
					{
						imageStream->Write(device->get_Result()[i][j], 0, device->get_Result()[i][j]->get_Length());
					}
					catch (...)
					{
						__dispose_guard_0.SetCurrentException(std::current_exception());
					}
				}
			}
		}
	}
	catch (...)
	{
		__dispose_guard_1.SetCurrentException(std::current_exception());
	}
}

Obraz JPG wygenerowany przez przykładowy kod
Konwertuj dokumenty XPS / OXPS do formatu PNG
Poniżej przedstawiono kroki konwersji dokumentów XPS i OXPS do formatu PNG.
- Zainicjuj strumień ze źródłowym plikiem XPS/OXPS.
- Utwórz instancję klasy XpsDocument przy użyciu strumienia XPS/OXPS.
- Utwórz instancję klasy PngSaveOptions.
- Określ opcje, takie jak SmoothingMode i Resolution.
- Utwórz instancję klasy ImageDevice.
- Zapisz dokument XPS/OXPS w ImageDevice za pomocą XpsDocument->Save(System::SharedPtr urządzenie, System::SharedPtr opcje) metoda.
- Iteruj przez partycje dokumentu.
- Zainicjuj strumień wyjściowy i zapisz obrazy PNG.
Poniższy przykładowy kod pokazuje, jak konwertować dokumenty XPS/OXPS do formatu PNG przy użyciu języka C++.
// Plik wejściowy
System::String inputFileName = u"SourceDirectory\\sample.xps";
//Plik wyjściowy 
System::String outputFileName = u"OutputDirectory\\XPStoImage_out.png";
// Zainicjuj strumień wejściowy XPS
{
	System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(inputFileName, System::IO::FileMode::Open, System::IO::FileAccess::Read);
	// Czyszczenie zasobów w instrukcji „używanie”.
	System::Details::DisposeGuard<1> __dispose_guard_1({ xpsStream });
	// ------------------------------------------------------------------------
	try
	{
		// Załaduj dokument XPS ze strumienia
		System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
		// lub załaduj dokument XPS bezpośrednio z pliku. Wtedy nie jest potrzebny xpsStream.
		// Dokument XpsDocument = nowy XpsDocument(inputFileName, nowy XpsLoadOptions());
		// Zainicjuj obiekt PngSaveOptions z niezbędnymi parametrami.
		System::SharedPtr<PngSaveOptions> options = [&] { 
			auto tmp_0 = System::MakeObject<PngSaveOptions>(); 
			tmp_0->set_SmoothingMode(System::Drawing::Drawing2D::SmoothingMode::HighQuality); 
			tmp_0->set_Resolution(300); 
			tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 2, 6 })); 
			return tmp_0; 
		}();
		// Utwórz urządzenie renderujące dla formatu PNG
		System::SharedPtr<ImageDevice> device = System::MakeObject<ImageDevice>();
		document->Save(device, options);
		// Iteruj przez partycje dokumentów (stałe dokumenty, w kategoriach XPS)
		for (int32_t i = 0; i < device->get_Result()->get_Length(); i++)
		{
			for (int32_t j = 0; j < device->get_Result()[i]->get_Length(); j++)
			{
				// Zainicjuj strumień wyjściowy obrazu
				{
					System::SharedPtr<System::IO::Stream> imageStream = System::IO::File::Open(System::IO::Path::GetDirectoryName(outputFileName) + u"\\" + System::IO::Path::GetFileNameWithoutExtension(outputFileName) + u"_" + (i + 1) + u"_" + (j + 1) + System::IO::Path::GetExtension(outputFileName), System::IO::FileMode::Create, System::IO::FileAccess::Write);
					// Czyszczenie zasobów w instrukcji „używanie”.
					System::Details::DisposeGuard<1> __dispose_guard_0({ imageStream });
					// ------------------------------------------------------------------------
					try
					{
						imageStream->Write(device->get_Result()[i][j], 0, device->get_Result()[i][j]->get_Length());
					}
					catch (...)
					{
						__dispose_guard_0.SetCurrentException(std::current_exception());
					}
				}
			}
		}
	}
	catch (...)
	{
		__dispose_guard_1.SetCurrentException(std::current_exception());
	}
}

Obraz PNG wygenerowany przez przykładowy kod
Uzyskaj bezpłatną licencję
Możesz wypróbować interfejs API bez ograniczeń ewaluacyjnych, prosząc o bezpłatną licencję tymczasową.
Wniosek
W tym artykule nauczyłeś się, jak konwertować dokumenty XPS i OXPS do formatów obrazów JPG i PNG za pomocą C++. Ponadto Aspose.Page for C++ API zapewnia różne dodatkowe formaty do użycia. Możesz szczegółowo zapoznać się z interfejsem API, odwiedzając oficjalną dokumentację. W przypadku jakichkolwiek pytań prosimy o kontakt z nami na naszym bezpłatnym forum pomocy technicznej.